#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)ans[j]=++num;
ans[i]=++num;
}
assert(num==m);
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);
~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7424 KB |
Output is correct |
2 |
Correct |
9 ms |
7424 KB |
Output is correct |
3 |
Runtime error |
19 ms |
14848 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7424 KB |
Output is correct |
2 |
Correct |
9 ms |
7424 KB |
Output is correct |
3 |
Runtime error |
19 ms |
14848 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
26864 KB |
Output is correct |
2 |
Correct |
194 ms |
34032 KB |
Output is correct |
3 |
Correct |
168 ms |
18076 KB |
Output is correct |
4 |
Correct |
239 ms |
59364 KB |
Output is correct |
5 |
Correct |
260 ms |
61668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
197 ms |
64540 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
347 ms |
59432 KB |
Output is correct |
2 |
Correct |
379 ms |
65820 KB |
Output is correct |
3 |
Correct |
90 ms |
24724 KB |
Output is correct |
4 |
Correct |
141 ms |
33468 KB |
Output is correct |
5 |
Correct |
450 ms |
70980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
293 ms |
83540 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7424 KB |
Output is correct |
2 |
Correct |
9 ms |
7424 KB |
Output is correct |
3 |
Runtime error |
19 ms |
14848 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |