이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ar array
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 5e5 + 5;
int deg[maxn], N;
vector<int> leaf;
vector<int> adj[maxn];
int sz[maxn];
void dfs(int u, int p = -1)
{
sz[u] = 1;
for(int v : adj[u]) if(v != p){
dfs(v, u);
sz[u] += sz[v];
}
if(sz[u] == 1) leaf.eb(u);
}
signed main(void)
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
#endif // LOCAL
cin >> N;
for(int i = 1; i < N; ++i){
int u, v; cin >> u >> v;
deg[u]++; deg[v]++;
adj[u].eb(v); adj[v].eb(u);
}
int r;
for(int i = 1; i <= N; ++i)
if(adj[i].size() >= 2) r = i;
dfs(r);
cout << (leaf.size() + 1) / 2 << '\n';
for(int i = 0; i < leaf.size(); i += 2){
if(i == leaf.size() - 1){
cout << leaf[i - 1] << ' ' << leaf[i] << '\n';
}
else cout << leaf[i] << ' ' << leaf[i + 1] << '\n';
}
}
컴파일 시 표준 에러 (stderr) 메시지
net.cpp: In function 'int main()':
net.cpp:50:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < leaf.size(); i += 2){
~~^~~~~~~~~~~~~
net.cpp:51:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i == leaf.size() - 1){
~~^~~~~~~~~~~~~~~~~~
net.cpp:48:6: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
dfs(r);
~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |