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>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
const ll mod = 998244353;
#define fi first
#define se second
#define rep(i,n) for(ll i=0;i<n;i++)
#define all(x) x.begin(),x.end()
#define faster ios::sync_with_stdio(false);cin.tie(nullptr)
vector<ll> bfs(vector<vector<ll>> G){
ll N=G.size();
vector<ll> dis(N,-1);
dis[0]=0;
queue<ll> que;
que.push(0);
while(!que.empty()){
ll v=que.front();
que.pop();
for(ll nv:G[v]){
if(dis[nv]!=-1) continue;
dis[nv]=dis[v]+1;
que.push(nv);
}
}
return dis;
}
int main() {
ll N,Q,K;
cin >> N >> Q >> K;
vector<vector<ll>> G(N);
vector<ll> A(N-1),B(N-1);
rep(i,N-1){
cin >> A[i] >> B[i];
A[i]--,B[i]--;
G[A[i]].push_back(B[i]);
G[B[i]].push_back(A[i]);
}
vector<ll> dis=bfs(G);
vector<ll> par(N,-1);
vector<ll> Eid(N,-1);
rep(i,N-1){
if(dis[A[i]]>dis[B[i]]) par[A[i]]=B[i],Eid[A[i]]=i;
if(dis[B[i]]>dis[A[i]]) par[B[i]]=A[i],Eid[B[i]]=i;
}
vector<ll> ans(N-1,0);
while(Q--){
ll x;
cin >> x;
vector<ll> t(x);
vector<ll> cnt(N-1,0);
rep(i,x) cin >> t[i],t[i]--;
rep(i,x-1){
ll u=t[i];
ll v=t[i+1];
while(dis[u]>dis[v]){
cnt[Eid[u]]++;
u=par[u];
}
while(dis[v]>dis[u]){
cnt[Eid[v]]++;
v=par[v];
}
while(u!=v){
cnt[Eid[u]]++;
u=par[u];
cnt[Eid[v]]++;
v=par[v];
}
}
rep(i,N-1) if(cnt[i]>0) ans[i]++;
}
ll ANS=0;
rep(i,N-1) if(ans[i]>=K) ANS++;
cout << ANS << endl;
rep(i,N-1) if(ans[i]>=K) cout << i+1 << " ";
cout << endl;
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... |