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 maxn 100050
vector<vector<pair<int,int> > > adj(maxn);
vector<int> cnt(maxn,0);
vector<int> vect1;
vector<int> seg(maxn*20,0);
vector<int> depth(maxn,INT_MAX);
vector<int> pos(maxn);
int no_of_vertex,no_of_people,mini;
void build(int node,int start,int end){
if(start==end){
seg[node] = vect1[start];
return;
}
int mid = (start+end)/2;
build(node*2+1,start,mid);
build(node*2+2,mid+1,end);
int l = seg[node*2+1],r=seg[node*2+2];
if(depth[l]<depth[r]){
seg[node] = l;
}
else{
seg[node] = r;
}
}
int query(int node,int start,int end,int rangemin,int rangemax){
//cout << node << " " << start << " " << end << " "<< rangemin << " " << rangemax << " " << seg[node] << endl;
if(start>rangemax||end<rangemin){
return -1;
}
if(start>=rangemin&&end<=rangemax){
return seg[node];
}
int mid = (start+end)/2;
int a = query(node*2+1,start,mid,rangemin,rangemax);
int b = query(node*2+2,mid+1,end,rangemin,rangemax);
if(a==-1){
return b;
}
else if(b==-1){
return a;
}
if(depth[a]<depth[b]){
return a;
}
else{
return b;
}
}
void dfs(int node,int parent,int d){
pos[node] = (int)vect1.size();
vect1.push_back(node);
depth[node] = d;
for(auto k:adj[node]){
if(k.first!=parent){
vect1.push_back(node);
dfs(k.first,node,d+1);
vect1.push_back(node);
}
}
}
vector<int> ans;
void dfs2(int node,int parent,int last){
for(auto k:adj[node]){
if(k.first!=parent){
dfs2(k.first,node,k.second);
cnt[node] += cnt[k.first];
}
}
if(cnt[node]>=mini){
ans.push_back(last);
}
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int no_of_input;
int input1,input2,input3,input;
cin >> no_of_vertex >> no_of_people >> mini;
for(int i=0;i<no_of_vertex-1;i++){
cin >> input1 >> input2;
adj[input1].push_back({input2,i});
adj[input2].push_back({input1,i});
}
dfs(1,-1,0);
build(0,0,vect1.size()-1);
for(int i=0;i<no_of_people;i++){
cin >> no_of_input >> input1 >> input2;
assert(no_of_input==2);
int lca = query(0,0,vect1.size()-1,min(pos[input1],pos[input2]),max(pos[input1],pos[input2]));
cnt[input1]++;
cnt[input2]++;
cnt[lca] -= 2;
}
/*for(int i=1;i<=no_of_vertex;i++){
for(int j=1;j<=no_of_vertex;j++){
cout << i << " " << j << " " << query(0,0,vect1.size()-1,min(pos[i],pos[j]),max(pos[i],pos[j])) << "\n";
}
}*/
dfs2(1,-1,-1);
for(int i=1;i<=no_of_vertex;i++){
assert(cnt[i]==0);
}
sort(ans.begin(),ans.end());
cout << ans.size() << '\n';
for(auto k:ans){
cout << k+1 << " ";
}
}
Compilation message (stderr)
railway.cpp: In function 'int32_t main()':
railway.cpp:79:23: warning: unused variable 'input3' [-Wunused-variable]
int input1,input2,input3,input;
^~~~~~
railway.cpp:79:30: warning: unused variable 'input' [-Wunused-variable]
int input1,input2,input3,input;
^~~~~
# | 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... |