#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (ll)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
ll mod = 1e9 + 7;
const int N = 3e5 + 10;
vector <pair <int, int>> ed[N];
int p[N][20], in[N], ou[N], timer = 1, id[N][20], cc = 0, a[N], b[N], used[N];
struct kahn{
vector <int> ed[N * 21];
int col[N * 21], cc = 1, e, used[N * 21];
void add(int a, int b) {
if (a == 0)
return;
ed[b].pb(a);
}
vector <int> lst;
void dfs(int u, int is) {
used[u] = 1;
for (auto x : ed[u]) {
if (used[x] == 0) {
dfs(x, 1);
}
}
if (u <= e && is) {
lst.pb(u);
}
}
void process(int n) {
e = n;
for (int i = 1; i <= n; i++) {
if (used[i] == 0)
dfs(i, 0);
sort(all(lst));
for (auto x : lst) {
col[x] = cc++;
}
lst.clear();
if (col[i] == 0)
col[i] = cc++;
cout << col[i] << ' ';
}
cout << '\n';
}
} E;
void dfs(int u, int v) {
p[u][0] = v;
for (int j = 1; j < 20; j++) {
p[u][j] = p[p[u][j - 1]][j - 1];
id[u][j] = cc++;
E.add(id[u][j - 1], id[u][j]);
E.add(id[p[u][j - 1]][j - 1], id[u][j]);
}
in[u] = timer++;
for (auto x : ed[u]) {
if (x.f == v)
continue;
id[x.f][0] = x.s;
dfs(x.f, u);
}
ou[u] = timer++;
}
bool is(int a, int b) {
return in[a] <= in[b] && ou[a] >= in[b];
}
int lca(int a, int b) {
if (is(a, b))
return a;
if (is(b, a))
return b;
for (int j = 19; j >= 0; j--) {
if (is(p[a][j], b) == 0)
a = p[a][j];
}
return p[a][0];
}
void path(int a, int b, int num) {
int c = lca(a, b);
for (int j = 19; j >= 0; j--) {
if (is(c, p[a][j])) {
E.add(id[a][j], num);
a = p[a][j];
}
if (is(c, p[b][j])) {
E.add(id[b][j], num);
b = p[b][j];
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
in[0] = 0;
ou[0] = 1e9;
int n, e;
cin >> n >> e;
cc = e + 1;
for (int i = 1; i <= e; i++) {
cin >> a[i] >> b[i];
}
for (int i = 1; i < n; i++) {
int x;
cin >> x;
used[x] = 1;
ed[a[x]].pb({b[x], x});
ed[b[x]].pb({a[x], x});
}
dfs(1, 0);
for (int i = 1; i <= e; i++) {
if (used[i] == 0) {
path(a[i], b[i], i);
}
}
E.process(e);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
169296 KB |
Output is correct |
2 |
Correct |
26 ms |
169308 KB |
Output is correct |
3 |
Correct |
27 ms |
169352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
169296 KB |
Output is correct |
2 |
Correct |
26 ms |
169308 KB |
Output is correct |
3 |
Correct |
27 ms |
169352 KB |
Output is correct |
4 |
Correct |
29 ms |
169820 KB |
Output is correct |
5 |
Correct |
28 ms |
169812 KB |
Output is correct |
6 |
Correct |
29 ms |
169888 KB |
Output is correct |
7 |
Correct |
30 ms |
169564 KB |
Output is correct |
8 |
Correct |
27 ms |
169772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
249624 KB |
Output is correct |
2 |
Runtime error |
127 ms |
262144 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
198 ms |
262148 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
198 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
188 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
169296 KB |
Output is correct |
2 |
Correct |
26 ms |
169308 KB |
Output is correct |
3 |
Correct |
27 ms |
169352 KB |
Output is correct |
4 |
Correct |
29 ms |
169820 KB |
Output is correct |
5 |
Correct |
28 ms |
169812 KB |
Output is correct |
6 |
Correct |
29 ms |
169888 KB |
Output is correct |
7 |
Correct |
30 ms |
169564 KB |
Output is correct |
8 |
Correct |
27 ms |
169772 KB |
Output is correct |
9 |
Correct |
127 ms |
249624 KB |
Output is correct |
10 |
Runtime error |
127 ms |
262144 KB |
Execution killed with signal 9 |
11 |
Halted |
0 ms |
0 KB |
- |