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 ll long long
#define pi pair<int,int>
#define pii pair<int,pair<int,int>>
#define fi first
#define se second
const int N = 1e5 + 5;
const int LG = 20;
int p[N][LG + 2], tin[N], tout[N], timer, n, m, k, inc[N], de[N], dep[N], val[N], a[N];
vector<pi> adj[N];
void dfs1(int u, int par){
for(int k = 1; k < LG; k++)
p[u][k] = p[p[u][k - 1]][k - 1];
tin[u] = ++timer;
for(auto x:adj[u]){
if(x.fi == par) continue;
int v = x.fi;
p[v][0] = u;
dep[v] = dep[u] + 1;
dfs1(v, u);
}
tout[u] = timer;
}
bool ances(int u, int v){
return tin[u] <= tin[v] && tout[u] > tout[v];
}
int lca(int u, int v){
if(u == v) return u;
if(dep[u] < dep[v]) swap(u, v);
for(int k = LG - 1; k >= 0; k--)
if(dep[p[u][k]] >= dep[v]) u = p[u][k];
if(u == v) return u;
for(int k = LG - 1; k >= 0; k--)
if(p[u][k] != p[v][k]) u = p[u][k], v = p[v][k];
return p[u][0];
}
void dfs2(int u, int par){
for(auto x:adj[u]){
int id = x.se;
int v = x.fi;
if(v == par) continue;
dfs2(v, u);
inc[u] += inc[v];
val[id] = inc[v];
// cout << u << ' ' << v << ' ' << inc[v] << '\n';
}
inc[u] -= de[u];
}
void UP(int u, int v){
inc[v]++;
de[u]++;
}
bool cmd(int u, int v){
return tin[u] < tin[v];
}
signed main(){
cin.tie(0)->sync_with_stdio(false);
// freopen("IN.INP","r",stdin);
// freopen("OU.OUT","w",stdout);
cin >> n >> m >> k;
for(int i = 1; i < n; i++){
int u, v; cin >> u >> v;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
}
dep[1] = 1;
dfs1(1, 0);
// cout << lca(2, 5) << '\n';
for(int k = 1; k <= m; k++){
int x; cin >> x;
for(int i = 1; i <= x; i++) cin >> a[i];
sort(a + 1, a + x + 1, cmd);
int _lca = a[1];
for(int i = 2; i <= x; i++){
// cout << a[i] << ' ';
if(ances(a[i - 1], a[i])){
UP(a[i - 1], a[i]);
}else{
int tmp = lca(_lca, a[i]);
if(tmp == _lca){
UP(lca(a[i - 1], a[i]), a[i]);
}else{
UP(tmp, _lca);
UP(tmp, a[i]);
_lca = tmp;
}
}
}
// cout << '\n';
// for(int i = 1; i <= n; i++) cout <<i << ' '<< inc[i] << ' ' << de[i] << '\n';
}
dfs2(1, 0);
std::vector<int> vec;
for(int i = 1; i < n; i++)
if(val[i] >= k) vec.push_back(i);
cout << vec.size() << '\n';
for(auto x:vec) 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... |