#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
const int MAXN = 100'005;
vector<vector<int>> adjlist;
vector<int> tvis, lo;
int cnt = 0;
void dfs(int x, int par) {
tvis[x] = lo[x] = cnt++;
bool vpar = 0;
for (auto nxt : adjlist[x]) {
if (nxt == par && !vpar) {
vpar = 1;
continue;
}
if (tvis[nxt] != INT_MAX) lo[x] = min(lo[x], tvis[nxt]);
else {
dfs(nxt, x);
lo[x] = min(lo[x], lo[nxt]);
if (lo[nxt] > tvis[x]) cout << nxt << ' ' << x << '\n';
}
}
}
int root(int a, vector<int>& par) {
if (par[a] == -1) return a;
else return par[a] = root(par[a], par);
}
void merge(int a, int b, vector<int>& par, vector<int> &rank) {
if (rank[a] > rank[b]) swap(a, b); // b has higher rank
par[a] = b;
if (rank[a] == rank[b]) rank[b]++;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int nodes, edges; cin >> nodes >> edges;
vector<int> par1(nodes + 1, -1), par2(nodes + 1, -1), rank1(nodes + 1, -1), rank2(nodes + 1, -1);
adjlist.resize(nodes + 1);
tvis.resize(nodes + 1); lo.resize(nodes + 1);
for (int i = 0; i < edges; i++) {
int a, b; cin >> a >> b;
if (root(a, par1) != root(b, par1)) {
merge(root(a, par1), root(b, par1), par1, rank1);
adjlist[a].push_back(b);
adjlist[b].push_back(a);
}
else if (root(a, par2) != root(b, par2)) {
merge(root(a, par2), root(b, par2), par2, rank2);
adjlist[a].push_back(b);
adjlist[b].push_back(a);
}
}
for (int i = 1; i <= nodes; i++) tvis[i] = INT_MAX;
for (int i = 1; i <= nodes; i++)
if (tvis[i] == INT_MAX)
dfs(i, -1);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1100 KB |
Output is correct |
2 |
Correct |
3 ms |
844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
1516 KB |
Output is correct |
2 |
Correct |
87 ms |
1348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
149 ms |
2792 KB |
Output is correct |
2 |
Correct |
175 ms |
2500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
240 ms |
5172 KB |
Output is correct |
2 |
Correct |
210 ms |
4452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
305 ms |
11500 KB |
Output is correct |
2 |
Correct |
269 ms |
7376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
485 ms |
13124 KB |
Output is correct |
2 |
Correct |
483 ms |
9660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
654 ms |
16044 KB |
Output is correct |
2 |
Correct |
592 ms |
10124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
884 ms |
16420 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
951 ms |
15436 KB |
Output is correct |
2 |
Correct |
904 ms |
12624 KB |
Output is correct |