QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#790647#8055. BalanceShwStoneWA 141ms48768kbC++143.3kb2024-11-28 14:24:462024-11-28 14:24:46

Judging History

你现在查看的是最新测评结果

  • [2024-11-28 14:24:46]
  • 评测
  • 测评结果:WA
  • 用时:141ms
  • 内存:48768kb
  • [2024-11-28 14:24:46]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

const int MAXN(5e5 + 5);

int n, m, mm;
vector<int> graph[MAXN], st, tree[MAXN], as;
int cnta[MAXN], dp[MAXN], ans[MAXN];
int uu[MAXN], vv[MAXN], bel[MAXN];
int tsiz[MAXN], dep[MAXN], color[MAXN];
int dfn[MAXN], low[MAXN], dfnCnt, bccCnt;
bool inSt[MAXN], hasa[MAXN];

void tarjan(int u, int from) {
	dfn[u] = low[u] = ++dfnCnt;
	st.emplace_back(u); inSt[u] = true;
	for (int i : graph[u]) {
		if (i == from) continue;
		int v = uu[i] ^ vv[i] ^ u;
		if (!dfn[v]) {
			tarjan(v, i);
			low[u] = min(low[u], low[v]);
		} else if (inSt[v]) low[u] = min(low[u], dfn[v]);
	}
	if (low[u] == dfn[u]) {
		int v;
		bccCnt++;
		do {
			v = st.back();
			st.pop_back();
			inSt[v] = false;
			bel[v] = bccCnt;
			tsiz[bccCnt]++;
		} while (u != v);
	}
}

void dfs(int u) {
	for (int v : tree[u]) {
		if (dep[v]) continue;
		dep[v] = dep[u] + 1;
		dfs(v);
		tsiz[u] += tsiz[v];
	}
}

bool check(int u, int v) {
	if (dep[u] < dep[v]) return hasa[tsiz[v]];
	else return hasa[n - tsiz[u]];
}

void dfs1(int u, int f) {
	for (int v : tree[u]) {
		if (v == f) continue;
		dfs1(v, u);
		dp[u] = max(dp[u], dp[v] + check(u, v));
	}
}

void dfs2(int u, int f) {
	vector<int> tmp1, tmp2;
	tmp1.emplace_back(0);
	for (int v : tree[u]) {
		tmp1.emplace_back(dp[v] + check(u, v));
		tmp2.emplace_back(dp[v] + check(u, v));
	}
	
	tmp2.emplace_back(0);
	reverse(tmp2.begin(), tmp2.end());
	int cnt1 = 0, cnt2 = tmp2.size() - 2;

	for (int i = 1; i < tmp1.size(); i++) {
		tmp1[i] = max(tmp1[i], tmp1[i - 1]);
		tmp2[i] = max(tmp2[i], tmp2[i - 1]);
	}
	ans[u] = tmp1.back();

	int tdp = dp[u];
	for (int v : tree[u]) {
		dp[u] = max(tmp1[cnt1], tmp2[cnt2]);
		cnt1++; cnt2--;
		if (v != f) dfs2(v, u);
	}
	dp[u] = tdp;
}

void dfs3(int u, int f, int c) {
	color[u] = c;
	for (int v : tree[u]) {
		if (v != f) dfs3(v, u, c);
	}
}

void dfs4(int u, int f, int id) {
	bool flag = false;
	color[u] = as[id];
	for (int v : tree[u]) {
		if (v == f) continue;
		if (!flag && dp[u] == dp[v] + check(u, v)) {
			flag = true;
			dfs4(v, u, id - check(u, v));
		} else dfs3(v, u, as[id]);
	}
}

void solve() {
	scanf("%d %d", &n, &m);
	dfnCnt = bccCnt = 0;
	as.clear();
	for (int i = 1; i <= n; i++) {
		dfn[i] = low[i] = 0;
		graph[i].clear();
		tree[i].clear();
		tsiz[i] = cnta[i] = dep[i] = 0;
		dp[i] = ans[i] = 0;
		hasa[i] = false;
	}
	for (int i = 1; i <= m; i++) {
		scanf("%d %d", uu + i, vv + i);
		graph[uu[i]].emplace_back(i);
		graph[vv[i]].emplace_back(i);
	}
	for (int i = 1; i <= n; i++) {
		int x;
		scanf("%d", &x);
		cnta[x]++;
	}
	for (int i = 1; i <= n; i++) {
		if (cnta[i]) as.emplace_back(i);
		cnta[i] += cnta[i - 1];
		hasa[cnta[i]] = true;
	}
	dep[1] = 1;
	tarjan(1, 0);
	for (int i = 1; i <= m; i++) {
		int u = bel[uu[i]], v = bel[vv[i]];
		if (u != v) {
			tree[u].emplace_back(v);
			tree[v].emplace_back(u);
		}
	}
	dfs(1);
	dfs1(1, 0);
	dfs2(1, 0);
	for (int i = 1; i <= bccCnt; i++) {
		if (ans[i] >= as.size() - 1) {
			puts("Yes");
			dfs1(i, 0);
			dfs4(i, 0, as.size() - 1);
			for (int u = 1; u <= n; u++) {
				printf("%d%c", color[bel[u]], " \n"[u == n]);
			}
			return;
		}
	}
	puts("No");
}

int main() {
	int _;
	scanf("%d", &_);
	while (_--) {
		solve();
	}
	return 0;
}


详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 48768kb

input:

5
5 4
1 2
2 3
3 4
4 5
1 2 3 4 5
5 4
1 2
1 3
1 4
1 5
1 2 3 4 5
5 4
1 2
1 3
1 4
1 5
1 2 2 2 3
5 6
1 2
1 2
2 3
3 4
4 5
3 5
1 2 1 2 1
2 2
1 2
1 2
1 2

output:

Yes
1 2 3 4 5
No
Yes
2 3 1 2 2
Yes
2 2 1 1 1
No

result:

ok ok (5 test cases)

Test #2:

score: 0
Accepted
time: 105ms
memory: 48440kb

input:

100000
4 3
4 2
1 3
2 1
2 1 3 2
5 7
2 5
5 3
2 3
5 1
4 3
2 4
4 3
1 3 1 1 2
3 2
2 1
2 3
1 1 1
4 7
3 4
1 4
2 3
4 3
4 2
3 1
4 3
2 3 3 2
4 6
2 4
3 1
1 2
1 2
2 3
4 2
1 1 3 3
4 7
3 2
4 2
1 2
3 4
3 2
2 4
3 4
2 1 1 1
5 5
3 2
1 5
4 5
4 3
2 1
1 2 2 3 2
3 5
2 3
1 3
3 1
3 1
2 1
3 2 3
2 3
2 1
2 1
1 2
1 1
2 3
2 1
1...

output:

Yes
2 2 3 1
No
Yes
1 1 1
No
No
Yes
2 1 1 1
No
No
Yes
1 1
Yes
1 1
Yes
1 1
Yes
1 1 1 1
No
Yes
1 1 1 1 1
Yes
1 3 1 1 1
Yes
1 1 1
Yes
1 2
Yes
1 1 1 1 1
Yes
1 2
No
Yes
1 1
Yes
1 1 1
Yes
1 1
Yes
1 1 1 1
Yes
1 1
Yes
2 2 2 2 2
Yes
1 1 1 1 1
Yes
1 1
Yes
1 1 1 2
No
Yes
1 1
No
Yes
1 1
No
No
No
Yes
1 1 1 1 2
Ye...

result:

ok ok (100000 test cases)

Test #3:

score: -100
Wrong Answer
time: 141ms
memory: 47916kb

input:

83335
9 17
1 4
8 9
5 2
1 3
2 7
1 7
2 8
6 7
2 4
1 8
5 8
7 1
8 6
4 6
4 7
6 9
7 9
7 3 4 4 7 4 2 4 8
6 9
3 1
1 2
3 5
1 2
3 4
4 5
6 3
6 1
6 2
4 3 2 2 1 3
9 8
9 3
5 7
5 9
2 6
1 8
4 1
4 2
4 3
4 2 5 3 4 5 4 5 4
6 7
2 1
1 5
6 1
3 1
6 5
2 4
5 3
2 1 2 1 2 1
4 6
2 1
4 2
1 4
1 2
1 4
3 1
2 2 4 2
6 10
2 3
3 5
2 1
...

output:

No
No
Yes
3 4 4 4 5 4 5 2 5
No
Yes
2 2 4 2
No
Yes
2 3 3 3
Yes
2 2 1
No
Yes
1 1 1 1 1
No
Yes
1 1 1
Yes
1 1 3 3 3
Yes
1 1
Yes
1 1
Yes
1 1 1 1
Yes
3 1 3
No
Yes
1 1 1 1 1 1 1 1
Yes
1 1 1 1 1 1 1
No
Yes
1 1
No
Yes
1 1 1 1 1
Yes
2 1 1 2 1
No
Yes
1 2 3 1 3 3 3 1
No
No
No
No
No
No
No
No
No
No
No
No
Yes
7 6 ...

result:

wrong answer 1-th smallest numbers are not same (test case 40)