#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++)
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
26972 KB |
Output is correct |
2 |
Correct |
4 ms |
26972 KB |
Output is correct |
3 |
Correct |
4 ms |
26972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
26972 KB |
Output is correct |
2 |
Correct |
4 ms |
26972 KB |
Output is correct |
3 |
Correct |
4 ms |
26972 KB |
Output is correct |
4 |
Correct |
4 ms |
26968 KB |
Output is correct |
5 |
Correct |
4 ms |
27128 KB |
Output is correct |
6 |
Correct |
4 ms |
27480 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 |
52 ms |
31612 KB |
Output is correct |
2 |
Correct |
83 ms |
32792 KB |
Output is correct |
3 |
Correct |
65 ms |
29668 KB |
Output is correct |
4 |
Correct |
144 ms |
38780 KB |
Output is correct |
5 |
Correct |
171 ms |
39284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
39552 KB |
Output is correct |
2 |
Correct |
53 ms |
31824 KB |
Output is correct |
3 |
Correct |
29 ms |
29504 KB |
Output is correct |
4 |
Correct |
63 ms |
36420 KB |
Output is correct |
5 |
Correct |
25 ms |
31068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
223 ms |
47508 KB |
Output is correct |
2 |
Correct |
216 ms |
53152 KB |
Output is correct |
3 |
Correct |
54 ms |
34544 KB |
Output is correct |
4 |
Correct |
71 ms |
38120 KB |
Output is correct |
5 |
Correct |
270 ms |
51484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
43192 KB |
Output is correct |
2 |
Correct |
80 ms |
38484 KB |
Output is correct |
3 |
Correct |
309 ms |
47696 KB |
Output is correct |
4 |
Correct |
244 ms |
45468 KB |
Output is correct |
5 |
Correct |
19 ms |
28252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
26972 KB |
Output is correct |
2 |
Correct |
4 ms |
26972 KB |
Output is correct |
3 |
Correct |
4 ms |
26972 KB |
Output is correct |
4 |
Correct |
4 ms |
26968 KB |
Output is correct |
5 |
Correct |
4 ms |
27128 KB |
Output is correct |
6 |
Correct |
4 ms |
27480 KB |
Output is correct |
7 |
Correct |
4 ms |
26972 KB |
Output is correct |
8 |
Correct |
4 ms |
26972 KB |
Output is correct |
9 |
Correct |
52 ms |
31612 KB |
Output is correct |
10 |
Correct |
83 ms |
32792 KB |
Output is correct |
11 |
Correct |
65 ms |
29668 KB |
Output is correct |
12 |
Correct |
144 ms |
38780 KB |
Output is correct |
13 |
Correct |
171 ms |
39284 KB |
Output is correct |
14 |
Correct |
85 ms |
39552 KB |
Output is correct |
15 |
Correct |
53 ms |
31824 KB |
Output is correct |
16 |
Correct |
29 ms |
29504 KB |
Output is correct |
17 |
Correct |
63 ms |
36420 KB |
Output is correct |
18 |
Correct |
25 ms |
31068 KB |
Output is correct |
19 |
Correct |
223 ms |
47508 KB |
Output is correct |
20 |
Correct |
216 ms |
53152 KB |
Output is correct |
21 |
Correct |
54 ms |
34544 KB |
Output is correct |
22 |
Correct |
71 ms |
38120 KB |
Output is correct |
23 |
Correct |
270 ms |
51484 KB |
Output is correct |
24 |
Correct |
143 ms |
43192 KB |
Output is correct |
25 |
Correct |
80 ms |
38484 KB |
Output is correct |
26 |
Correct |
309 ms |
47696 KB |
Output is correct |
27 |
Correct |
244 ms |
45468 KB |
Output is correct |
28 |
Correct |
19 ms |
28252 KB |
Output is correct |
29 |
Correct |
282 ms |
45604 KB |
Output is correct |
30 |
Correct |
292 ms |
43548 KB |
Output is correct |
31 |
Correct |
258 ms |
40244 KB |
Output is correct |
32 |
Correct |
90 ms |
30432 KB |
Output is correct |
33 |
Correct |
278 ms |
41724 KB |
Output is correct |