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;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
const int MOD = 1e9+7;
#define F first
#define S second
#define PB push_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
void FASTIO() {ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); }
const int MXN = 1e5+5;
int n, m, k;
vector<pii> adj[MXN];
int order[MXN]; int counter = 0;
int H[MXN], lift[18][MXN];
int A[MXN];
vi ans;
void postorder(int u, int p) {
for(auto e : adj[u]) if(e.F != p) {
postorder(e.F, u);
}
order[u] = counter++;
}
void build_lift(int u, int p) {
H[u] = H[p]+1;
lift[0][u] = p;
for(auto e : adj[u]) if(e.F != p) {
build_lift(e.F, u);
}
}
int get_lca(int a, int b) {
if(H[a] < H[b]) swap(a, b);
// lift a to match b height
for(int p = 17; p >= 0; p--)
while(H[a]-(1<<p) >= H[b]) {
a = lift[p][a];
}
if(a == b) return a;
for(int p = 17; p >= 0; p--) {
while(lift[p][a] != lift[p][b]) {
a = lift[p][a];
b = lift[p][b];
}
}
return lift[0][a];
}
int dfs(int u, int p) {
int sum = A[u];
for(auto e : adj[u]) if(e.F != p) {
int subtree = dfs(e.F, u);
if(subtree >= k) {
ans.PB(e.S);
}
sum += subtree;
}
return sum;
}
int main() {
FASTIO();
cin >> n >> m >> k;
for(int i = 1; i < n; i++) {
int a, b; cin >> a >> b;
adj[a].PB({b, i});
adj[b].PB({a, i});
}
postorder(1, 1);
build_lift(1, 1);
for(int p = 1; p < 18; p++) {
for(int i = 1; i <= n; i++) {
lift[p][i] = lift[p-1][lift[p-1][i]];
}
}
for(int i = 0; i < m; i++) {
int sz; cin >> sz;
vi comp(sz);
for(int j = 0; j < sz; j++) cin >> comp[j];
sort(all(comp), [&](int a, int b) {
return order[a] < order[b];
});
for(int j = 0; j < sz; j++) A[comp[j]]++;
for(int j = 0; j < sz; j++) {
int lca = get_lca(comp[j], comp[(j+1)%sz]);
A[lca]--;
}
}
dfs(1, -1);
cout << ans.size() << "\n";
for(int x : ans) cout << x << " ";
cout << "\n";
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... |