티스토리 뷰

https://www.acmicpc.net/problem/24955

// https://www.acmicpc.net/problem/24955
class Node {
	constructor(item) {
		this.item = item;
		this.next = null;
	}
}

class Queue {
	constructor() {
		this.head = null;
		this.tail = null;
		this.length = 0;
	}

	push(item) {
		const node = new Node(item);
		if (this.head == null) {
			this.head = node;
		} else {
			this.tail.next = node;
		}

		this.tail = node;
		this.length += 1;
	}

	pop() {
		const popItem = this.head;
		this.head = this.head.next;
		this.length -= 1;
		return popItem.item;
	}
}

const MOD = 1_000_000_007;
const input = require('fs')
	.readFileSync('./dev/stdin')
	.toString()
	.trim()
	.split('\n')
	.map((v) => v.trim().split(' ').map(Number));

const [N, _] = input.shift();
const nums = input.shift();

const edges = Array.from(new Array(N), () => []);

function clacMod(main, value) {
	for (let i = value; i > 0; i = Math.floor(i / 10)) {
		main = (main * 10) % MOD;
	}
	main += value;
	main %= MOD;
	return main;
}

for (let i = 1; i < N; i++) {
	const [a, b] = input.shift().map((v) => v - 1);
	edges[a].push(b);
	edges[b].push(a);
}

const answers = input.map((v) => {
	const [s, e] = v.map((v) => v - 1);
	const answer = Array(N).fill(-1);
	const q = new Queue();
	q.push(s);
	answer[s] = nums[s];
	while (q.length > 0) {
		const now = q.pop();
		edges[now].forEach((next) => {
			if (answer[next] == -1) {
				answer[next] = clacMod(answer[now], nums[next]);
				q.push(next);
			}
		});
	}
	return answer[e];
});
console.log(answers.join('\n'));

 

 

 

 

 

728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함