이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
typedef pair<int, int> ii;
const int N = 5e5 + 5;
int n, child[N], nLeaf, cur, tmp, pter[N];
vector<int> gr[N], vec[N];
void prep (int u, int p) {
for (int v : gr[u]) if (v != p) {
prep(v, u);
child[u] += child[v];
}
child[u] += (gr[u].size() == 1);
}
int findCentroid (int u, int p) {
for (int v : gr[u]) if (v != p) {
if (child[v] >= (nLeaf + 1) / 2 ) return findCentroid(v, u);
}
return u;
}
void dfs (int u, int p) {
for (int v : gr[u]) if (v != p) dfs(v, u);
if (gr[u].size() == 1) vec[cur].pb(u);
}
int main () {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int _ = 1; _ < n; ++_) {
int u, v; cin >> u >> v;
gr[u].pb(v); gr[v].pb(u);
}
int root;
for (int i = 1; i <= n; ++i) if (gr[i].size() ) root = i;
for (int u = 1; u <= n; ++u) nLeaf += (gr[u].size() == 1);
cout << (nLeaf + 1) / 2 << '\n';
prep(root, root);
int cen = findCentroid(root, root);
for (int u : gr[cen]) cur ++, dfs(u, cen);
priority_queue<ii> pq;
for (int i = 1; i <= cur; ++i) {
pq.push(ii(vec[i].size() - pter[i], i) );
}
while (pq.size() > 1) {
ii tmp1 = pq.top(); pq.pop();
ii tmp2 = pq.top(); pq.pop();
cout << vec[tmp1.se][ pter[tmp1.se] ] << ' ' << vec[tmp2.se][ pter[tmp2.se] ] << '\n';
pter[tmp1.se] ++; pter[tmp2.se] ++;
if (pter[tmp1.se] < vec[tmp1.se].size() ) pq.push(ii(vec[tmp1.se].size() - pter[tmp1.se], tmp1.se) );
if (pter[tmp2.se] < vec[tmp2.se].size() ) pq.push(ii(vec[tmp2.se].size() - pter[tmp2.se], tmp2.se) );
}
if (pq.size() ) {
int u = vec[pq.top().se][ pter[pq.top().se] ];
for (int v = 1; v <= n; ++v) if (gr[v].size() == 1 && v != u) { cout << v << ' ' << u << '\n'; break; }
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
net.cpp: In function 'int main()':
net.cpp:62:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (pter[tmp1.se] < vec[tmp1.se].size() ) pq.push(ii(vec[tmp1.se].size() - pter[tmp1.se], tmp1.se) );
~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
net.cpp:63:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (pter[tmp2.se] < vec[tmp2.se].size() ) pq.push(ii(vec[tmp2.se].size() - pter[tmp2.se], tmp2.se) );
~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
net.cpp:47:38: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
int cen = findCentroid(root, root);
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |