#include <bits/stdc++.h>
#define int int64_t
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
using ar = array<int, 2>;
const int64_t INF = 1ll << 60;
const int N = 3e5 + 5;
namespace dsu {
int dpr[N];
int gpr(int x) {
return dpr[x] == x ? x : dpr[x] = gpr(dpr[x]);
}
void merge(int u, int v) {
dpr[gpr(u)] = gpr(v);
}
}
int st[N], fn[N], T, pr[N], up[N], mn[N], W[N], n, m;
ar E[N];
bool ok[N];
vector<int> ad[N];
vector<ar> adj[N];
bool is_p(int p, int v) {
return st[p] <= st[v] && fn[v] <= fn[p];
}
void dfs(int v) {
st[v] = T++;
for (auto [u, id]: adj[v]) if (u != pr[v]) {
up[u] = id;
pr[u] = v;
dfs(u);
}
fn[v] = T;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
FOR(i, 0, m) {
cin >> E[i][0] >> E[i][1];
--E[i][0], --E[i][1];
}
FOR(i, 1, n) {
int x; cin >> x; --x;
auto [u, v] = E[x];
adj[v].push_back({u, x});
adj[u].push_back({v, x});
ok[x] = true;
}
fill(mn, mn + m, m);
iota(dsu::dpr, dsu::dpr + n, 0);
up[0] = -1;
dfs(0);
FOR(i, 0, m) if (!ok[i]) {
auto [u, v] = E[i];
int tu = dsu::gpr(u), tv = dsu::gpr(v);
while (!is_p(tu, v)) {
assert(up[tu] != -1);
mn[up[tu]] = i;
dsu::merge(tu, pr[tu]);
tu = dsu::gpr(tu);
}
while (!is_p(tv, u)) {
assert(up[tv] != -1);
mn[up[tv]] = i;
dsu::merge(tv, pr[tv]);
tv = dsu::gpr(tv);
}
}
for (int i = m - 1; i >= 0; --i) if (ok[i])
ad[mn[i]].push_back(i);
int r = m;
for (int i = m - 1; i >= 0; --i) {
if (!ok[i]) {
W[i] = r--;
for (auto t: ad[i]) if (t > i)
W[t] = r--;
}
else if (mn[i] > i)
W[i] = r--;
}
FOR(i, 0, m)
cout << W[i] << ' ';
cout << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
25180 KB |
Output is correct |
2 |
Correct |
7 ms |
25180 KB |
Output is correct |
3 |
Correct |
6 ms |
25180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
25180 KB |
Output is correct |
2 |
Correct |
7 ms |
25180 KB |
Output is correct |
3 |
Correct |
6 ms |
25180 KB |
Output is correct |
4 |
Correct |
6 ms |
25432 KB |
Output is correct |
5 |
Correct |
6 ms |
25180 KB |
Output is correct |
6 |
Correct |
7 ms |
25180 KB |
Output is correct |
7 |
Correct |
7 ms |
25248 KB |
Output is correct |
8 |
Correct |
7 ms |
25432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
37344 KB |
Output is correct |
2 |
Correct |
75 ms |
44736 KB |
Output is correct |
3 |
Correct |
65 ms |
37836 KB |
Output is correct |
4 |
Correct |
98 ms |
58032 KB |
Output is correct |
5 |
Correct |
107 ms |
60080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
44160 KB |
Output is correct |
2 |
Correct |
37 ms |
34132 KB |
Output is correct |
3 |
Correct |
23 ms |
31832 KB |
Output is correct |
4 |
Correct |
43 ms |
39112 KB |
Output is correct |
5 |
Correct |
20 ms |
33368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
165 ms |
57284 KB |
Output is correct |
2 |
Correct |
187 ms |
61892 KB |
Output is correct |
3 |
Correct |
44 ms |
37848 KB |
Output is correct |
4 |
Correct |
63 ms |
41924 KB |
Output is correct |
5 |
Correct |
245 ms |
65028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
132 ms |
49672 KB |
Output is correct |
2 |
Correct |
78 ms |
42236 KB |
Output is correct |
3 |
Correct |
264 ms |
59340 KB |
Output is correct |
4 |
Correct |
285 ms |
57864 KB |
Output is correct |
5 |
Correct |
18 ms |
31608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
25180 KB |
Output is correct |
2 |
Correct |
7 ms |
25180 KB |
Output is correct |
3 |
Correct |
6 ms |
25180 KB |
Output is correct |
4 |
Correct |
6 ms |
25432 KB |
Output is correct |
5 |
Correct |
6 ms |
25180 KB |
Output is correct |
6 |
Correct |
7 ms |
25180 KB |
Output is correct |
7 |
Correct |
7 ms |
25248 KB |
Output is correct |
8 |
Correct |
7 ms |
25432 KB |
Output is correct |
9 |
Correct |
41 ms |
37344 KB |
Output is correct |
10 |
Correct |
75 ms |
44736 KB |
Output is correct |
11 |
Correct |
65 ms |
37836 KB |
Output is correct |
12 |
Correct |
98 ms |
58032 KB |
Output is correct |
13 |
Correct |
107 ms |
60080 KB |
Output is correct |
14 |
Correct |
56 ms |
44160 KB |
Output is correct |
15 |
Correct |
37 ms |
34132 KB |
Output is correct |
16 |
Correct |
23 ms |
31832 KB |
Output is correct |
17 |
Correct |
43 ms |
39112 KB |
Output is correct |
18 |
Correct |
20 ms |
33368 KB |
Output is correct |
19 |
Correct |
165 ms |
57284 KB |
Output is correct |
20 |
Correct |
187 ms |
61892 KB |
Output is correct |
21 |
Correct |
44 ms |
37848 KB |
Output is correct |
22 |
Correct |
63 ms |
41924 KB |
Output is correct |
23 |
Correct |
245 ms |
65028 KB |
Output is correct |
24 |
Correct |
132 ms |
49672 KB |
Output is correct |
25 |
Correct |
78 ms |
42236 KB |
Output is correct |
26 |
Correct |
264 ms |
59340 KB |
Output is correct |
27 |
Correct |
285 ms |
57864 KB |
Output is correct |
28 |
Correct |
18 ms |
31608 KB |
Output is correct |
29 |
Correct |
236 ms |
60624 KB |
Output is correct |
30 |
Correct |
281 ms |
61200 KB |
Output is correct |
31 |
Correct |
247 ms |
60156 KB |
Output is correct |
32 |
Correct |
59 ms |
37896 KB |
Output is correct |
33 |
Correct |
249 ms |
60260 KB |
Output is correct |