#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
vector<vector<int>> graph;
vector<pii> bridges;
vector<int> rep;
vector<int> comp;
vector<int> low;
vector<int> num;
vector<int> complow;
vector<vector<int>> lift;
vector<int> depth;
int c = 0;
int finde(int i)
{
if (i != rep[i]) rep[i] = finde(rep[i]);
return rep[i];
}
int finde2(int i)
{
if (i != comp[i]) comp[i] = finde2(comp[i]);
return comp[i];
}
void dfs(int i, int d)
{
num[i] = ++c;
depth[i] = d;
for (int ele:graph[i])
{
if (num[ele]) continue;
lift[0][ele] = i;
dfs(ele, d+1);
}
}
void dfs2(int i)
{
num[i] = low[i] = ++c;
low[i] = complow[finde2(i)];
for (int ele:graph[i])
{
if (num[ele]) continue;
dfs2(ele);
if (low[ele]==num[ele]) bridges.push_back({i, ele});
low[i] = min(low[i], low[ele]);
}
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n, m;
cin >> n >> m;
num.assign(n, 0);
low.assign(n, 0);
rep.assign(n, 0);
comp.assign(n, 0);
complow.assign(n, 1e9);
graph.assign(n, vector<int>());
iota(rep.begin(), rep.end(), 0);
iota(comp.begin(), comp.end(), 0);
for (int i = 0; i < m; i++)
{
int a, b;
cin >> a >> b;
a--; b--;
if (finde(a)!= finde(b))
{
rep[finde(a)] = finde(b);
graph[a].push_back(b);
graph[b].push_back(a);
}
else
{
if (finde2(a) != finde2(b))
{
comp[finde2(a)] = finde2(b);
}
}
}
lift = vector<vector<int>>(18, vector<int>(n, -1));
depth.assign(n, 0);
for (int i = 0; i < n; i++)
{
c = 0;
if (!num[i]) dfs(i, 0);
}
for (int i = 1; i < 18; i++)
{
for (int j = 0; j < n; j++)
{
if (lift[i-1][j]==-1) continue;
lift[i][j] = lift[i-1][lift[i-1][j]];
}
}
for (int i =0; i < n; i++)
{
if (complow[finde2(i)]==1e9) complow[finde2(i)]=i;
else
{
int a = complow[finde2(i)];
int b = i;
if (depth[b]>depth[a])swap(a, b);
int diff = depth[a]-depth[b];
for (int i = 17; i >= 0; i--)
{
if (diff < (1<<i)) continue;
diff -= 1<<i;
a = lift[i][a];
}
if (a ==b)
{
complow[finde2(i)] = a;
continue;
}
for (int i = 17; i >= 0; i--)
{
if (lift[i][a]==lift[i][b]) continue;
a = lift[i][a];
b = lift[i][b];
}
a = lift[0][a];
complow[finde2(i)] = a;
}
}
for (int i = 0; i < n; i++)
{
if (complow[i]==1e9) continue;
complow[i] = num[complow[i]];
}
num.assign(n, 0);
for (int i =0; i < n; i++)
{
c = 0;
if (!num[i]) dfs2(i);
}
for (pii ele:bridges) cout << ele.first+1 << " " << ele.second+1 << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1372 KB |
Output is correct |
2 |
Correct |
6 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
199 ms |
6228 KB |
Output is correct |
2 |
Correct |
195 ms |
6164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
341 ms |
11348 KB |
Output is correct |
2 |
Correct |
417 ms |
13140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
567 ms |
20360 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
745 ms |
31388 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1488 ms |
45508 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1575 ms |
59088 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1976 ms |
65536 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2368 ms |
65536 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |