이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 4e18+9;
void solve(){
int n;
cin >> n;
vector<vector<int>> adj(n+1);
for(int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> leaf;
auto dfs = [&](auto dfs, int u, int p) -> void{
if(adj[u].size() == 1){
leaf.push_back(u);
}
for(int v : adj[u]){
if(v == p) continue;
dfs(dfs, v, u);
}
};
for(int i = 1; i <= n; i++){
if(adj[i].size() > 1){
dfs(dfs, i, 0);
break;
}
}
//leaf is sorted based on dfs order
cout << (leaf.size()+1)/2 << "\n";
for(int i = 0; i < leaf.size()/2; i++){
cout << leaf[i] << " " << leaf[i+leaf.size()/2] << "\n";
}
if(leaf.size()&1){
cout << leaf[0] << " " << leaf.back() << "\n";
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
컴파일 시 표준 에러 (stderr) 메시지
net.cpp: In function 'void solve()':
net.cpp:34:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(int i = 0; i < leaf.size()/2; i++){
| ~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |