#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr ll INFLL = numeric_limits<ll>::max() / 2;
template<class T>
istream &operator>>(istream &is, vector<T> &a) {
for (auto &i : a) {
is >> i;
}
return is;
}
vector<vector<pair<int, int>>> g;
vector<pair<int, int>> par;
vector<int> tin, tout;
void dfs(int v, int p) {
static int T = 0;
tin[v] = T++;
for (auto [u, i] : g[v]) {
if (u != p) {
par[u] = {v, i};
dfs(u, v);
}
}
tout[v] = T;
}
bool is_anc(int u, int v) {
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
struct DSU {
vector<int> p;
DSU(int n) {
p.resize(n);
iota(p.begin(), p.end(), 0);
}
int get(int v) {
if (p[v] == v) {
return v;
}
return p[v] = get(p[v]);
}
void unite(int u, int v) {
u = get(u);
v = get(v);
if (u == v) {
return;
}
p[v] = u;
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
#ifdef __APPLE__
freopen("input.txt", "r", stdin);
#endif
int n, m;
cin >> n >> m;
vector<pair<int, int>> e(m);
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
--u;
--v;
e[i] = {u, v};
}
vector<bool> tr(m);
g.resize(n);
for (int i = 0; i < n - 1; ++i) {
int x;
cin >> x;
--x;
tr[x] = 1;
auto [u, v] = e[x];
g[u].push_back({v, x});
g[v].push_back({u, x});
}
par.resize(n, {-1, -1});
tin.resize(n);
tout.resize(n);
dfs(0, -1);
int ind = 1;
vector<int> ans(m, -1);
DSU t(n);
for (int i = 0; i < m; ++i) {
auto [u, v] = e[i];
if (tr[i]) {
if (ans[i] == -1) {
if (!is_anc(u, v)) {
swap(u, v);
}
t.unite(u, v);
ans[i] = ind++;
}
continue;
}
vector<int> cur;
while (!is_anc(u, v)) {
// if (ans[par[u].second] == -1) {
// cur.push_back(par[u].second);
// }
// u = par[u].first;
int head = t.get(u);
if (is_anc(head, v)) {
break;
}
cur.push_back(par[head].second);
t.unite(par[head].first, head);
u = par[head].first;
}
while (!is_anc(v, u)) {
// if (ans[par[v].second] == -1) {
// cur.push_back(par[v].second);
// }
// v = par[v].first;
int head = t.get(v);
if (is_anc(head, u)) {
break;
}
cur.push_back(par[head].second);
t.unite(par[head].first, head);
v = par[head].first;
}
sort(cur.begin(), cur.end());
for (int j : cur) {
assert(ans[j] == -1);
ans[j] = ind++;
}
assert(ans[i] == -1);
ans[i] = ind++;
}
for (int i : ans) {
cout << i << " ";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
10156 KB |
Output is correct |
2 |
Correct |
77 ms |
14192 KB |
Output is correct |
3 |
Correct |
73 ms |
6928 KB |
Output is correct |
4 |
Correct |
112 ms |
27588 KB |
Output is correct |
5 |
Correct |
124 ms |
29156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
15772 KB |
Output is correct |
2 |
Correct |
41 ms |
8472 KB |
Output is correct |
3 |
Correct |
21 ms |
4552 KB |
Output is correct |
4 |
Correct |
50 ms |
13480 KB |
Output is correct |
5 |
Correct |
19 ms |
5632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
177 ms |
29396 KB |
Output is correct |
2 |
Correct |
203 ms |
34084 KB |
Output is correct |
3 |
Correct |
39 ms |
9868 KB |
Output is correct |
4 |
Correct |
59 ms |
14648 KB |
Output is correct |
5 |
Correct |
243 ms |
35736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
138 ms |
20756 KB |
Output is correct |
2 |
Correct |
70 ms |
14724 KB |
Output is correct |
3 |
Correct |
261 ms |
32044 KB |
Output is correct |
4 |
Correct |
257 ms |
29788 KB |
Output is correct |
5 |
Correct |
15 ms |
2900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
43 ms |
10156 KB |
Output is correct |
10 |
Correct |
77 ms |
14192 KB |
Output is correct |
11 |
Correct |
73 ms |
6928 KB |
Output is correct |
12 |
Correct |
112 ms |
27588 KB |
Output is correct |
13 |
Correct |
124 ms |
29156 KB |
Output is correct |
14 |
Correct |
67 ms |
15772 KB |
Output is correct |
15 |
Correct |
41 ms |
8472 KB |
Output is correct |
16 |
Correct |
21 ms |
4552 KB |
Output is correct |
17 |
Correct |
50 ms |
13480 KB |
Output is correct |
18 |
Correct |
19 ms |
5632 KB |
Output is correct |
19 |
Correct |
177 ms |
29396 KB |
Output is correct |
20 |
Correct |
203 ms |
34084 KB |
Output is correct |
21 |
Correct |
39 ms |
9868 KB |
Output is correct |
22 |
Correct |
59 ms |
14648 KB |
Output is correct |
23 |
Correct |
243 ms |
35736 KB |
Output is correct |
24 |
Correct |
138 ms |
20756 KB |
Output is correct |
25 |
Correct |
70 ms |
14724 KB |
Output is correct |
26 |
Correct |
261 ms |
32044 KB |
Output is correct |
27 |
Correct |
257 ms |
29788 KB |
Output is correct |
28 |
Correct |
15 ms |
2900 KB |
Output is correct |
29 |
Correct |
257 ms |
34260 KB |
Output is correct |
30 |
Correct |
266 ms |
34916 KB |
Output is correct |
31 |
Correct |
261 ms |
33832 KB |
Output is correct |
32 |
Correct |
69 ms |
9844 KB |
Output is correct |
33 |
Correct |
248 ms |
34132 KB |
Output is correct |