This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Author: goats_9 */
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int LOG = 18;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, k;
cin >> n >> m >> k;
vector<ll> depth(n + 1), maj(n + 1), ans(n);
vector<array<int, 2>> edges(n);
vector<vector<array<int, 2>>> adj(n + 1, vector<array<int, 2>>());
vector<array<int, LOG>> par(n + 1);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
edges[i] = {u, v};
adj[u].push_back({v, i});
adj[v].push_back({u, i});
}
function<void(int, int)> dfs = [&] (int u, int p) {
par[u][0] = p;
for (int i = 1; i < LOG; i++) par[u][i] = par[par[u][i - 1]][i - 1];
depth[u] = depth[p] + 1;
for (auto [v, _] : adj[u]) {
if (v == p) continue;
dfs(v, u);
}
};
auto lca = [&] (int u, int v) -> int {
if (depth[u] > depth[v]) swap(u, v);
int d = depth[v] - depth[u];
for (int i = LOG - 1; i >= 0; i--) {
if (d&(1<<i)) v = par[v][i];
}
if (u == v) return u;
for (int i = LOG - 1; i >= 0; i--) {
if (par[u][i] != par[v][i]) {
u = par[u][i];
v = par[v][i];
}
}
return par[u][0];
};
function <void(int, int, int)> dfs2 = [&] (int u, int p, int idx) {
for (auto [v, j] : adj[u]) {
if (v == p) continue;
dfs2(v, u, j);
maj[u] += maj[v];
}
ans[idx] = maj[u];
};
dfs(1, 0);
while (m--) {
int sz;
cin >> sz;
set<array<ll, 2>> st;
while (sz--) {
int x;
cin >> x;
st.insert({-depth[x], x});
}
while (st.size() > 1) {
auto [du, u] = *st.begin();
st.erase(st.begin());
auto [dv, v] = *st.begin();
st.erase(st.begin());
int w = lca(u, v);
++maj[u], ++maj[v];
maj[w] -= 2;
st.insert({-depth[w], w});
}
}
dfs2(1, 0, 0);
vector<int> res;
for (int i = 1; i < n; i++) {
// cerr << ans[i] << ' ';
if (ans[i] >= k) res.push_back(i);
}
cout << res.size() << '\n';
for (auto u : res) cout << u << ' ';
return 0;
}
# | 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... |