#include <bits/stdc++.h>
#define pii pair<int,int>
#define x first
#define y second
using namespace std;
const int N=300005;
int n,timer=1,lg=20,tin[N],p[N],m,t=1,ans[N],yes[N],tout[N],used[N];
vector<pii> ed;
vector<int> g[N],dp[N];
int nom[N];
void DFS(int v,int p){
used[v]=1;
dp[v][0]=p;
for(int k=1;k<=19;k++)
dp[v][k]=dp[dp[v][k-1]][k-1];
tin[v]=timer++;
for(int k=0;k<g[v].size();k++){
int to=g[v][k];
if(used[to]==0)
DFS(to,v);
}
tout[v]=timer++;
}
bool tree(int a,int b){
if(tin[a]<=tin[b] && tout[a]>=tout[b])
return true;
return false;
}
int LCA(int a,int b){
if(tree(a,b)) return a;
if(tree(b,a)) return b;
int t=a;
for(int k=19;k>=0;k--){
if(dp[t][k]!=0){
int to=dp[t][k];
if(!tree(to,b))
t=to;
}
}
return dp[t][0];
}
int FS(int v){
if(v==p[v]) return v;
return p[v]=FS(p[v]);
}
void US(int a,int b){
a=FS(a);
b=FS(b);
if(a!=b){
if(tree(b,a))
swap(a,b);
p[b]=a;
}
a=FS(a);
b=FS(b);
}
main(){
cin>>n>>m;
for(int k=1;k<=m;k++){
int a,b;
cin>>a>>b;
ed.push_back({a,b});
}
vector<int> op;
for(int k=1;k<n;k++){
int a;
cin>>a;
a--;
op.push_back(a);
yes[a]=1;
g[ed[a].x].push_back(ed[a].y);
g[ed[a].y].push_back(ed[a].x);
}
for(int k=0;k<=n;k++){
dp[k].resize(lg+1,0);
p[k]=k;
}
DFS(1,0);
for(int k=0;k<op.size();k++){
int a=ed[op[k]].x,b=ed[op[k]].y;
if(tree(b,a))
swap(a,b);
nom[b]=op[k];
}
for(int k=0;k<m;k++){
if(ans[k]==0){
if(yes[k]){
US(ed[k].x,ed[k].y);
ans[k]=t;
}
else{
int a=ed[k].x,b=ed[k].y;
int lc=LCA(a,b);
set<int> v;
while(FS(lc)!=FS(b) || FS(lc)!=FS(a)){
if(FS(lc)!=FS(a)){
int lo=nom[a];
if(!ans[lo])
v.insert(lo);
US(a,dp[a][0]);
a=FS(a);
}
if(FS(lc)!=FS(b)){
int lo=nom[b];
if(!ans[lo])
v.insert(lo);
US(b,dp[b][0]);
b=FS(b);
}
}
for(auto i : v){
ans[i]=t;
t++;
}
ans[k]=t;
}
t++;
}
cout<<ans[k]<<' ';
}
return 0;
}
Compilation message
riggedroads.cpp: In function 'void DFS(int, int)':
riggedroads.cpp:14:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
14 | for(int k=1;k<=19;k++)
| ^~~
riggedroads.cpp:16:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
16 | tin[v]=timer++;
| ^~~
riggedroads.cpp:17:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for(int k=0;k<g[v].size();k++){
| ~^~~~~~~~~~~~
riggedroads.cpp: At global scope:
riggedroads.cpp:57:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
57 | main(){
| ^
riggedroads.cpp: In function 'int main()':
riggedroads.cpp:79:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for(int k=0;k<op.size();k++){
| ~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14592 KB |
Output is correct |
2 |
Correct |
10 ms |
14464 KB |
Output is correct |
3 |
Correct |
11 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14592 KB |
Output is correct |
2 |
Correct |
10 ms |
14464 KB |
Output is correct |
3 |
Correct |
11 ms |
14464 KB |
Output is correct |
4 |
Correct |
12 ms |
14592 KB |
Output is correct |
5 |
Correct |
12 ms |
14592 KB |
Output is correct |
6 |
Correct |
13 ms |
14592 KB |
Output is correct |
7 |
Correct |
11 ms |
14592 KB |
Output is correct |
8 |
Correct |
12 ms |
14592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
211 ms |
33192 KB |
Output is correct |
2 |
Correct |
401 ms |
40756 KB |
Output is correct |
3 |
Correct |
400 ms |
26800 KB |
Output is correct |
4 |
Correct |
592 ms |
66012 KB |
Output is correct |
5 |
Correct |
624 ms |
68572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
360 ms |
42840 KB |
Output is correct |
2 |
Correct |
215 ms |
26220 KB |
Output is correct |
3 |
Correct |
113 ms |
20592 KB |
Output is correct |
4 |
Correct |
265 ms |
35528 KB |
Output is correct |
5 |
Correct |
112 ms |
22880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
769 ms |
73480 KB |
Output is correct |
2 |
Correct |
1006 ms |
82892 KB |
Output is correct |
3 |
Correct |
257 ms |
33632 KB |
Output is correct |
4 |
Correct |
356 ms |
40864 KB |
Output is correct |
5 |
Correct |
995 ms |
85720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
785 ms |
52864 KB |
Output is correct |
2 |
Correct |
409 ms |
39400 KB |
Output is correct |
3 |
Correct |
1159 ms |
70928 KB |
Output is correct |
4 |
Correct |
1089 ms |
67056 KB |
Output is correct |
5 |
Correct |
73 ms |
19444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14592 KB |
Output is correct |
2 |
Correct |
10 ms |
14464 KB |
Output is correct |
3 |
Correct |
11 ms |
14464 KB |
Output is correct |
4 |
Correct |
12 ms |
14592 KB |
Output is correct |
5 |
Correct |
12 ms |
14592 KB |
Output is correct |
6 |
Correct |
13 ms |
14592 KB |
Output is correct |
7 |
Correct |
11 ms |
14592 KB |
Output is correct |
8 |
Correct |
12 ms |
14592 KB |
Output is correct |
9 |
Correct |
211 ms |
33192 KB |
Output is correct |
10 |
Correct |
401 ms |
40756 KB |
Output is correct |
11 |
Correct |
400 ms |
26800 KB |
Output is correct |
12 |
Correct |
592 ms |
66012 KB |
Output is correct |
13 |
Correct |
624 ms |
68572 KB |
Output is correct |
14 |
Correct |
360 ms |
42840 KB |
Output is correct |
15 |
Correct |
215 ms |
26220 KB |
Output is correct |
16 |
Correct |
113 ms |
20592 KB |
Output is correct |
17 |
Correct |
265 ms |
35528 KB |
Output is correct |
18 |
Correct |
112 ms |
22880 KB |
Output is correct |
19 |
Correct |
769 ms |
73480 KB |
Output is correct |
20 |
Correct |
1006 ms |
82892 KB |
Output is correct |
21 |
Correct |
257 ms |
33632 KB |
Output is correct |
22 |
Correct |
356 ms |
40864 KB |
Output is correct |
23 |
Correct |
995 ms |
85720 KB |
Output is correct |
24 |
Correct |
785 ms |
52864 KB |
Output is correct |
25 |
Correct |
409 ms |
39400 KB |
Output is correct |
26 |
Correct |
1159 ms |
70928 KB |
Output is correct |
27 |
Correct |
1089 ms |
67056 KB |
Output is correct |
28 |
Correct |
73 ms |
19444 KB |
Output is correct |
29 |
Correct |
1238 ms |
66836 KB |
Output is correct |
30 |
Correct |
1274 ms |
72760 KB |
Output is correct |
31 |
Correct |
1080 ms |
69212 KB |
Output is correct |
32 |
Correct |
365 ms |
26740 KB |
Output is correct |
33 |
Correct |
1047 ms |
69404 KB |
Output is correct |