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 endl '\n'
#define ll long long
const ll sz=1e5+5;
ll pre[sz], tt, pos[sz], in[sz], root, p[sz], lvl[sz], dp[sz][100], ln, mx;
vector<ll>g[sz], vec={0};
set<ll>s;
/*
{} []
*/
void dfs1(ll u, ll par)
{
vec.push_back(u);
pos[u]=++tt;
for(auto v:g[u]){
if(v!=par){
dfs1(v, u);
}
}
}
void dfs2(ll u, ll par)
{
lvl[u]=lvl[par]+1;
dp[u][0]=par;
for(int i=1; i<ln; i++){
dp[u][i]=dp[dp[u][i-1]][i-1];
}
for(auto v:g[u]){
if(v!=par){
p[v]=u;
dfs2(v, u);
}
}
}
ll lca(ll u, ll v)
{
if(lvl[u]<lvl[v]) swap(u, v);
ll dif=lvl[u]-lvl[v];
for(int i=0; i<ln; i++){
if(dif&(1<<i)){
u=dp[u][i];
}
}
if(u==v) return u;
for(int i=ln-1; i>=0; i--){
if(dp[u][i]!=dp[v][i]){
u=dp[u][i];
v=dp[v][i];
}
}
return dp[u][0];
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll n, m, kk;
cin>>n>>m>>kk;
map<pair<ll, ll>, ll>m1;
for(int i=1; i<n; i++){
ll a, b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
m1[{a, b}]=i;
m1[{b, a}]=i;
in[a]++; in[b]++;
}
for(int i=1; i<=n; i++){
if(in[i]==1) root=i;
mx=max(mx, in[i]);
}
if(mx>2){
map<pair<ll, ll> , ll>mp;
dfs2(1, 0);
ln=(ceil)(log2(n));
dfs2(1, 0);
for(int i=1; i<=m; i++){
ll num;
cin>>num;
ll k[num+1];
for(int j=1; j<=num; j++){
cin>>k[j];
}
set<pair<ll, ll>>cur;
for(int j=2; j<=num; j++){
ll x=k[j-1], y=k[j];
ll l=lca(x, y);
//cout<<x<<' '<<y<<' '<<l<<endl;
while(y!=l){
ll a=y, b=p[y];
ll c=a, d=b;
//cout<<a<<' '<<b<<endl;
if(c>d) swap(c, d);
cur.insert({c, d});
y=b;
}
while(x!=l){
ll a=x, b=p[x];
ll c=a, d=b;
if(c>d) swap(c, d);
cur.insert({c, d});
x=b;
}
}
for(auto i:cur){
mp[{i.first, i.second}]++;
}
}
for(auto i:mp){
if(i.second>=kk){
s.insert(m1[{i.first.first, i.first.second}]);
}
}
cout<<s.size()<<endl;
for(auto i:s) cout<<i<<' ';
return 0;
}
map<ll, ll>mp;
dfs1(root, 0);
for(int i=1; i<=m; i++){
ll num;
cin>>num;
ll k[num+1];
for(int j=1; j<=num; j++){
cin>>k[j];
}
for(int j=2; j<=num; j++){
ll x=k[j-1], y=k[j];
ll a=pos[x], b=pos[y];
if(a>b) swap(a, b);
pre[a]++; pre[b]--;
}
}
for(int i=1; i<n; i++){
pre[i]+=pre[i-1];
mp[m1[{vec[i], vec[i+1]}]]+=pre[i];
//cout<<p[i]<<' ';
}
for(auto i:mp){
if(i.second>=kk){
s.insert(i.first);
}
}
cout<<s.size()<<endl;
for(auto i:s) cout<<i<<' ';
}
/*
{} []
4 3 3
2 1
1 4
4 3
3 1 2 3
2 1 4
2 2 3
*/
# | 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... |