# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
606815 | ThMinh_ | Network (BOI15_net) | C++14 | 672 ms | 239644 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define forin(i,a,b) for(int i=a;i<=b;++i)
#define forv(a,b) for(auto &a:b)
#define ii pair<int, int>
using namespace std;
const int N = 5e5+10;
int n, root;
int color[N];
vector<int> adj[N];
vector<ii> ans;
queue<int> dfs(int u) {
color[u] = 1;
queue<int> res;
if(adj[u].size() == 1) {res.push(u); return res;}
forv(v, adj[u]) if(!color[v]) {
queue<int> q = dfs(v);
if(res.size() + q.size() > 2 && res.size() > 0 && q.size() > 0) {
ans.push_back({res.front(), q.front()});
res.pop(); q.pop();
}
while(!q.empty()) {res.push(q.front()); q.pop();}
}
return res;
}
int main () {
cin.tie(0)->sync_with_stdio(0);
if(fopen("Task.inp","r")) {
freopen("Task.inp","r",stdin);
freopen("WA.out","w",stdout);
}
cin>>n;
forin(i,1,n - 1) {
int u, v; cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
queue<int> slt;
forin(i,1,n) if(adj[i].size() > 1) {slt = dfs(i); root = i; break;}
if(slt.size() == 2) {root = slt.front(); slt.pop();}
if(slt.size() == 1) ans.push_back({root, slt.front()});
cout<<ans.size()<<"\n";
forv(v, ans) cout<<v.first<<" "<<v.second<<"\n";
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |