QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#747877 | #8140. Customs Controls 2 | Repeater | WA | 0ms | 3872kb | C++20 | 1.9kb | 2024-11-14 18:34:17 | 2024-11-14 18:34:18 |
Judging History
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), indeg(n + 1);
vector<vector<int>> adj(n + 1), adj1(n + 1);
for(int i = 0; i < m; ++i){
int u, v;
cin >> u >> v;
adj[u].emplace_back(v);
adj1[v].emplace_back(u);
indeg[v]++;
}
vector<i64> fa(2 * n + 1), mx(2 * n + 1), tmx(n + 1);
for(int i = 1; i <= 2 * n; ++i) fa[i] = i;
vector<int> ans(n + 1), 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: adj1[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;
ans[n] = 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";
for(int i = 1; i <= n; ++i){
if(ans[i] > 1e9 || ans[i] <= 0){
cout << "No\n";
return;
}
}
cout << "Yes\n";
for(int i = 1; i <= n; ++i){
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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3628kb
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: 3624kb
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: 3552kb
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: 3568kb
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: 3620kb
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: 3816kb
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: 3872kb
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 Yes 1 1 1 4 4 1 2 1 2 2 3 3 4 2 2 1 1 1 1 1 Yes 1 3 3 4 2 1 1 5 3 1 1 1 1 1 2 1 2 2 2 1 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 Yes 1 2 1 1 4 4 1 1 2 1 2 1 1 3 1 2 5 1 3 1 No Yes 1 1 3 1 3 2 2 1 1 1 ...
result:
wrong answer participant failed to find a solution (test case 10)