#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}
const int N = 300003;
struct UFDS {
int p[N],siz[N];
void init(int n){
for(int i = 0; i <= n; i++){
p[i] = i;
siz[i] = 1;
}
}
int get(int x){
return p[x]=(x==p[x] ? x : get(p[x]));
}
void unio(int u , int v){
int a = get(u);
int b=get(v);
if(a==b)return;
//if(siz[a]>siz[b])swap(a,b);
p[b]=a;
siz[a]+=siz[b];
}
};
const int l = 25;
vector<pair<int,int>> adj[N];
vector<pair<int,int>> vp(N);
int idx[N];
int timer;
int tin[N], tout[N];
int up[N][l + 5];
int ans[N];
int cur;
int there[N];
UFDS d;
void dfs(int v, int p){
tin[v] = ++timer;
up[v][0] = p;
for (int i = 1; i <= l; ++i)
up[v][i] = up[up[v][i-1]][i-1];
for (auto &[u , id] : adj[v]) {
if (u != p){
dfs(u, v);
idx[u] = id;
}
}
tout[v] = ++timer;
}
bool is_ancestor(int u, int v){
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v){
if (is_ancestor(u, v))
return u;
if (is_ancestor(v, u))
return v;
for (int i = l; i >= 0; --i) {
if (!is_ancestor(up[u][i], v))
u = up[u][i];
}
return up[u][0];
}
void help(int x , int y){
int lc = lca(x,y);
int u = d.get(x) , v = d.get(y);
vector<int> ed;
while(u != lc && is_ancestor(lc , u) == lc){
ed.pb(idx[u]);
d.unio(d.get(up[u][0]) , u);
u = d.get(u);
}
while(v != lc && is_ancestor(lc , v) == lc){
ed.pb(idx[v]);
d.unio(d.get(up[v][0]) , v);
v = d.get(v);
}
sv(ed);
for(int i : ed){
if(ans[i] == 0)ans[i] = cur++;
}
}
void solve(){
int n , m;
cin >> n >> m;
for(int i = 0; i < m; i++){
int a,b;
cin >> a >> b;
a--;
b--;
vp[i] = mp(a,b);
}
d.init(n + 2);
memset(there , 0, sizeof there);
for(int i = 1; i < n; i++){
int j;
cin >> j;
j--;
there[j] = 1;
adj[vp[j].vf].pb(mp(vp[j].vs , j));
adj[vp[j].vs].pb(mp(vp[j].vf , j));
}
timer = 0;
dfs(1,1);
memset(ans,0,sizeof ans);
cur = 1;
for(int i = 0; i < m; i++){
if(ans[i] != 0)continue;
if(there[i] == 1){
ans[i] = cur++;
}
else {
help(vp[i].vf , vp[i].vs);
ans[i] = cur++;
}
}
for(int i = 0; i < m; i++){
cout << ans[i] << ' ';
}
cout << '\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;cin >> t;while(t--)
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12056 KB |
Output is correct |
2 |
Correct |
8 ms |
11980 KB |
Output is correct |
3 |
Incorrect |
6 ms |
12028 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12056 KB |
Output is correct |
2 |
Correct |
8 ms |
11980 KB |
Output is correct |
3 |
Incorrect |
6 ms |
12028 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
56 ms |
29908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
83 ms |
36944 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
301 ms |
63092 KB |
Output is correct |
2 |
Correct |
296 ms |
70248 KB |
Output is correct |
3 |
Correct |
64 ms |
28608 KB |
Output is correct |
4 |
Correct |
97 ms |
36920 KB |
Output is correct |
5 |
Correct |
347 ms |
74740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
214 ms |
47868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12056 KB |
Output is correct |
2 |
Correct |
8 ms |
11980 KB |
Output is correct |
3 |
Incorrect |
6 ms |
12028 KB |
Output isn't correct |