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], in[MAXN], out[MAXN], order[MAXN];
int tempo;
void dfs_lca(int u, int p) {
in[u] = ++tempo;
order[tempo] = u;
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);
}
out[u] = tempo;
}
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;
sort(xs.begin(), xs.end(), [&](int a, int b) { return in[a] < in[b]; });
vector<int> lcas;
for (int i = 0; i < s-1; ++i)
lcas.push_back(lca(xs[i], xs[i + 1]));
sort(lcas.begin(), lcas.end(), [&](int a, int b) { return prof[a] > prof[b]; });
lcas.erase(unique(lcas.begin(), lcas.end()), lcas.end());
set<int> atual;
for (int x: xs)
atual.insert(in[x]);
for (int u: lcas) {
auto beg = atual.lower_bound(in[u]);
auto ed = atual.upper_bound(out[u]);
vector<int> tmp;
while (beg != ed) {
++sub[order[*beg]];
--sub[u];
tmp.push_back(*beg);
++beg;
}
for (int v: tmp)
atual.erase(v);
atual.insert(in[u]);
}
}
dfs_sub(1, 1);
vector<int> ans;
for (int i = 1; i <= n; ++i)
if (sub[i] >= k) ans.push_back(idx[i]);
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... |