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 int long long
const int MAXN=1e5;
vector<vector<int>> graph(MAXN+1);
int paidx[MAXN+1];
int w[MAXN+1]={0};
int depth[MAXN+1];
vector<vector<int>> sparse(18);
int in[2*MAXN+1];
int out[2*MAXN+1];
vector<int> loga;
int cmp(int a,int b){
if(depth[a]<depth[b]){
return a;
}
return b;
}
void DFS(int u,int p){
in[u]=sparse[0].size();
loga.push_back(loga[loga.size()>>1]+1);
sparse[0].push_back(u);
for(int v:graph[u]){
if(v!=p){
depth[v]=depth[u]+1;
DFS(v,u);
}
}
out[u]=sparse[0].size()-1;
if(p!=-1){
sparse[0].push_back(p);
}
}
int LCA(int u,int v){
int l=in[u],r=in[v];
if(l>r){
swap(l,r);
}
return cmp(sparse[loga[r-l+1]][l],sparse[loga[r-l+1]][r-(1<<loga[r-l+1])+1]);
}
bool cmp2(int a,int b){
return (in[a]<in[b]);
}
vector<int> ans;
int k;
void DFS2(int u,int p){
for(int v:graph[u]){
if(v!=p){
DFS2(v,u);
w[u]+=w[v];
}
}
if(w[u]>=k){
ans.push_back(paidx[u]);
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m;
cin >> n >> m >> k;
loga.push_back(-1);
vector<pair<int,int>> edges;
for(int i=0;i<n-1;i++){
int a,b;
cin >> a >> b;
edges.push_back({a,b});
graph[a].push_back(b);
graph[b].push_back(a);
}
depth[1]=0;
DFS(1,-1);
for(int i=1;i<18;i++){
for(int j=0;j+(1<<i)<=sparse[0].size();j++){
sparse[i].push_back(cmp(sparse[i-1][j],sparse[i-1][j+(1<<(i-1))]));
}
}
while(m--){
int s;
cin >> s;
vector<int> a(s);
for(int i=0;i<s;i++){
cin >> a[i];
}
sort(a.begin(),a.end(),cmp2);
for(int i=0;i+1<s;i++){
a.push_back(LCA(a[i],a[i+1]));
}
sort(a.begin(),a.end(),cmp2);
stack<int> st;
for(int i=0;i<a.size();i++){
if(i>0&&a[i]==a[i-1]){
continue;
}
while(!st.empty()&&out[st.top()]<out[a[i]]){
st.pop();
}
if(!st.empty()){
int u=a[i],p=st.top();
w[u]++;
w[p]--;
}
st.push(a[i]);
}
}
for(int i=0;i<edges.size();i++){
int u=edges[i].first,v=edges[i].second;
if(depth[u]>depth[v]){
swap(u,v);
}
paidx[v]=i+1;
}
DFS2(1,-1);
sort(ans.begin(),ans.end());
cout << ans.size() << endl;
for(int i=0;i<ans.size();i++){
cout << ans[i] << ' ';
}
}
Compilation message (stderr)
railway.cpp: In function 'int main()':
railway.cpp:75:29: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(int j=0;j+(1<<i)<=sparse[0].size();j++){
| ~~~~~~~~^~~~~~~~~~~~~~~~~~
railway.cpp:92:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for(int i=0;i<a.size();i++){
| ~^~~~~~~~~
railway.cpp:107:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for(int i=0;i<edges.size();i++){
| ~^~~~~~~~~~~~~
railway.cpp:117:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | for(int i=0;i<ans.size();i++){
| ~^~~~~~~~~~~
# | 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... |