# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
147130 | fadi57 | Network (BOI15_net) | C++14 | 589 ms | 47856 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
problem is equivalent to: cover the tree with minimum number of paths from one node to another one
optimal solution is to connect nodes with degree = 1
connect leaves i and i+sz/2 where sz is size of set of the leaves
this works because it is always possible to pick a node such that number of leaves in its subtree is sz/2
*handle the case when sz is odd
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9+7;
const int N = 5e5+5;
int n;
vector<int>leaf;
vector<int>v[N];
void dfs(int s,int p){
if(v[s].size()==1){
leaf.push_back(s);
}
for(int i=0;i<v[s].size();i++){
if(v[s][i]!=p){
dfs(v[s][i],s);
}
}
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
int x,y;
cin>>n;
for(int i=0;i<n-1;i++){
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1,-1);
int k=(int)leaf.size();
if(k%2==0){
cout<<k/2<<"\n";
for(int i=0;i<k/2;i++){
cout<<leaf[i]<<" "<<leaf[i+k/2]<<"\n";
}
}else{
cout<<k/2+1<<"\n";
for(int i=0;i<k/2;i++){
cout<<leaf[i]<<" "<<leaf[i+k/2+1]<<"\n";
}
cout<<leaf[k/2]<<" "<<1;
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |