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;
const int MAXN = 1e5 + 10;
const int MAXK = 20;
vector<pair<int, int>> graph[MAXN];
int dp[MAXN][MAXK];
int prof[MAXN], sub[MAXN], idx[MAXN];
void dfs_lca(int u, int p) {
for (auto [v, id]: graph[u]) {
if (v == p) continue;
dp[v][0] = u;
for (int k = 1; k < MAXK; ++k)
dp[v][k] = dp[dp[v][k-1]][k-1];
prof[v] = prof[u] + 1;
dfs_lca(v, u);
}
}
int lca(int a, int b) {
if (prof[b] > prof[a]) swap(a, b);
int d = prof[a] - prof[b];
for (int k = 0; k < MAXK; ++k)
if (d & (1 << k)) a = dp[a][k];
if (a == b) return a;
for (int k = MAXK - 1; k >= 0; --k)
if (dp[a][k] != dp[b][k]) {
a = dp[a][k];
b = dp[b][k];
}
return dp[a][0];
}
void dfs_sub(int u, int p) {
for (auto [v, id]: graph[u])
if (v != p) {
idx[v] = id;
dfs_sub(v, u);
sub[u] += sub[v];
}
}
int main() {
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i <= n-1; ++i) {
int u, v;
cin >> u >> v;
graph[u].emplace_back(v, i);
graph[v].emplace_back(u, i);
}
dfs_lca(1, 1);
for (int i = 0; i < m; ++i) {
int s;
cin >> s;
vector<int> xs(s);
for (int &x: xs)
cin >> x;
int pai = xs[0];
for (int x: xs)
pai = lca(pai, x);
sub[pai] -= s;
for (int x: xs)
++sub[x];
}
dfs_sub(1, 1);
vector<int> ans;
for (int i = 1; i <= n; ++i) {
//cerr << sub[i] << " ";
if (sub[i] >= k) ans.push_back(idx[i]);
}
cerr << "\n";
sort(ans.begin(), ans.end());
cout << ans.size() << "\n";
for (int x: ans)
cout << x << " ";
cout << "\n";
}
# | 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... |