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;
using ll = long long;
const int lg = 17;
int main() {
cin.tie(0)->sync_with_stdio(false);
#ifdef sunnatov
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, m, k;
cin >> n >> m >> k;
vector<vector<int>> g(n + 1), jump(n + 1, vector<int>(lg));
map<pair<int, int>, int> mp;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
mp[make_pair(min(u, v), max(u, v))] = i;
}
vector<int> tin(n + 1), tout(n + 1), par(n + 1);
int z = 0;
auto dfs = [&](auto &dfs, int u) -> void {
if (par[u] != 0) {
for (int i = 1; i < lg; i++) {
jump[i][u] = jump[i - 1][jump[i - 1][u]];
if (jump[i][u] == 0) break;
}
}
tin[u] = z++;
for (int v: g[u]) {
if (jump[0][u] == v) continue;
jump[0][v] = u;
par[v] = u;
dfs(dfs, v);
}
tout[u] = z++;
};
auto isAnc = [&](int u, int v) {
return tin[u] <= tin[v] and tout[v] <= tout[u];
};
auto lca = [&](int u, int v) {
if (isAnc(u, v)) return u;
if (isAnc(v, u)) return v;
for (int i = lg - 1; i >= 0; i--) {
if (jump[i][u] == 0 or isAnc(jump[i][u], v)) continue;
u = jump[i][u];
}
return jump[0][u];
};
dfs(dfs, 1);
vector<int> cnt(n + 1);
while (m--) {
int ln;
cin >> ln;
vector<int> a(ln);
for (int &x: a) cin >> x;
if (ln == 1) continue;
int anc = a[0];
for (int i = 1; i < ln; i++) anc = lca(anc, a[i]);
cnt[anc]++;
for (int i = 0; i < ln; i++) {
bool ok = true;
for (int j = 0; j < ln; j++) {
if (i != j) ok &= !isAnc(a[i], a[j]);
}
if (ok) cnt[a[i]]--;
}
}
// for (int i = 1; i <= n; i++) cout << cnt[i] << ' ';
// cout << endl;
vector<int> ans;
auto dfs2 = [&](auto &dfs2, int u) -> void {
for (int v: g[u]) {
if (par[u] == v) continue;
dfs2(dfs2, v);
cnt[v] += cnt[u];
}
// cout << u << ' ' << cnt[par[u]] << '\n';
if (cnt[par[u]] >= k) ans.emplace_back(mp[make_pair(min(u, par[u]), max(u, par[u]))]);
};
dfs2(dfs2, 1);
sort(ans.begin(), ans.end());
cout << ans.size() << '\n';
for (int x: ans) cout << x << ' ';
}
# | 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... |