#include <bits/stdc++.h>
using namespace std;
class DSU{
private:
vector<int> p;
public:
void init(int sz){
p.resize(sz + 1);
iota(p.begin(), p.end(), 0);
}
int findR(int x){
return p[x] == x ? x : p[x] = findR(p[x]);
}
bool same(int a, int b){
return findR(a) == findR(b);
}
void join(int a, int b){
p[findR(a)] = findR(b);
}
};
struct Edge{
int u, v;
};
int N, E;
vector<Edge> edge;
vector<int> R, W;
bitset<300010> on;
DSU s;
int par[300010], d[300010], idx[300010];
vector<pair<int, int> > adj[300010];
set<int> q;
void dfs(int u, int p){
for(auto &k : adj[u]){
int v, id;
tie(v, id) = k;
if(v == p) continue;
d[v] = d[u] + 1;
par[v] = u, idx[v] = id;
dfs(v, u);
}
}
void query(int a, int b){
while(a != b){
if(d[a] < d[b]) swap(a, b);
while(d[a] > d[b]){
if(!s.same(a, par[a])){
s.join(a, par[a]);
q.insert(idx[a]);
a = par[a];
} else a = s.findR(a);
}
if(d[a] == d[b] && a != b){
if(!s.same(a, par[a])){
s.join(a, par[a]);
q.insert(idx[a]);
a = par[a];
} else a = s.findR(a);
swap(a, b);
if(!s.same(a, par[a])){
s.join(a, par[a]);
q.insert(idx[a]);
a = par[a];
} else a = s.findR(a);
}
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> N >> E;
edge.resize(E); W.assign(E, 0); s.init(N);
for(int i = 0; i < E; i++){
cin >> edge[i].u >> edge[i].v;
}
R.resize(N - 1);
for(int i = 0; i < N - 1; i++){
cin >> R[i], --R[i];
on[R[i]] = true;
adj[edge[R[i]].u].emplace_back(edge[R[i]].v, R[i]);
adj[edge[R[i]].v].emplace_back(edge[R[i]].u, R[i]);
}
dfs(1, 1);
int wg = 1;
for(int i = 0; i < E; i++){
if(!W[i]){
if(on[i]){
if(d[edge[i].u] < d[edge[i].v]) swap(edge[i].u, edge[i].v);
s.join(edge[i].u, edge[i].v);
W[i] = wg++;
} else {
query(edge[i].u, edge[i].v);
for(auto &k : q){
W[k] = wg++;
}
q.clear();
W[i] = wg++;
}
}
cout << W[i] << " \n"[i == E - 1];
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
8 ms |
7552 KB |
Output is correct |
3 |
Correct |
9 ms |
7552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
8 ms |
7552 KB |
Output is correct |
3 |
Correct |
9 ms |
7552 KB |
Output is correct |
4 |
Correct |
10 ms |
7552 KB |
Output is correct |
5 |
Correct |
9 ms |
7552 KB |
Output is correct |
6 |
Correct |
9 ms |
7552 KB |
Output is correct |
7 |
Correct |
10 ms |
7424 KB |
Output is correct |
8 |
Correct |
9 ms |
7552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
16500 KB |
Output is correct |
2 |
Correct |
101 ms |
21120 KB |
Output is correct |
3 |
Correct |
98 ms |
16632 KB |
Output is correct |
4 |
Correct |
159 ms |
32588 KB |
Output is correct |
5 |
Correct |
174 ms |
33760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
131 ms |
25208 KB |
Output is correct |
2 |
Correct |
69 ms |
15356 KB |
Output is correct |
3 |
Correct |
36 ms |
11512 KB |
Output is correct |
4 |
Correct |
89 ms |
20600 KB |
Output is correct |
5 |
Correct |
38 ms |
12536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
271 ms |
42104 KB |
Output is correct |
2 |
Correct |
307 ms |
49644 KB |
Output is correct |
3 |
Correct |
73 ms |
19064 KB |
Output is correct |
4 |
Correct |
118 ms |
22296 KB |
Output is correct |
5 |
Correct |
333 ms |
48120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
190 ms |
30328 KB |
Output is correct |
2 |
Correct |
104 ms |
21752 KB |
Output is correct |
3 |
Correct |
316 ms |
38136 KB |
Output is correct |
4 |
Correct |
299 ms |
36204 KB |
Output is correct |
5 |
Correct |
27 ms |
9984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
8 ms |
7552 KB |
Output is correct |
3 |
Correct |
9 ms |
7552 KB |
Output is correct |
4 |
Correct |
10 ms |
7552 KB |
Output is correct |
5 |
Correct |
9 ms |
7552 KB |
Output is correct |
6 |
Correct |
9 ms |
7552 KB |
Output is correct |
7 |
Correct |
10 ms |
7424 KB |
Output is correct |
8 |
Correct |
9 ms |
7552 KB |
Output is correct |
9 |
Correct |
61 ms |
16500 KB |
Output is correct |
10 |
Correct |
101 ms |
21120 KB |
Output is correct |
11 |
Correct |
98 ms |
16632 KB |
Output is correct |
12 |
Correct |
159 ms |
32588 KB |
Output is correct |
13 |
Correct |
174 ms |
33760 KB |
Output is correct |
14 |
Correct |
131 ms |
25208 KB |
Output is correct |
15 |
Correct |
69 ms |
15356 KB |
Output is correct |
16 |
Correct |
36 ms |
11512 KB |
Output is correct |
17 |
Correct |
89 ms |
20600 KB |
Output is correct |
18 |
Correct |
38 ms |
12536 KB |
Output is correct |
19 |
Correct |
271 ms |
42104 KB |
Output is correct |
20 |
Correct |
307 ms |
49644 KB |
Output is correct |
21 |
Correct |
73 ms |
19064 KB |
Output is correct |
22 |
Correct |
118 ms |
22296 KB |
Output is correct |
23 |
Correct |
333 ms |
48120 KB |
Output is correct |
24 |
Correct |
190 ms |
30328 KB |
Output is correct |
25 |
Correct |
104 ms |
21752 KB |
Output is correct |
26 |
Correct |
316 ms |
38136 KB |
Output is correct |
27 |
Correct |
299 ms |
36204 KB |
Output is correct |
28 |
Correct |
27 ms |
9984 KB |
Output is correct |
29 |
Correct |
338 ms |
37752 KB |
Output is correct |
30 |
Correct |
371 ms |
40952 KB |
Output is correct |
31 |
Correct |
341 ms |
36344 KB |
Output is correct |
32 |
Correct |
96 ms |
16888 KB |
Output is correct |
33 |
Correct |
327 ms |
36216 KB |
Output is correct |