이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define maxn 100050
#define f first
#define s second
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];
int r = seg[node*2+2];
if(l==-1){
seg[node] = r;
return;
}
if(r==-1){
seg[node] = l;
return;
}
if(depth[r]<depth[l]){
seg[node] = r;
}
else{
seg[node] = l;
}
}
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;
mini *= 2;
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;
vector<pair<int,int> > temp;
for(int j=0;j<no_of_input;j++){
cin >> input1;
temp.push_back({pos[input1],input1});
}
sort(temp.begin(),temp.end());
for(int j=0;j<no_of_input;j++){
int small = min(temp[j].f,temp[(j+1)%no_of_input].f);
int big = max(temp[j].f,temp[(j+1)%no_of_input].f);
int lca = query(0,0,vect1.size()-1,small,big);
//cout << lca << "--\n";
cnt[lca] -= 2;
cnt[temp[j].s]++;
cnt[temp[(j+1)%no_of_input].s]++;
}
}
dfs2(1,-1,-1);
sort(ans.begin(),ans.end());
cout << ans.size() << '\n';
for(auto k:ans){
cout << k+1 << " ";
}
}
컴파일 시 표준 에러 (stderr) 메시지
railway.cpp: In function 'int32_t main()':
railway.cpp:91:23: warning: unused variable 'input3' [-Wunused-variable]
int input1,input2,input3,input;
^~~~~~
railway.cpp:91: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... |