/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = (int) 1e5;
int N, M;
struct DSU {
int root[N_MAX + 2];
void init () {
iota(root + 1, root + N + 1, 1);
}
int findRoot (int u) {
return (root[u] == u ? u : root[u] = findRoot(root[u]));
}
bool join (int u, int v) {
u = findRoot(u);
v = findRoot(v);
root[u] = v;
return u != v;
}
};
DSU A, B;
vector <int> adj[N_MAX + 2];
void addEdge (int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
int level[N_MAX + 2];
int dfs (int u, int parent = -1) {
int minLevel = level[u];
bool par = false;
for (int v : adj[u]) {
if (level[v] == -1) {
level[v] = level[u] + 1;
minLevel = min(minLevel, dfs(v, u));
} else if (v == parent && par == false) {
par = true;
} else {
minLevel = min(minLevel, level[v]);
}
}
if (parent != -1 && minLevel == level[u]) {
cout << parent << " " << u << "\n";
}
return minLevel;
}
void dfs () {
fill(level + 1, level + N + 1, -1);
for (int u = 1; u <= N; u++) {
if (level[u] == -1) {
level[u] = 0;
dfs(u);
}
}
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
A.init(); B.init();
int cnt = 0;
while (M--) {
int u, v;
cin >> u >> v;
if (A.join(u, v) == true) {
addEdge(u, v); cnt++;
} else if (B.join(u, v) == true) {
addEdge(u, v); cnt++;
}
}
assert(cnt <= (N - 1) * 2);
dfs();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3120 KB |
Output is correct |
2 |
Correct |
5 ms |
2900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
2968 KB |
Output is correct |
2 |
Correct |
84 ms |
2828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
168 ms |
3508 KB |
Output is correct |
2 |
Correct |
201 ms |
3116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
244 ms |
4960 KB |
Output is correct |
2 |
Correct |
212 ms |
4660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
368 ms |
9616 KB |
Output is correct |
2 |
Correct |
301 ms |
6672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
588 ms |
10644 KB |
Output is correct |
2 |
Correct |
570 ms |
8188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
778 ms |
12560 KB |
Output is correct |
2 |
Correct |
686 ms |
8484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
923 ms |
12564 KB |
Output is correct |
2 |
Correct |
898 ms |
8488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1178 ms |
11984 KB |
Output is correct |
2 |
Correct |
981 ms |
9600 KB |
Output is correct |