이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <utility>
#include <queue>
#include <map>
#include <iomanip>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int n,a,b;
cin>>n;
vector<vector<int> >g(n);
for(int i=0;i<n-1;i++){
cin>>a>>b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
vector<int>j;
int con=0;
vector<pair<int,vector<int> > >padre(n,make_pair(0,j));
vector<pair<int,int> >nodo(n,make_pair(-1,-1));
for(int i=0;i<n;i++){
if(g[i].size()==1){
int p=g[i][0];
nodo[i]=make_pair(i,p);
j=padre[p].second;
j.push_back(i);
padre[p].second=j;
padre[p].first++;
con++;
}
}
int x=con;
if(x%2==0){
cout<<x/2<<"\n";
x/=2;
}
else{
cout<<x/2+1<<"\n";
x/=2;
x++;
}
int u=0;
for(int i=0;i<n;i++){
if(padre[i].first>2){
for(int k=0;k<padre[i].second.size();k++){
if(k<padre[i].second.size()-1){
if(k%2==0){
cout<<padre[i].second[k]+1<<" ";
}
else
{cout<<padre[i].second[k]+1<<"\n";
u++;
}
nodo[padre[i].second[k]].first=-1;
}
}
}
}
int p,k=0;
while(u<x){
bool im=false;
for(int i=k;i<n;i++){
if(nodo[i].first!=-1){
cout<<nodo[i].first+1<<" ";
nodo[i].first=-1;
p=nodo[i].second;
k=i;
break;
}
}
for(int i=k;i<n;i++){
if(nodo[i].first!=-1 &&nodo[i].second!=p){
cout<<nodo[i].first+1<<"\n";
nodo[i].first=-1;
im=true;
break;
}
}
if(!im){
for(int i=0;i<n;i++){
if(i!=p && i!=k){
cout<<i+1<<"\n";
break;
}
}
}
u++;
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
net.cpp: In function 'int main()':
net.cpp:54:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int k=0;k<padre[i].second.size();k++){
| ~^~~~~~~~~~~~~~~~~~~~~~~
net.cpp:55:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | if(k<padre[i].second.size()-1){
| ~^~~~~~~~~~~~~~~~~~~~~~~~~
net.cpp:81:26: warning: 'p' may be used uninitialized in this function [-Wmaybe-uninitialized]
81 | if(nodo[i].first!=-1 &&nodo[i].second!=p){
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |