이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
vector <vector <ll> > adjl(1000000);
vector <ll> bl(1000000, 0);
ll listovi = 0;
vector <ll> leaf;
void dfs(ll u, ll p){
for (int i = 0; i < adjl[u].size(); i++){
ll v = adjl[u][i];
if (v != p)
dfs(v,u), bl[u] += bl[v];
}
if (adjl[u].size() == 1)
bl[u] += 1;
}
ll cent(ll u, ll p){
for (int i = 0; i < adjl[u].size(); i++){
ll v = adjl[u][i];
if (v != p){
if (listovi - bl[v] < bl[v])
return cent(v, u);
}
}
return u;
}
void relax(ll u, ll p){
if (adjl[u].size() == 1){
leaf.push_back(u);
return;
}
for (int i = 0; i < adjl[u].size(); i++){
ll v = adjl[u][i];
if (v != p)
relax(v,u);
}
}
int main()
{
ll n;
cin >> n;
for (int i = 0; i < n - 1; i++){
ll u, v;
cin >> u >> v;
u--, v--;
adjl[u].push_back(v), adjl[v].push_back(u);
}
ll start = 0;
for (int i = 0; i < n; i++)
if (adjl[i].size() != 1)
start = i;
else
listovi++;
dfs(start, -1);
ll centroid = cent(start, -1);
for (int i = 0; i < adjl[centroid].size(); i++){
ll v = adjl[centroid][i];
relax(v,centroid);
}
cout << (listovi+1)/2 << endl;
for (int i = 0; i < listovi/2; i++)
cout << leaf[i] + 1 << " " << leaf[i+listovi/2] + 1 << endl;
if (listovi%2 == 1)
cout << leaf[0] + 1 << " " << leaf[listovi-1] + 1<< endl;
}
컴파일 시 표준 에러 (stderr) 메시지
net.cpp: In function 'void dfs(ll, ll)':
net.cpp:17:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for (int i = 0; i < adjl[u].size(); i++){
| ~~^~~~~~~~~~~~~~~~
net.cpp: In function 'll cent(ll, ll)':
net.cpp:32:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int i = 0; i < adjl[u].size(); i++){
| ~~^~~~~~~~~~~~~~~~
net.cpp: In function 'void relax(ll, ll)':
net.cpp:58:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int i = 0; i < adjl[u].size(); i++){
| ~~^~~~~~~~~~~~~~~~
net.cpp: In function 'int main()':
net.cpp:95:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for (int i = 0; i < adjl[centroid].size(); 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... |