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, m, cnt, tim, root;
int low[N], num[N], color[N], vis[N], id[N];
vector<int> loop[N], ke[N];
vector<ii> adj[N], ans;
stack<int> st;
void anal(int u, int edad) {
low[u] = num[u] = ++tim;
vis[u] = 1; st.push(u);
forv(e, adj[u]) {
int v = e.first, eid = e.second;
if(edad == eid) continue;
if(!vis[v]) {
anal(v, eid);
low[u] = min(low[u], low[v]);
}
else if(vis[v] == 1) low[u] = min(low[u], num[v]);
}
if(low[u] == num[u]) {
int v; cnt++;
do {
v = st.top(); st.pop();
loop[cnt].push_back(v);
id[v] = cnt;
vis[v] = 2;
} while(v != u);
}
}
queue<int> dfs(int u) {
color[u] = 1;
queue<int> res;
if(ke[u].size() == 1) {res.push(u); return res;}
forv(v, ke[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);
}
if(fopen("BlueHouse.inp","r")) {
freopen("BlueHouse.inp","r",stdin);
freopen("BlueHouse.out","w",stdout);
}
cin>>n;
m = n - 1;
forin(i,1,m) {
int u, v; cin>>u>>v;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
}
anal(1, 0);
if(cnt == 2) {
cout<<1<<"\n"<<loop[1].back()<<" "<<loop[2].back();
return 0;
}
forin(i,1,cnt) {
forv(u, loop[i]) {
forv(v, adj[u]) if(id[v.first] != i) {
ke[i].push_back(id[v.first]);
}
}
}
queue<int> slt;
forin(i,1,cnt) if(ke[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<<loop[v.first].back()<<" "<<loop[v.second].back()<<"\n";
}
Compilation message (stderr)
net.cpp: In function 'int main()':
net.cpp:51:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | freopen("Task.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
net.cpp:52:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | freopen("WA.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
net.cpp:55:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | freopen("BlueHouse.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
net.cpp:56:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | freopen("BlueHouse.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |