This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
using ii = pair<int, int>;
const int MAX_N = 1e5 + 9;
const int MAX_K = 18;
vector<ii> adj[MAX_N];
int up[MAX_K][MAX_N], d[MAX_N];
int in[MAX_N], t = 0;
int idx[MAX_N];
void dfs(int u, int p = 0, int h = 0) {
in[u] = t++, up[0][u] = p, d[u] = h;
for (auto [v, i] : adj[u]) {
if (v != p) {
idx[v] = i;
dfs(v, u, h + 1);
}
}
}
void init(int n) {
for (int i = 0; i < MAX_K - 1; i++) {
for (int j = 1; j <= n; j++) {
up[i + 1][j] = up[i][up[i][j]];
}
}
}
int lca(int a, int b) {
if (d[a] < d[b]) {
swap(a, b);
}
int diff = d[a] - d[b];
for (int i = 0; i < MAX_K; i++) {
if (diff >> i & 1) a = up[i][a];
}
if (a == b) {
return a;
}
for (int i = MAX_K - 1; i >= 0; i--) {
if (up[i][a] != up[i][b]) {
a = up[i][a];
b = up[i][b];
}
}
return up[0][a];
}
int ans[MAX_N], c = 0, k;
void dfs2(int u, int p = 0) {
for (auto [v, i] : adj[u]) {
if (v != p) {
dfs2(v, u);
ans[u] += ans[v];
}
}
if (ans[u] >= 2 * k) c++;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m >> k;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
adj[a].emplace_back(b, i);
adj[b].emplace_back(a, i);
}
dfs(1);
init(n);
for (int i = 0; i < m; i++) {
int s;
cin >> s;
vector<int> a(s);
for (int j = 0; j < s; j++) {
cin >> a[j];
}
sort(all(a), [&](const int &x, const int &y) {
return in[x] < in[y];
});
auto nxt = [&s](int j) {
if (++j == s) j = 0;
return j;
};
for (int j = 0; j < s; j++) {
int x = a[j];
int y = a[nxt(j)];
++ans[x], ++ans[y];
ans[lca(x, y)] -= 2;
}
}
dfs2(1);
cout << c << '\n';
for (int i = 2; i <= n; i++) {
if (ans[i] >= 2 * k) cout << idx[i] << ' ';
}
return 0;
}
Compilation message (stderr)
railway.cpp: In function 'void dfs(int, int, int)':
railway.cpp:19:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
19 | for (auto [v, i] : adj[u]) {
| ^
railway.cpp: In function 'void dfs2(int, int)':
railway.cpp:58:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
58 | for (auto [v, i] : adj[u]) {
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |