#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44
using namespace std;
const int MAXN = (int) 1e5;
struct DSU {
int par[MAXN + 1];
int n;
inline void init(int _n) {
n = _n;
memset(par, 0, sizeof(par));
}
int root(int x) {
if(par[x] == 0)
return x;
return par[x] = root(par[x]);
}
inline void join(int x, int y) {
x = root(x), y = root(y);
if(x != y) {
par[x] = y;
}
}
}dsu1, dsu2;
vector <int> g[MAXN + 1];
inline void addEdge(int x, int y) {
g[x].push_back(y);
g[y].push_back(x);
}
int lvl[MAXN + 1], low[MAXN + 1];
void dfs(int nod, int par) {
lvl[nod] = lvl[par] + 1;
low[nod] = lvl[nod];
int cnt = 0;
for(auto it : g[nod]) {
if(lvl[it] == 0) {
dfs(it, nod);
low[nod] = min(low[nod], low[it]);
if(low[it] == lvl[it]) {
cout << nod << " " << it << "\n";
}
}
else {
if(it == par) {
if(cnt == 1) {
low[nod] = min(low[nod], lvl[it]);
}
cnt++;
}
else {
low[nod] = min(low[nod], lvl[it]);
}
}
}
}
int main() {
//ifstream cin("B.in");
//ofstream cout("B.out");
int i, n, m;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
dsu1.init(n);
dsu2.init(n);
for(i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
if(dsu1.root(x) == dsu1.root(y)) {
if(dsu2.root(x) != dsu2.root(y)) {
dsu2.join(x, y);
addEdge(x, y);
}
}
else {
dsu1.join(x, y);
addEdge(x, y);
}
}
for(i = 1; i <= n; i++) {
if(lvl[i] == 0) {
dfs(i, 0);
}
}
//cin.close();
//cout.close();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3456 KB |
Output is correct |
2 |
Correct |
4 ms |
3456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
3840 KB |
Output is correct |
2 |
Correct |
7 ms |
3840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
3784 KB |
Output is correct |
2 |
Correct |
96 ms |
3704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
160 ms |
4344 KB |
Output is correct |
2 |
Correct |
187 ms |
4088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
284 ms |
5756 KB |
Output is correct |
2 |
Correct |
241 ms |
5408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
433 ms |
10224 KB |
Output is correct |
2 |
Correct |
365 ms |
7364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
664 ms |
11244 KB |
Output is correct |
2 |
Correct |
594 ms |
8960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
815 ms |
13032 KB |
Output is correct |
2 |
Correct |
768 ms |
8952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1042 ms |
13132 KB |
Output is correct |
2 |
Correct |
986 ms |
9072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1259 ms |
12556 KB |
Output is correct |
2 |
Correct |
1145 ms |
10108 KB |
Output is correct |