#include <bits/stdc++.h>
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
//#define endl '\n'
class UFDS {
public:
vi p, rank;
vector<ii> mn; int count = 0;
UFDS(int n, vi depth) {
p.resize(n); rank.resize(n); mn.resize(n);
for_(i, 0, n) {
p[i] = i;
mn[i] = {depth[i], i};
}
count = n;
}
int getParent(int i) {
return (p[i] == i) ? i : (p[i] = getParent(p[i]));
}
void join(int i, int j) {
int a = getParent(i), b = getParent(j);
if (a == b) return;
count -= 1;
mn[a] = mn[b] = min(mn[a], mn[b]);
if (rank[a] > rank[b]) {
p[b] = a;
} else {
p[a] = b;
if (rank[a] == rank[b]) rank[b] += 1;
}
}
};
int main() {
#ifdef mlocal
freopen("test.in", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m; cin >> n >> m;
vector<vector<ii>> adj(n);
vector<ii> edges;
vector<bool> used(m);
for_(i, 0, m) {
int a, b; cin >> a >> b;
edges.push_back({a-1, b-1});
}
for_(i, 0, n-1) {
int k; cin >> k;
k -= 1;
used[k] = true;
adj[edges[k][0]].push_back({edges[k][1], k});
adj[edges[k][1]].push_back({edges[k][0], k});
}
vi par(n, -1), pedge(n, -1), depth(n);
function<void(int)> dfs = [&] (int p) {
for (auto &i: adj[p]) if (i[0] != par[p]) {
par[i[0]] = p;
pedge[i[0]] = i[1];
depth[i[0]] = depth[p]+1;
dfs(i[0]);
}
};
dfs(0);
vi val(m, -1);
int pt = 1;
UFDS ufds(n, depth);
for_(i, 0, m) {
if (!used[i]) {
// cout << "huh edge " << i << " was not used " << endl;
int a = ufds.mn[ufds.getParent(edges[i][0])][1], b = ufds.mn[ufds.getParent(edges[i][1])][1];
vi curr;
while (a != b) {
assert(a == ufds.mn[ufds.getParent(a)][1] and b == ufds.mn[ufds.getParent(b)][1]);
if (depth[a] < depth[b]) swap(a, b);
ufds.join(a, par[a]);
// cout << "hence removing edge " << pedge[a] << endl;
curr.push_back(pedge[a]);
a = ufds.mn[ufds.getParent(par[a])][1];
}
sort(curr.begin(), curr.end());
for (auto j: curr) if (val[j] == -1) val[j] = pt++;
val[i] = pt++;
} else if (val[i] == -1) val[i] = pt++;
}
for (auto i: val) cout << i << " ";
cout << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
316 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
12664 KB |
Output is correct |
2 |
Correct |
114 ms |
18352 KB |
Output is correct |
3 |
Correct |
96 ms |
10212 KB |
Output is correct |
4 |
Correct |
158 ms |
35576 KB |
Output is correct |
5 |
Correct |
182 ms |
36924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
22388 KB |
Output is correct |
2 |
Correct |
55 ms |
9848 KB |
Output is correct |
3 |
Correct |
29 ms |
5192 KB |
Output is correct |
4 |
Correct |
68 ms |
16652 KB |
Output is correct |
5 |
Correct |
26 ms |
6860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
257 ms |
42512 KB |
Output is correct |
2 |
Correct |
292 ms |
49872 KB |
Output is correct |
3 |
Correct |
62 ms |
14168 KB |
Output is correct |
4 |
Correct |
128 ms |
21136 KB |
Output is correct |
5 |
Correct |
341 ms |
52220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
186 ms |
29604 KB |
Output is correct |
2 |
Correct |
106 ms |
20596 KB |
Output is correct |
3 |
Correct |
306 ms |
44352 KB |
Output is correct |
4 |
Correct |
298 ms |
41280 KB |
Output is correct |
5 |
Correct |
19 ms |
3780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
316 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
59 ms |
12664 KB |
Output is correct |
10 |
Correct |
114 ms |
18352 KB |
Output is correct |
11 |
Correct |
96 ms |
10212 KB |
Output is correct |
12 |
Correct |
158 ms |
35576 KB |
Output is correct |
13 |
Correct |
182 ms |
36924 KB |
Output is correct |
14 |
Correct |
112 ms |
22388 KB |
Output is correct |
15 |
Correct |
55 ms |
9848 KB |
Output is correct |
16 |
Correct |
29 ms |
5192 KB |
Output is correct |
17 |
Correct |
68 ms |
16652 KB |
Output is correct |
18 |
Correct |
26 ms |
6860 KB |
Output is correct |
19 |
Correct |
257 ms |
42512 KB |
Output is correct |
20 |
Correct |
292 ms |
49872 KB |
Output is correct |
21 |
Correct |
62 ms |
14168 KB |
Output is correct |
22 |
Correct |
128 ms |
21136 KB |
Output is correct |
23 |
Correct |
341 ms |
52220 KB |
Output is correct |
24 |
Correct |
186 ms |
29604 KB |
Output is correct |
25 |
Correct |
106 ms |
20596 KB |
Output is correct |
26 |
Correct |
306 ms |
44352 KB |
Output is correct |
27 |
Correct |
298 ms |
41280 KB |
Output is correct |
28 |
Correct |
19 ms |
3780 KB |
Output is correct |
29 |
Correct |
348 ms |
40100 KB |
Output is correct |
30 |
Correct |
357 ms |
40748 KB |
Output is correct |
31 |
Correct |
342 ms |
37688 KB |
Output is correct |
32 |
Correct |
92 ms |
10476 KB |
Output is correct |
33 |
Correct |
322 ms |
38680 KB |
Output is correct |