#include <bits/stdc++.h>
using namespace std;
int dd[300005];
int sz[300005],cha[300005];
int head[300005];
int sauhld[300005];
int pos[300005],maxx[300005];
int timee;
vector<int>g[300005];
pair<int,int>tree[600005];
int lim=300000;
void upd(int p,int value,int pos) {
p--;
for (tree[p += lim] ={value,pos}; p > 1; p >>= 1) tree[p>>1] =min(tree[p] , tree[p^1]);
}
pair<int,int>query(int l, int r) {
pair<int,int>res={1e9,0};
if(l>r)return res;
l--;
for (l += lim, r += lim; l < r; l >>= 1, r >>= 1) {
if (l&1) res = min(res,tree[l++]);
if (r&1) res = min(res,tree[--r]);
}
return res;
}
int vt[300005];
struct canh
{
int u,v,c;
}e[300005];
int db[300005];
int id[300005];
int n,m;
void dfs(int u,int pa)
{
sz[u]=1;
cha[u]=pa;
maxx[u]=0;
int vtmax=0;
for(int i=0;i<g[u].size();i++)
if(g[u][i]!=pa)
{
int v=g[u][i];
dfs(v,u);
sz[u]+=sz[v];
if(sz[v]>maxx[u]){vtmax=i;maxx[u]=sz[v];}
}
swap(g[u][0],g[u][vtmax]);
}
void hld(int u)
{
timee++;pos[u]=timee;
vt[timee]=u;
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i];
if(cha[v]==u)
{
if(2*sz[v]>=sz[u]&&i==0)
{
head[v]=head[u];
sauhld[v]=sauhld[u];
}
else
{
head[v]=v;
sauhld[v]=sauhld[u]+1;
}
hld(v);
}
}}
bool cmp(int a,int b)
{
return id[a]<id[b];
}
int cur=0;
void update(int u, int v)
{
vector<int>vec;
if(sauhld[u]<sauhld[v])swap(u,v);
while(sauhld[u]>sauhld[v])
{
while(1)
{
auto[c,uu]=query(pos[head[u]],pos[u]);
if(c==1e9)break;
vec.push_back(uu);
upd(pos[uu],1e9,0);
}
u=cha[head[u]];
}
while(head[u]!=head[v])
{
while(1)
{
auto[c,uu]=query(pos[head[u]],pos[u]);
if(c==1e9)break;
vec.push_back(uu);
upd(pos[uu],1e9,0);
}
u=cha[head[u]];
while(1)
{
auto[c,uu]=query(pos[head[v]],pos[v]);
if(c==1e9)break;
vec.push_back(uu);
upd(pos[uu],1e9,0);
}v=cha[head[v]];
}
if(pos[u]<pos[v])swap(u,v);
while(1)
{
auto[c,uu]=query(pos[v]+1,pos[u]);
if(c==1e9)break;
vec.push_back(uu);
upd(pos[uu],1e9,0);
}
sort(vec.begin(),vec.end(),cmp);
for(auto vv:vec)
{
e[id[vv]].c=cur+1;cur++;
}
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>e[i].u>>e[i].v;
}
for(int i=1;i<n;i++)
{
cin>>db[i];
dd[db[i]]=1;
g[e[db[i]].u].push_back(e[db[i]].v);
g[e[db[i]].v].push_back(e[db[i]].u);
}
sauhld[1]=head[1]=1;
dfs(1,0);
hld(1);
for(int i=2;i<=n;i++)upd(pos[i],0,i);
for(int i=1;i<n;i++)
{
auto[u,v,_]=e[db[i]];
if(u==cha[v])swap(u,v);
id[u]=db[i];
}
for(int i=1;i<=m;i++)
{
auto[u,v,_]=e[i];
if(dd[i]==1)
{
if(u==cha[v])swap(u,v);
if(query(pos[u],pos[u]).first==0){e[i].c=cur+1;cur++;upd(pos[u],1e9,0);}
}
else
{
update(u,v);
e[i].c=cur+1;
cur++;
}
}
for(int i=1;i<=m;i++)cout<<e[i].c<<" ";
return 0;
}
Compilation message
riggedroads.cpp: In function 'void dfs(int, int)':
riggedroads.cpp:41:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(int i=0;i<g[u].size();i++)
| ~^~~~~~~~~~~~
riggedroads.cpp: In function 'void hld(int)':
riggedroads.cpp:55:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int i=0;i<g[u].size();i++)
| ~^~~~~~~~~~~~
riggedroads.cpp: In function 'void update(int, int)':
riggedroads.cpp:86:5: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
86 | auto[c,uu]=query(pos[head[u]],pos[u]);
| ^
riggedroads.cpp:97:5: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
97 | auto[c,uu]=query(pos[head[u]],pos[u]);
| ^
riggedroads.cpp:105:5: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
105 | auto[c,uu]=query(pos[head[v]],pos[v]);
| ^
riggedroads.cpp:114:5: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
114 | auto[c,uu]=query(pos[v]+1,pos[u]);
| ^
riggedroads.cpp: In function 'int main()':
riggedroads.cpp:146:5: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
146 | auto[u,v,_]=e[db[i]];
| ^
riggedroads.cpp:152:5: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
152 | auto[u,v,_]=e[i];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
26972 KB |
Output is correct |
2 |
Correct |
5 ms |
26972 KB |
Output is correct |
3 |
Correct |
4 ms |
27080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
26972 KB |
Output is correct |
2 |
Correct |
5 ms |
26972 KB |
Output is correct |
3 |
Correct |
4 ms |
27080 KB |
Output is correct |
4 |
Correct |
5 ms |
26972 KB |
Output is correct |
5 |
Correct |
4 ms |
26972 KB |
Output is correct |
6 |
Correct |
4 ms |
27116 KB |
Output is correct |
7 |
Correct |
4 ms |
26972 KB |
Output is correct |
8 |
Correct |
4 ms |
26972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
32204 KB |
Output is correct |
2 |
Correct |
89 ms |
34820 KB |
Output is correct |
3 |
Correct |
80 ms |
31724 KB |
Output is correct |
4 |
Correct |
139 ms |
41168 KB |
Output is correct |
5 |
Correct |
150 ms |
42020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
41080 KB |
Output is correct |
2 |
Correct |
64 ms |
32848 KB |
Output is correct |
3 |
Correct |
30 ms |
30428 KB |
Output is correct |
4 |
Correct |
66 ms |
37684 KB |
Output is correct |
5 |
Correct |
25 ms |
31580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
218 ms |
50608 KB |
Output is correct |
2 |
Correct |
308 ms |
56264 KB |
Output is correct |
3 |
Correct |
49 ms |
35656 KB |
Output is correct |
4 |
Correct |
72 ms |
39660 KB |
Output is correct |
5 |
Correct |
274 ms |
54788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
45020 KB |
Output is correct |
2 |
Correct |
85 ms |
39880 KB |
Output is correct |
3 |
Correct |
312 ms |
50652 KB |
Output is correct |
4 |
Correct |
255 ms |
48468 KB |
Output is correct |
5 |
Correct |
20 ms |
28752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
26972 KB |
Output is correct |
2 |
Correct |
5 ms |
26972 KB |
Output is correct |
3 |
Correct |
4 ms |
27080 KB |
Output is correct |
4 |
Correct |
5 ms |
26972 KB |
Output is correct |
5 |
Correct |
4 ms |
26972 KB |
Output is correct |
6 |
Correct |
4 ms |
27116 KB |
Output is correct |
7 |
Correct |
4 ms |
26972 KB |
Output is correct |
8 |
Correct |
4 ms |
26972 KB |
Output is correct |
9 |
Correct |
53 ms |
32204 KB |
Output is correct |
10 |
Correct |
89 ms |
34820 KB |
Output is correct |
11 |
Correct |
80 ms |
31724 KB |
Output is correct |
12 |
Correct |
139 ms |
41168 KB |
Output is correct |
13 |
Correct |
150 ms |
42020 KB |
Output is correct |
14 |
Correct |
91 ms |
41080 KB |
Output is correct |
15 |
Correct |
64 ms |
32848 KB |
Output is correct |
16 |
Correct |
30 ms |
30428 KB |
Output is correct |
17 |
Correct |
66 ms |
37684 KB |
Output is correct |
18 |
Correct |
25 ms |
31580 KB |
Output is correct |
19 |
Correct |
218 ms |
50608 KB |
Output is correct |
20 |
Correct |
308 ms |
56264 KB |
Output is correct |
21 |
Correct |
49 ms |
35656 KB |
Output is correct |
22 |
Correct |
72 ms |
39660 KB |
Output is correct |
23 |
Correct |
274 ms |
54788 KB |
Output is correct |
24 |
Correct |
169 ms |
45020 KB |
Output is correct |
25 |
Correct |
85 ms |
39880 KB |
Output is correct |
26 |
Correct |
312 ms |
50652 KB |
Output is correct |
27 |
Correct |
255 ms |
48468 KB |
Output is correct |
28 |
Correct |
20 ms |
28752 KB |
Output is correct |
29 |
Correct |
335 ms |
48676 KB |
Output is correct |
30 |
Correct |
335 ms |
46928 KB |
Output is correct |
31 |
Correct |
347 ms |
43552 KB |
Output is correct |
32 |
Correct |
91 ms |
32400 KB |
Output is correct |
33 |
Correct |
283 ms |
44520 KB |
Output is correct |