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>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define pb push_back
#define rc (i<<1)|1
#define lc (i<<1)
#define el "\n"
#define f first
#define s second
typedef long long ll;
const int MM=1e5+5, MOD=1e9+7, LOG=19;
int N, M, K, psa[MM], dep[MM], son[MM], top[MM], fa[MM], sz[MM], in[MM], ot[MM], cnt;
vector<pair<int,int>> adj[MM];
vector<int> vdj[MM], ans;
void df1(int u, int pa=0) {
dep[u]=dep[pa]+1; fa[u]=pa; sz[u]=1; in[u]=++cnt;
for(auto &e:adj[u]) if(e.f!=pa) {
int v=e.f;
df1(v, u); sz[u]+=sz[v];
if(sz[v]>sz[son[u]]) son[u]=v; }
ot[u]=++cnt;
}
void df2(int u, int pa=0) {
if(son[u]) {
top[son[u]]=top[u]; df2(son[u], u); }
for(auto &e:adj[u]) if(e.f!=pa&&e.f!=son[u]) {
df2(top[e.f]=e.f, u);
}
}
bool above(int u, int v) {
return in[u]<in[v]&&ot[v]<ot[u];
}
bool cmp(int u, int v) {
return in[u]<in[v];
}
int lca(int u, int v) {
for(; top[u]!=top[v]; u=fa[top[u]]) {
if(dep[top[u]]<dep[top[v]]) swap(u, v); }
return (dep[u]<dep[v]?u:v);
}
int buildvrt(vector<int> &vrt, int k) {
sort(begin(vrt), end(vrt), cmp);
for(int i=1; i<k; ++i) {
vrt.pb(lca(vrt[i-1], vrt[i])); }
sort(begin(vrt), end(vrt), cmp);
vrt.erase(unique(begin(vrt), end(vrt)), end(vrt));
vector<int> stk={vrt[0]};
for(int i=1; i<vrt.size(); ++i) {
int u=vrt[i];
while(stk.size()>=2&&!above(stk.back(), u)) {
vdj[stk[stk.size()-2]].pb(stk.back());
stk.pop_back(); }
stk.pb(u); }
while(stk.size()>=2) {
vdj[stk[stk.size()-2]].pb(stk.back());
stk.pop_back(); }
return stk[0];
}
void df3(int u, int pa=0) {
for(int &v:vdj[u]) if(v!=pa) ++psa[v], --psa[u], df3(v, u);
vdj[u].clear();
}
void df4(int u, int pa=0) {
for(auto &e:adj[u]) if(e.f!=pa) {
df4(e.f, u); psa[u]+=psa[e.f]; }
for(auto &e:adj[u]) if(e.f!=pa) {
if(psa[e.f]>=K) ans.pb(e.s);
}
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin>>N>>M>>K;
for(int i=1, u, v; i<N; ++i) {
cin>>u>>v;
adj[u].pb({v,i}); adj[v].pb({u,i}); }
df1(1); df2(top[1]=1);
for(int i=1, k; i<=M; ++i) {
cin>>k; vector<int> vrt;
for(int j=1, x; j<=k; ++j) cin>>x, vrt.pb(x);
df3( buildvrt(vrt, k)); }
df4(1);
sort(begin(ans), end(ans));
cout<<ans.size()<<el;
for(int i=0; i<ans.size(); ++i) cout<<ans[i]<<" \n"[i==ans.size()-1];
}
// MM
Compilation message (stderr)
railway.cpp: In function 'int buildvrt(std::vector<int>&, int)':
railway.cpp:56:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int i=1; i<vrt.size(); ++i) {
| ~^~~~~~~~~~~
railway.cpp: In function 'int32_t main()':
railway.cpp:95:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(int i=0; i<ans.size(); ++i) cout<<ans[i]<<" \n"[i==ans.size()-1];
| ~^~~~~~~~~~~
railway.cpp:95:58: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(int i=0; i<ans.size(); ++i) cout<<ans[i]<<" \n"[i==ans.size()-1];
| ~^~~~~~~~~~~~~~
# | 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... |