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