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;
#define N 100005
#define log 17
#define fi first
#define se second
typedef pair<int, int> ii;
int n, m, k, par[log + 2][N], tin[N], tout[N], tim, dp[N];
vector<ii> adj[N];
vector<int> res;
void dfs(int u, int p){
tin[u] = ++tim;
for (auto x : adj[u]){
int v = x.fi;
if (v == p) continue;
par[0][v] = u;
dfs(v, u);
}
tout[u] = tim;
}
bool in(int u, int v){
return (tin[u] <= tin[v] && tout[v] <= tout[u]);
}
int getlca(int u, int v){
if (in(u, v)) return u;
if (in(v, u)) return v;
for (int k = log; k >= 0; k--){
if (par[k][u] && !in(par[k][u], v)) u = par[k][u];
}
return par[0][u];
}
bool cmp(int&a, int& b){
return tin[a] < tin[b];
}
void dfs2(int u, int p){
for (auto x : adj[u]){
int v = x.fi, idx = x.se;
if (v == p) continue;
dfs2(v, u);
if (dp[v] >= k) res.push_back(idx);
dp[u] += dp[v];
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> m >> k;
for (int i = 1; i <= n - 1; i++){
int u, v;
cin >> u >> v;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
}
dfs(1, 0);
for (int j = 1; j <= log; j++){
for (int i = 1; i <= n; i++) par[j][i] = par[j - 1][par[j - 1][i]];
}
for (int i = 1; i <= m; i++){
int num;
cin >> num;
vector<int> vec;
for (int j = 1; j <= num; j++){
int val;
cin >> val;
vec.push_back(val);
}
sort(vec.begin(), vec.end(), cmp);
int lst = -1;
dp[getlca(vec[0], vec[num - 1])]--;
for (auto x : vec){
dp[x]++;
if (lst != -1) dp[getlca(x, lst)]--;
lst = x;
}
}
dfs2(1, 0);
/// for (int i = 1; i <= n; i++) cout << i << " : " << dp[i] << "\n";
sort(res.begin(), res.end());
cout << (int) res.size() << "\n";
for (auto x : res) cout << x << ' ';
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... |