#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())
const int N = 3e5 + 10;
vector <pair <int, int>> ed[N];
int p[N][19], in[N], ou[N], timer = 1, id[N][19], cc = 0, a[N], b[N], used[N], dep[N];
struct kahn{
vector <int> ed[N * 20];
int col[N * 20], cc = 1, e;
void add(int a, int b) {
if (a == 0)
return;
ed[b].pb(a);
}
vector <int> lst;
void dfs(int u, int is) {
col[u] = -2;
for (auto x : ed[u]) {
if (col[x] == -1) {
dfs(x, 1);
}
}
if (u <= e && is) {
lst.pb(u);
}
}
void process(int n) {
e = n;
mem(col, -1);
for (int i = 1; i <= n; i++) {
if (col[i] == -1)
dfs(i, 0);
sort(all(lst));
for (auto x : lst) {
col[x] = cc++;
}
if (col[i] < 0)
col[i] = cc++;
cout << col[i] << ' ';
lst.clear();
}
cout << '\n';
}
} E;
void dfs(int u, int v) {
dep[u] = dep[v] + 1;
p[u][0] = v;
for (int j = 1; j < 19; 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 = 18; j >= 0; j--) {
if (is(p[a][j], b) == 0)
a = p[a][j];
}
return p[a][0];
}
int find(int a, int k) {
for (int i = 0; i < 19; i++) {
if (k & (1 << i)) {
a = p[a][i];
}
}
return a;
}
void add(int a, int c, int num) {
int len = dep[a] - dep[c] + 1;
int t = log2(len);
E.add(id[a][t], num);
E.add(id[find(a, len - (1 << t))][t], num);
}
void path(int a, int b, int num) {
int c = lca(a, b);
add(a, c, num);
add(b, c, num);
}
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 |
41 ms |
182264 KB |
Output is correct |
2 |
Correct |
30 ms |
182360 KB |
Output is correct |
3 |
Correct |
26 ms |
182288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
182264 KB |
Output is correct |
2 |
Correct |
30 ms |
182360 KB |
Output is correct |
3 |
Correct |
26 ms |
182288 KB |
Output is correct |
4 |
Correct |
45 ms |
182872 KB |
Output is correct |
5 |
Incorrect |
40 ms |
182872 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
136 ms |
254504 KB |
Output is correct |
2 |
Runtime error |
182 ms |
262144 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
177 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
242 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
225 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
182264 KB |
Output is correct |
2 |
Correct |
30 ms |
182360 KB |
Output is correct |
3 |
Correct |
26 ms |
182288 KB |
Output is correct |
4 |
Correct |
45 ms |
182872 KB |
Output is correct |
5 |
Incorrect |
40 ms |
182872 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |