-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB_Fish_Graph.cpp
More file actions
111 lines (90 loc) · 2.31 KB
/
B_Fish_Graph.cpp
File metadata and controls
111 lines (90 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <bits/stdc++.h>
using namespace std;
#define int long long
inline void n_lamba_29() {
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n+1);
for(int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<bool> vis(n+1);
vector<int> par(n+1);
vector<pair<int,int>> ce;
function<void(int, int)> dfs = [&](int node, int p) {
vis[node] = 1;
par[node] = p;
for(auto &j : adj[node]) {
if(!vis[j]) {
dfs(j, node);
}
else if(j != p) {
ce.push_back({node, j});
}
}
};
for(int i = 1; i <=n; i++) {
if(!vis[i]) {
dfs(i, i);
}
}
vector<vector<bool>> mp(n+1, vector<bool>(n+1));
for(auto &[start, end] : ce) {
if(mp[min(start, end)][max(start, end)]) continue;
mp[min(start, end)][max(start, end)] = true;
vector<bool> isPath(n+1);
vector<int> path, deg(n+1);
int temp = start;
int spnode = -1;
while(temp != end) {
isPath[temp] = true;
path.push_back(temp);
temp = par[temp];
}
path.push_back(temp);
isPath[temp] = true;
for(int node : path) {
for(auto & j : adj[node]) {
if(!isPath[j]) {
deg[node]++;
}
}
}
for(int node : path) {
if(deg[node] >= 2) {
spnode = node;
break;
}
}
if(spnode != -1) {
cout << "YES\n";
cout << path.size() + 2 << "\n";
for(int i=0;i<path.size()-1;i++) {
cout << path[i] << " " << path[i+1] << "\n";
}
cout << end << " " << start << "\n";
int cnt = 0;
for(auto &j : adj[spnode]) {
if(!isPath[j] && cnt < 2) {
cout << spnode << " " << j << "\n";
cnt++;
}
}
return;
}
}
cout << "NO\n";
}
int32_t main(){
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
int _;
cin >> _;
while (_-->0) {
n_lamba_29();
}
return 0;
}