#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
const int maxn=3e5+9;
vector<pii>a[maxn];
int st[maxn][21],d[maxn];
pii pr[maxn];
void dfs(int u,int par){
for (auto v:a[u]){
if (v.fi==par)continue;
pr[v.fi]={u,v.se};
st[v.fi][0]=u;
for1(i,1,20)st[v.fi][i]=st[st[v.fi][i-1]][i-1];
d[v.fi]=d[u]+1;
dfs(v.fi,u);
}
}
pii b[maxn];
int lca(int u,int v){
if (d[u]<d[v])swap(u,v);
int k=d[u]-d[v];
for1(i,0,20)if (k>>i&1)u=st[u][i];
if (u==v)return u;
for2(i,20,0){
if (!st[u][i]||!st[v][i])continue;
if (st[u][i]!=st[v][i]){
u=st[u][i];
v=st[v][i];
}
}
return st[u][0];
}
bool use[maxn];
int ans[maxn];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
int n,m;
cin>>n>>m;
for1(i,1,m)cin>>b[i].fi>>b[i].se;
for1(i,1,n-1){
int id;
cin>>id;
use[id]=1;
a[b[id].fi].pb({b[id].se,id});
a[b[id].se].pb({b[id].fi,id});
}
dfs(1,0);
vector<int>perm;
auto take=[&](int id){
int u=b[id].fi,v=b[id].se,t=lca(u,v),tu=u,tv=v;
vector<int>xd;
vector<int>vertice;
while (true){
if (!tu)break;
if (lca(tu,t)==tu)break;
vertice.pb(tu);
//cerr<<tu<<'\n';
tu=pr[tu].fi;
}
while (true){
if (!tv)break;
if (lca(tv,t)==tv)break;
vertice.pb(tv);
//cerr<<tv<<'\n';
tv=pr[tv].fi;
}
for (auto v1:vertice){
if (pr[v1].se)xd.pb(pr[v1].se);
pr[v1]={0,0};
}
if (u!=tu)pr[u]={tu,0};
if (v!=tv)pr[v]={tv,0};
sort(all(xd));
for (auto v1:xd)perm.pb(v1);
if (!use[id])perm.pb(id);
};
for1(i,1,m){
take(i);
}
int cnt=0;
for (auto v:perm)ans[v]=++cnt;
for1(i,1,m)cout<<ans[i]<<" ";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14680 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Correct |
3 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14680 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Correct |
3 ms |
14684 KB |
Output is correct |
4 |
Correct |
4 ms |
14696 KB |
Output is correct |
5 |
Incorrect |
4 ms |
14684 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
28712 KB |
Output is correct |
2 |
Correct |
110 ms |
32828 KB |
Output is correct |
3 |
Correct |
100 ms |
20804 KB |
Output is correct |
4 |
Correct |
180 ms |
55492 KB |
Output is correct |
5 |
Correct |
194 ms |
55832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
94 ms |
36552 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
219 ms |
58188 KB |
Output is correct |
2 |
Correct |
217 ms |
64420 KB |
Output is correct |
3 |
Correct |
52 ms |
30364 KB |
Output is correct |
4 |
Correct |
92 ms |
34972 KB |
Output is correct |
5 |
Correct |
273 ms |
65956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
189 ms |
45260 KB |
Output is correct |
2 |
Correct |
100 ms |
35904 KB |
Output is correct |
3 |
Correct |
368 ms |
64272 KB |
Output is correct |
4 |
Correct |
327 ms |
60260 KB |
Output is correct |
5 |
Correct |
20 ms |
18780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14680 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Correct |
3 ms |
14684 KB |
Output is correct |
4 |
Correct |
4 ms |
14696 KB |
Output is correct |
5 |
Incorrect |
4 ms |
14684 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |