#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 = 1e5+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);
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
net.cpp: In function 'int main()':
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("Task.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
net.cpp:53:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | freopen("WA.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
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.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
net.cpp:57:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | freopen("BlueHouse.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7384 KB |
Output is correct |
3 |
Correct |
4 ms |
7384 KB |
Output is correct |
4 |
Correct |
5 ms |
7392 KB |
Output is correct |
5 |
Correct |
4 ms |
7384 KB |
Output is correct |
6 |
Correct |
5 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
5 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
5 ms |
7380 KB |
Output is correct |
14 |
Correct |
4 ms |
7388 KB |
Output is correct |
15 |
Correct |
6 ms |
7380 KB |
Output is correct |
16 |
Correct |
6 ms |
7388 KB |
Output is correct |
17 |
Correct |
5 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7384 KB |
Output is correct |
3 |
Correct |
4 ms |
7384 KB |
Output is correct |
4 |
Correct |
5 ms |
7392 KB |
Output is correct |
5 |
Correct |
4 ms |
7384 KB |
Output is correct |
6 |
Correct |
5 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
5 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
5 ms |
7380 KB |
Output is correct |
14 |
Correct |
4 ms |
7388 KB |
Output is correct |
15 |
Correct |
6 ms |
7380 KB |
Output is correct |
16 |
Correct |
6 ms |
7388 KB |
Output is correct |
17 |
Correct |
5 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
4 ms |
7380 KB |
Output is correct |
21 |
Correct |
4 ms |
7380 KB |
Output is correct |
22 |
Correct |
5 ms |
7636 KB |
Output is correct |
23 |
Correct |
7 ms |
7636 KB |
Output is correct |
24 |
Correct |
5 ms |
7648 KB |
Output is correct |
25 |
Correct |
5 ms |
7636 KB |
Output is correct |
26 |
Correct |
5 ms |
7508 KB |
Output is correct |
27 |
Correct |
6 ms |
7712 KB |
Output is correct |
28 |
Correct |
5 ms |
7572 KB |
Output is correct |
29 |
Correct |
5 ms |
7508 KB |
Output is correct |
30 |
Correct |
5 ms |
7384 KB |
Output is correct |
31 |
Correct |
5 ms |
7696 KB |
Output is correct |
32 |
Correct |
6 ms |
7384 KB |
Output is correct |
33 |
Correct |
5 ms |
7388 KB |
Output is correct |
34 |
Correct |
3 ms |
7380 KB |
Output is correct |
35 |
Correct |
4 ms |
7380 KB |
Output is correct |
36 |
Correct |
4 ms |
7380 KB |
Output is correct |
37 |
Correct |
5 ms |
7892 KB |
Output is correct |
38 |
Correct |
6 ms |
8148 KB |
Output is correct |
39 |
Correct |
5 ms |
7508 KB |
Output is correct |
40 |
Correct |
6 ms |
7388 KB |
Output is correct |
41 |
Correct |
6 ms |
7384 KB |
Output is correct |
42 |
Correct |
5 ms |
7380 KB |
Output is correct |
43 |
Correct |
6 ms |
7508 KB |
Output is correct |
44 |
Correct |
5 ms |
7380 KB |
Output is correct |
45 |
Correct |
4 ms |
7380 KB |
Output is correct |
46 |
Correct |
5 ms |
7516 KB |
Output is correct |
47 |
Correct |
5 ms |
7384 KB |
Output is correct |
48 |
Correct |
4 ms |
7384 KB |
Output is correct |
49 |
Correct |
7 ms |
7508 KB |
Output is correct |
50 |
Correct |
4 ms |
7380 KB |
Output is correct |
51 |
Correct |
4 ms |
7380 KB |
Output is correct |
52 |
Correct |
6 ms |
7640 KB |
Output is correct |
53 |
Correct |
7 ms |
7764 KB |
Output is correct |
54 |
Correct |
4 ms |
7384 KB |
Output is correct |
55 |
Correct |
5 ms |
7444 KB |
Output is correct |
56 |
Correct |
6 ms |
7380 KB |
Output is correct |
57 |
Correct |
5 ms |
7352 KB |
Output is correct |
58 |
Correct |
6 ms |
7636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7384 KB |
Output is correct |
3 |
Correct |
4 ms |
7384 KB |
Output is correct |
4 |
Correct |
5 ms |
7392 KB |
Output is correct |
5 |
Correct |
4 ms |
7384 KB |
Output is correct |
6 |
Correct |
5 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
5 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
5 ms |
7380 KB |
Output is correct |
14 |
Correct |
4 ms |
7388 KB |
Output is correct |
15 |
Correct |
6 ms |
7380 KB |
Output is correct |
16 |
Correct |
6 ms |
7388 KB |
Output is correct |
17 |
Correct |
5 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
4 ms |
7380 KB |
Output is correct |
21 |
Correct |
4 ms |
7380 KB |
Output is correct |
22 |
Correct |
5 ms |
7636 KB |
Output is correct |
23 |
Correct |
7 ms |
7636 KB |
Output is correct |
24 |
Correct |
5 ms |
7648 KB |
Output is correct |
25 |
Correct |
5 ms |
7636 KB |
Output is correct |
26 |
Correct |
5 ms |
7508 KB |
Output is correct |
27 |
Correct |
6 ms |
7712 KB |
Output is correct |
28 |
Correct |
5 ms |
7572 KB |
Output is correct |
29 |
Correct |
5 ms |
7508 KB |
Output is correct |
30 |
Correct |
5 ms |
7384 KB |
Output is correct |
31 |
Correct |
5 ms |
7696 KB |
Output is correct |
32 |
Correct |
6 ms |
7384 KB |
Output is correct |
33 |
Correct |
5 ms |
7388 KB |
Output is correct |
34 |
Correct |
3 ms |
7380 KB |
Output is correct |
35 |
Correct |
4 ms |
7380 KB |
Output is correct |
36 |
Correct |
4 ms |
7380 KB |
Output is correct |
37 |
Correct |
5 ms |
7892 KB |
Output is correct |
38 |
Correct |
6 ms |
8148 KB |
Output is correct |
39 |
Correct |
5 ms |
7508 KB |
Output is correct |
40 |
Correct |
6 ms |
7388 KB |
Output is correct |
41 |
Correct |
6 ms |
7384 KB |
Output is correct |
42 |
Correct |
5 ms |
7380 KB |
Output is correct |
43 |
Correct |
6 ms |
7508 KB |
Output is correct |
44 |
Correct |
5 ms |
7380 KB |
Output is correct |
45 |
Correct |
4 ms |
7380 KB |
Output is correct |
46 |
Correct |
5 ms |
7516 KB |
Output is correct |
47 |
Correct |
5 ms |
7384 KB |
Output is correct |
48 |
Correct |
4 ms |
7384 KB |
Output is correct |
49 |
Correct |
7 ms |
7508 KB |
Output is correct |
50 |
Correct |
4 ms |
7380 KB |
Output is correct |
51 |
Correct |
4 ms |
7380 KB |
Output is correct |
52 |
Correct |
6 ms |
7640 KB |
Output is correct |
53 |
Correct |
7 ms |
7764 KB |
Output is correct |
54 |
Correct |
4 ms |
7384 KB |
Output is correct |
55 |
Correct |
5 ms |
7444 KB |
Output is correct |
56 |
Correct |
6 ms |
7380 KB |
Output is correct |
57 |
Correct |
5 ms |
7352 KB |
Output is correct |
58 |
Correct |
6 ms |
7636 KB |
Output is correct |
59 |
Runtime error |
12 ms |
14720 KB |
Execution killed with signal 11 |
60 |
Halted |
0 ms |
0 KB |
- |