이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//Bismillahir Rahmanir Rahim
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
const int maxn = 500100;
vector<int> adj[maxn], lef;
void dfs(int u, int par){
int cnt = 0;
for(int v : adj[u]) if(v - par){
cnt++, dfs(v, u);
}
if(!cnt) lef.push_back(u);
}
int main(int argc, char const *argv[]) {
#ifdef LOCAL_TESTING
freopen("in", "r", stdin);
#endif
int n; cin>>n;
for(int i=0; i<n-1; i++) {
int u, v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 0);
vector<int> a = lef;
lef.clear();
dfs(2, 0);
vector<int> b = lef;
sort(a.begin(), a.end());
sort(b.begin(), b.end());
vector<int> c;
merge(a.begin(), a.end(),
b.begin(), b.end(),
back_inserter(c));
c.erase(unique(c.begin(), c.end()), c.end());
if(c.size() == 1) {
cout<<1<<endl;
cout<<c[0]<<" " << 1<<endl;
return 0;
}
cout<<(int) (c.size() + 1)/2<<endl;
for(int i=0; i+1<c.size(); i+=2) {
cout<<c[i]<<" " << c[i+1]<<endl;
}
if(c.size() & 1) {
cout << c[0] << " " << c[c.size()-1]<<endl;
}
}
컴파일 시 표준 에러 (stderr) 메시지
net.cpp: In function 'int main(int, const char**)':
net.cpp:45:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i+1<c.size(); i+=2) {
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |