TLE 60pts 求调
查看原帖
TLE 60pts 求调
424089
LuoTianyi_Official楼主2023/11/15 08:50
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> g[10005], w[10005], Sprague, Garundy;
int a[105], sz[10005], dis[10005], ans[105], rt, rzh, m;
bool vis[10005], cncagn[10000005];
void dfs1(int x, int fa) {
	sz[x] = 1;
	int maxx=1;
	for (int i: g[x]) if (i != fa && !vis[i]) {
		dfs1(i, x);
		sz[x] += sz[i];
		maxx = max(maxx, sz[i]);
	}
	maxx = max(maxx, sz[x]);
	if (maxx < rzh) rzh = maxx, rt = x;
}
void dfs2(int x, int fa) {
	Sprague.push_back(dis[x]);
	Garundy.push_back(dis[x]);
	for (int i=0; i<g[x].size(); i++) if (g[x][i] != fa && !vis[g[x][i]]) {
		dis[g[x][i]] = dis[x] + w[x][i];
		dfs2(g[x][i], x);
	}
}
void qwasd(int x) {
	vis[x] = cncagn[0] = true;
	Garundy.clear();
	for (int i=0; i<g[x].size(); i++) {
		dis[g[x][i]] = w[x][i];
		Sprague.clear();
		dfs2(g[x][i], x);
		for (int j: Sprague) for (int k=1; k<=m; k++) a[k] >= j && j <= 10000000 && (ans[k] |= cncagn[a[k] - j]);
		for (int j: Sprague) j <= 10000000 && (cncagn[j] = true);
	}
	for (int i: Garundy) i <= 10000000 && (cncagn[i] = false);
	for (int i=0; i<g[x].size(); i++) if (!vis[g[x][i]]) {
		rzh = 0x3fffffff;
		dfs1(g[x][i], 0);
		qwasd(g[x][i]);
	}
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n, x, y, v;
	cin >> n >> m;
	for (int i=1; i<n; i++) {
		cin >> x >> y >> v;
		g[x].push_back(y);
		g[y].push_back(x);
		w[x].push_back(v);
		w[y].push_back(v);
	}
	for (int i=1; i<=m; i++) cin >> a[i];
	rzh = 0x3fffffff;
	dfs1(1, 0);
	qwasd(rt);
	for (int i=1; i<=m; i++) cout << (ans[i] ? "AYE" : "NAY") << endl;
}
2023/11/15 08:50
加载中...