#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pair<int, int> >
#define fi first
#define se second
int n,m;
int A[300005], B[300005];
int par[300005];
pi P[300005];
vector<int>tmp;
vector<pi>v[300005];
int dep[300005];
int cnt = 1;
int getr(int x){
return (par[x] == x ? x : par[x] = getr(par[x]));
}
void merge(int a, int b){
a = getr(a), b = getr(b);
if(dep[a] > dep[b])swap(a, b);
if(a != b)par[b] = a;
}
bool vis[500005];
int mrk[500005];
void solve(){
cin >> n >> m;
for(int i=1;i<=m;i++)cin >> A[i] >> B[i];
for(int i=1;i<n;i++){
int x;cin >> x;
vis[x] = 1;
v[A[x]].push_back({B[x], x});
v[B[x]].push_back({A[x], x});
}
queue<pi>q;
q.push({1, -1});
while(!q.empty()){
pi x = q.front(); q.pop();
if(x.se == -1)dep[x.fi] = 1;
if(x.se != -1)P[x.fi] = {(A[x.se] == x.fi ? B[x.se] : A[x.se]),x.se};
for(auto [i, j] : v[x.fi])if(!dep[i])q.push({i, j}), dep[i] = dep[x.fi] + 1;
}
//for(int i=1;i<=n;i++)cout << P[i].fi << " " << P[i].se << '\n';
set<int>s;
for(int i=1;i<=n;i++)par[i] = i;
for(int i=1;i<=m;i++){
if(mrk[i])continue;
if(vis[i])mrk[i] = cnt++, merge(A[i], B[i]);
else{
tmp.clear();
int a = A[i], b = B[i];
a = getr(a), b = getr(b);
while(a != b){
if(dep[a] > dep[b]){
tmp.push_back(P[a].se);
merge(P[a].fi, a);
a = getr(a);
}
else{
tmp.push_back(P[b].se);
merge(P[b].fi, b);
b = getr(b);
}
}
sort(tmp.begin(), tmp.end());
for(auto j : tmp)if(!mrk[j])mrk[j] = cnt++;
mrk[i] = cnt++;
}
}
for(int i=1;i<=m;i++)cout << mrk[i] << " ";
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int t = 1;
//cin >> t;
while(t--){
solve();
}
}
Compilation message
riggedroads.cpp:76:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
76 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
6 ms |
7508 KB |
Output is correct |
5 |
Correct |
4 ms |
7508 KB |
Output is correct |
6 |
Correct |
6 ms |
7524 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
21156 KB |
Output is correct |
2 |
Correct |
94 ms |
27944 KB |
Output is correct |
3 |
Correct |
92 ms |
20708 KB |
Output is correct |
4 |
Correct |
152 ms |
45660 KB |
Output is correct |
5 |
Correct |
161 ms |
47692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
23548 KB |
Output is correct |
2 |
Correct |
55 ms |
15964 KB |
Output is correct |
3 |
Correct |
24 ms |
11712 KB |
Output is correct |
4 |
Correct |
55 ms |
19216 KB |
Output is correct |
5 |
Correct |
23 ms |
12036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
219 ms |
40756 KB |
Output is correct |
2 |
Correct |
266 ms |
43684 KB |
Output is correct |
3 |
Correct |
50 ms |
17508 KB |
Output is correct |
4 |
Correct |
89 ms |
22324 KB |
Output is correct |
5 |
Correct |
370 ms |
48824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
174 ms |
29272 KB |
Output is correct |
2 |
Correct |
100 ms |
21652 KB |
Output is correct |
3 |
Correct |
301 ms |
43160 KB |
Output is correct |
4 |
Correct |
309 ms |
40916 KB |
Output is correct |
5 |
Correct |
18 ms |
10740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
6 ms |
7508 KB |
Output is correct |
5 |
Correct |
4 ms |
7508 KB |
Output is correct |
6 |
Correct |
6 ms |
7524 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7508 KB |
Output is correct |
9 |
Correct |
55 ms |
21156 KB |
Output is correct |
10 |
Correct |
94 ms |
27944 KB |
Output is correct |
11 |
Correct |
92 ms |
20708 KB |
Output is correct |
12 |
Correct |
152 ms |
45660 KB |
Output is correct |
13 |
Correct |
161 ms |
47692 KB |
Output is correct |
14 |
Correct |
102 ms |
23548 KB |
Output is correct |
15 |
Correct |
55 ms |
15964 KB |
Output is correct |
16 |
Correct |
24 ms |
11712 KB |
Output is correct |
17 |
Correct |
55 ms |
19216 KB |
Output is correct |
18 |
Correct |
23 ms |
12036 KB |
Output is correct |
19 |
Correct |
219 ms |
40756 KB |
Output is correct |
20 |
Correct |
266 ms |
43684 KB |
Output is correct |
21 |
Correct |
50 ms |
17508 KB |
Output is correct |
22 |
Correct |
89 ms |
22324 KB |
Output is correct |
23 |
Correct |
370 ms |
48824 KB |
Output is correct |
24 |
Correct |
174 ms |
29272 KB |
Output is correct |
25 |
Correct |
100 ms |
21652 KB |
Output is correct |
26 |
Correct |
301 ms |
43160 KB |
Output is correct |
27 |
Correct |
309 ms |
40916 KB |
Output is correct |
28 |
Correct |
18 ms |
10740 KB |
Output is correct |
29 |
Correct |
298 ms |
41672 KB |
Output is correct |
30 |
Correct |
334 ms |
44300 KB |
Output is correct |
31 |
Correct |
282 ms |
44132 KB |
Output is correct |
32 |
Correct |
72 ms |
19852 KB |
Output is correct |
33 |
Correct |
291 ms |
43652 KB |
Output is correct |