예제 #1
0
int main() {
	freopen("input.txt", "r", stdin);
	scanf("%d%d%d", &n, &s, &m);
	for (int i = 1; i <= s; i++) {
		int x; scanf("%d", &x);
		w[x] = true;
	}
	for (int i = 1; i <= m; i++) {
		scanf("%d%d%d", &r[i].x, &r[i].y, &r[i].c);
		addedge(r[i].x, r[i].y, r[i].c);
	}
	dijkstra();
	t = 0;
	std::fill(h + 1, h + n + 1, 0);
	std::fill(fa + 1, fa + s + 1, 0);
	for (int i = 1; i <= m; i++) {
		tree[i] = (Road){c[r[i].x], c[r[i].y], d[r[i].x] + d[r[i].y] + r[i].c};
	}
	std::sort(tree + 1, tree + m + 1);
	for (int i = 1; i <= m; i++) {
		int fx = getfa(tree[i].x), fy = getfa(tree[i].y);
		if (fx == fy) continue;
		addedge(tree[i].x, tree[i].y, tree[i].c);
		fa[fx] = fy;
	}
	std::fill(v + 1, v + n + 1, false);
	for (int i = 1; i <= s; i++)
		if (!v[i]) buildtree(i);
	scanf("%d", &q);
	for (int cs = 1; cs <= q; cs++) {
		int x, y, d;
		scanf("%d%d%d", &x, &y, &d);
		x = c[x];
		y = c[y];
		if (getfa(x) != getfa(y)) {
			puts("NIE");
			continue;
		}
		int l = getLca(x, y);
		long long vmax = -INF;
		vmax = std::max(vmax, getValue(x, dep[x] - dep[l]));
		vmax = std::max(vmax, getValue(y, dep[y] - dep[l]));
		printf("%s\n", (vmax <= d) ? "TAK" : "NIE");
	}
	return 0;
}
int main()
{
	scanf("%d%d", &n, &m);
	
	for (int i = 0, a, b; i < n-1; i++) {
		scanf("%d%d", &a, &b);
		
		adjList[a].push_back(b);
		adjList[b].push_back(a);
	}
	
	ancestors[0][0] = 0;
	setOrders(0);
	
	for (int lvl = 1; lvl < 19; lvl++)
		for (int i = 0; i < n; i++)
			ancestors[i][lvl] = ancestors[ ancestors[i][lvl-1] ][lvl - 1];
	
	int a, b, k;
	while (m--) {
		scanf("%d%d%d", &a, &b, &k);
		
		if (isAncestor(a, b)) {
			traffic[a] += k;
			traffic[b] -= k;
		}
		else if (isAncestor(b, a)) {
			traffic[b] += k;
			traffic[a] -= k;
		}
		else {
			traffic[a] += k;
			traffic[b] += k;
			traffic[getLca(a, b)] -= 2*k;
		}
	}
	
	setMaxTraffic(0);
	
	printf("%lld\n", maxTraffic);
	return 0;
}