#include <bits/stdc++.h>
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define svec(x) sort(x.begin(), x.end())
#define press(x) x.erase(unique(x.begin(), x.end()), x.end())
#define lb(x, v) lower_bound(x.begin(), x.end(), v)
#define ub(x, v) upper_bound(x.begin(), x.end(), v)
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<int, LL> pil;
typedef pair<LL, int> pli;
const LL llinf=2000000000000000000;
const LL mod1=1000000007;
const LL mod2=998244353;
const int inf=2000000000;
int n, m, ans[300010];
pii edge[300010];
bool ch[300010];
vector<int> link[300010];
int d[300010], sp[300010][30], num, p[300010], cnt[300010];
void dfs(int num, int par){
d[num]=d[par]+1, sp[num][0]=par, p[num]=num;
for(auto i:link[num]){
if(i!=par)dfs(i, num);
}
}
int lca(int x, int y){
if(d[x]>d[y])swap(x, y);
for(int i=19; i>=0; i--){
if(d[y]-d[x]>=(1<<i))y=sp[y][i];
}
if(x==y)return x;
for(int i=19; i>=0; i--){
if(sp[x][i]!=sp[y][i]){
x=sp[x][i];
y=sp[y][i];
}
}
return sp[x][0];
}
int main(){
scanf("%d %d", &n, &m);
for(int i=1; i<=m; i++)scanf("%d %d", &edge[i].F, &edge[i].S);
for(int i=1; i<n; i++){
int a;
scanf("%d", &a);
ch[a]=true;
link[edge[a].F].eb(edge[a].S);
link[edge[a].S].eb(edge[a].F);
}
dfs(1, 0);
for(int j=1; j<20; j++){
for(int i=1; i<=n; i++)sp[i][j]=sp[sp[i][j-1]][j-1];
}
for(int i=1; i<=m; i++){
if(d[edge[i].F]<d[edge[i].S])swap(edge[i].F, edge[i].S);
if(ch[i])cnt[edge[i].F]=i;
}
for(int i=1; i<=m; i++){
if(ans[i])continue;
if(ch[i]){
ans[i]=++num;
p[edge[i].F]=p[edge[i].S];
continue;
}
int l=lca(edge[i].F, edge[i].S);
int a=p[edge[i].F], b=p[edge[i].S];
vector<int> vc;
while(d[a]>d[l]){vc.eb(cnt[a]); p[a]=p[l]; a=p[sp[a][0]];}
while(d[b]>d[l]){vc.eb(cnt[b]); p[b]=p[l]; b=p[sp[b][0]];}
svec(vc);
for(auto j:vc){
if(!ans[j])ans[j]=++num;
}
ans[i]=++num;
}
for(int i=1; i<=m; i++)printf("%d ", ans[i]);
}
Compilation message
riggedroads.cpp: In function 'int main()':
riggedroads.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
riggedroads.cpp:48:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1; i<=m; i++)scanf("%d %d", &edge[i].F, &edge[i].S);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
riggedroads.cpp:51:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a);
~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
7424 KB |
Output is correct |
2 |
Correct |
10 ms |
7424 KB |
Output is correct |
3 |
Correct |
9 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
7424 KB |
Output is correct |
2 |
Correct |
10 ms |
7424 KB |
Output is correct |
3 |
Correct |
9 ms |
7424 KB |
Output is correct |
4 |
Correct |
9 ms |
7552 KB |
Output is correct |
5 |
Correct |
11 ms |
7552 KB |
Output is correct |
6 |
Correct |
10 ms |
7552 KB |
Output is correct |
7 |
Correct |
11 ms |
7552 KB |
Output is correct |
8 |
Correct |
10 ms |
7552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
25456 KB |
Output is correct |
2 |
Correct |
163 ms |
31984 KB |
Output is correct |
3 |
Correct |
149 ms |
15992 KB |
Output is correct |
4 |
Correct |
234 ms |
57316 KB |
Output is correct |
5 |
Correct |
258 ms |
59620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
164 ms |
31900 KB |
Output is correct |
2 |
Correct |
109 ms |
18680 KB |
Output is correct |
3 |
Correct |
58 ms |
13300 KB |
Output is correct |
4 |
Correct |
136 ms |
27156 KB |
Output is correct |
5 |
Correct |
42 ms |
15480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
378 ms |
57456 KB |
Output is correct |
2 |
Correct |
381 ms |
63796 KB |
Output is correct |
3 |
Correct |
83 ms |
23444 KB |
Output is correct |
4 |
Correct |
132 ms |
31504 KB |
Output is correct |
5 |
Correct |
429 ms |
69176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
264 ms |
41180 KB |
Output is correct |
2 |
Correct |
156 ms |
32760 KB |
Output is correct |
3 |
Correct |
504 ms |
66296 KB |
Output is correct |
4 |
Correct |
436 ms |
62072 KB |
Output is correct |
5 |
Correct |
32 ms |
12664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
7424 KB |
Output is correct |
2 |
Correct |
10 ms |
7424 KB |
Output is correct |
3 |
Correct |
9 ms |
7424 KB |
Output is correct |
4 |
Correct |
9 ms |
7552 KB |
Output is correct |
5 |
Correct |
11 ms |
7552 KB |
Output is correct |
6 |
Correct |
10 ms |
7552 KB |
Output is correct |
7 |
Correct |
11 ms |
7552 KB |
Output is correct |
8 |
Correct |
10 ms |
7552 KB |
Output is correct |
9 |
Correct |
90 ms |
25456 KB |
Output is correct |
10 |
Correct |
163 ms |
31984 KB |
Output is correct |
11 |
Correct |
149 ms |
15992 KB |
Output is correct |
12 |
Correct |
234 ms |
57316 KB |
Output is correct |
13 |
Correct |
258 ms |
59620 KB |
Output is correct |
14 |
Correct |
164 ms |
31900 KB |
Output is correct |
15 |
Correct |
109 ms |
18680 KB |
Output is correct |
16 |
Correct |
58 ms |
13300 KB |
Output is correct |
17 |
Correct |
136 ms |
27156 KB |
Output is correct |
18 |
Correct |
42 ms |
15480 KB |
Output is correct |
19 |
Correct |
378 ms |
57456 KB |
Output is correct |
20 |
Correct |
381 ms |
63796 KB |
Output is correct |
21 |
Correct |
83 ms |
23444 KB |
Output is correct |
22 |
Correct |
132 ms |
31504 KB |
Output is correct |
23 |
Correct |
429 ms |
69176 KB |
Output is correct |
24 |
Correct |
264 ms |
41180 KB |
Output is correct |
25 |
Correct |
156 ms |
32760 KB |
Output is correct |
26 |
Correct |
504 ms |
66296 KB |
Output is correct |
27 |
Correct |
436 ms |
62072 KB |
Output is correct |
28 |
Correct |
32 ms |
12664 KB |
Output is correct |
29 |
Correct |
563 ms |
59740 KB |
Output is correct |
30 |
Correct |
539 ms |
63040 KB |
Output is correct |
31 |
Correct |
485 ms |
63024 KB |
Output is correct |
32 |
Correct |
176 ms |
18808 KB |
Output is correct |
33 |
Correct |
500 ms |
63352 KB |
Output is correct |