QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#747756#8140. Customs Controls 2Repeater#WA 0ms3872kbC++202.2kb2024-11-14 18:08:072024-11-14 18:08:07

Judging History

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

  • [2024-11-14 18:08:07]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3872kb
  • [2024-11-14 18:08:07]
  • 提交

answer

#include<iostream>
using namespace std;
#include<vector>

using i64 = long long;

void solve(){
	int n, m;
	cin >> n >> m;

	vector<int> cnt(n + 1);
	vector<vector<int>> adj(n + 1), adj1(n + 1), adj2(n + 1);
	for(int i = 0; i < m; ++i){
		int u, v;
		cin >> u >> v;

		adj1[v].emplace_back(u);
	}

	auto dfs = [&](auto dfs, int u) -> void{
		cnt[u] = 1;
		for(int i: adj1[u]){
			if(cnt[i] == 1) continue;
			dfs(dfs, i);
		}
		return;
	};

	vector<int> indeg(n + 1);
	dfs(dfs, n);
	for(int i = 1; i <= n; ++i){
		for(int v: adj1[i]){
			if(cnt[v] == 1){
				adj2[i].emplace_back(v);
				adj[v].emplace_back(i);
				indeg[i]++;
			}
		}
	}

	vector<i64> fa(2 * n + 1), mx(2 * n + 1), tmx(n + 1);
	for(int i = 1; i <= 2 * n; ++i) {
		fa[i] = i, mx[i] = 0;
		if(i <= n) cnt[i] = 0;
	}

	vector<int> ans(n + 1);

	vector<int> q(n + 1);
	int l = 0, r = -1;

	auto find = [&](auto find, int i) -> int{
		if(fa[i] != i) fa[i] = find(find, fa[i]);
		return fa[i];
	};

	for(int v: adj[1]){
		q[++r] = v;
		if(indeg[v] > 1){
			cout << "No\n";
			return;
		}
		int t = find(find, 1);
		fa[t] = v + n;
	}

	while(l <= r){
		int u = q[l++];
		int v = find(find, u + n);
		for(int i: adj2[u]){
			if(ans[i] == 0)
				ans[i] = mx[v] + 1 - tmx[i];
		}

		tmx[u] = mx[v] + 1;
		mx[u] = mx[v] + 1;

		for(int i: adj[u]){
			cnt[i]++;
			int t = find(find, i + n), v = find(find, u);
			if(t != v){
				fa[v] = t;
				mx[t] = max(mx[t], mx[v]);
			}
			if(indeg[i] == cnt[i]) q[++r] = i;
		}
	}

	for(int i = 1; i <= n; ++i) cnt[i] = 0;

	vector<i64> sum(n + 1);
	sum[1] = 1;

	l = 0, r = -1;
	q[++r] = 1;

	while(l <= r){
		int u = q[l++];
		for(int v: adj[u]){
			if(sum[v] != 0 && sum[v] != sum[u] + ans[v]){
				cout << "No\n";
				return;
			}
			sum[v] = sum[u] + ans[v];
			cnt[v]++;
			if(cnt[v] == indeg[v]) q[++r] = v;
		}
	}

	// for(int i = 1; i <= n; ++i) cout << sum[i] << " ";
	// 	cout << "\n";

	ans[n] = 1;
	cout << "Yes\n";
	for(int i = 1; i <= n; ++i){
		if(ans[i] == 0) ans[i] = 1;
		cout << ans[i] << " ";
	}
	cout << "\n";

	return;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int t = 1;
	cin >> t;
	while(t--) solve();

	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3792kb

input:

2
3 3
1 2
1 3
2 3
8 9
1 2
1 3
1 4
2 5
3 6
4 7
5 8
6 8
7 8

output:

No
Yes
1 1 1 1 1 1 1 1 

result:

ok ok (2 test cases)

Test #2:

score: 0
Accepted
time: 0ms
memory: 3628kb

input:

2
11 16
1 2
1 3
1 4
1 5
2 6
4 6
3 7
4 7
5 8
6 8
2 9
3 9
7 10
8 10
9 11
10 11
8 10
1 2
1 3
2 4
3 5
3 6
4 6
2 7
5 7
6 8
7 8

output:

Yes
1 1 1 1 2 1 2 1 3 1 1 
No

result:

ok ok (2 test cases)

Test #3:

score: 0
Accepted
time: 0ms
memory: 3660kb

input:

1
8 10
1 2
1 3
2 4
3 5
3 6
4 6
2 7
5 7
6 8
7 8

output:

No

result:

ok ok (1 test case)

Test #4:

score: 0
Accepted
time: 0ms
memory: 3872kb

input:

1
11 16
1 2
1 3
1 4
1 5
2 6
4 6
3 7
4 7
5 8
6 8
2 9
3 9
7 10
8 10
9 11
10 11

output:

Yes
1 1 1 1 2 1 2 1 3 1 1 

result:

ok ok (1 test case)

Test #5:

score: 0
Accepted
time: 0ms
memory: 3828kb

input:

1
3 3
1 2
1 3
2 3

output:

No

result:

ok ok (1 test case)

Test #6:

score: 0
Accepted
time: 0ms
memory: 3860kb

input:

1
15 24
1 3
1 7
1 6
1 12
3 11
3 5
3 13
3 14
3 8
7 11
7 13
7 14
11 2
11 10
6 9
5 9
13 9
14 4
8 4
2 4
10 4
9 4
12 15
4 15

output:

Yes
1 1 1 1 1 2 1 2 1 1 1 4 1 2 1 

result:

ok ok (1 test case)

Test #7:

score: -100
Wrong Answer
time: 0ms
memory: 3588kb

input:

10
20 40
1 8
1 11
1 19
8 7
8 16
8 15
8 14
8 4
8 17
11 5
11 6
11 2
11 3
7 6
7 2
7 3
16 9
16 12
15 9
15 12
14 9
4 18
4 10
5 13
5 10
6 18
6 10
2 13
2 18
3 18
3 10
9 13
9 10
12 13
12 10
19 20
17 20
13 20
18 20
10 20
20 30
8 19
19 12
5 12
5 10
10 4
18 4
18 14
14 6
15 6
15 7
7 3
17 3
17 2
2 16
9 16
9 11
1...

output:

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

result:

wrong answer participant failed to find a solution (test case 3)