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 mx = 1e5+5, ml = 17;
int n, m, k, tin[mx], cnt, up[ml][mx], depth[mx], weight[mx];
vector<pair<int, int>> adj[mx]; vector<int> ans;
void dfs(int node){
tin[node] = cnt++;
for (int l = 1; l < ml; l++) up[l][node] = up[l-1][up[l-1][node]];
for (pair<int, int> nxt : adj[node])
if (nxt.first != up[0][node])
depth[nxt.first] = depth[node]+1, up[0][nxt.first] = node, dfs(nxt.first);
}
int jmp(int node, int d){
for (int l = 0; l < ml; l++)
if ((d>>l)%2 == 1)
node = up[l][node];
return node;
}
int getLCA(int a, int b){
if (depth[a] < depth[b]) swap(a, b);
a = jmp(a, depth[a]-depth[b]); if (a == b) return a;
for (int l = ml-1; l >= 0; l--)
if (up[l][a] != up[l][b])
a = up[l][a], b = up[l][b];
return up[0][a];
}
int solve(int node){
int ret = weight[node];
for (pair<int, int> nxt : adj[node])
if (nxt.first != up[0][node]){
int val = solve(nxt.first); ret += val;
if (val >= k) ans.push_back(nxt.second);
}
return ret;
}
int main() {
cin >> n >> m >> k;
for (int i = 1; i < n; i++){
int a, b; cin >> a >> b;
adj[a].push_back({b, i}); adj[b].push_back({a, i});
}dfs(1);
for (int i = 0; i < m; i++){
int s; cin >> s; int cn[s];
for (int j = 0; j < s; j++) cin >> cn[j];
sort(cn, cn+s, [](int a, int b) { return tin[a] < tin[b]; } );
for (int j = 0; j < s-1; j++){
int lca = getLCA(cn[j], cn[j]+1);
weight[lca]--; weight[cn[j+1]]++;
}weight[getLCA(cn[0], cn[1])]--; weight[cn[0]]++;
}solve(1); sort(ans.begin(), ans.end());
cout<<ans.size()<<endl; for (int res : ans) cout<<res<<" ";
}
| # | 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... |