#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
vector<vector<int>> graph;
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])
{
cout << i << " " << ele << "\n";
}
low[i] = min(low[i], low[ele]);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//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);
}
}
}
rep.clear();
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);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Mismatched edge |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
1116 KB |
Mismatched edge |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
956 KB |
Output is correct |
2 |
Incorrect |
66 ms |
852 KB |
Mismatched edge |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
1672 KB |
Output is correct |
2 |
Incorrect |
130 ms |
1620 KB |
Mismatched edge |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
191 ms |
3748 KB |
Output is correct |
2 |
Runtime error |
175 ms |
18544 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
275 ms |
10768 KB |
Output is correct |
2 |
Runtime error |
244 ms |
29012 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
428 ms |
12376 KB |
Output is correct |
2 |
Runtime error |
422 ms |
45600 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
546 ms |
15188 KB |
Output is correct |
2 |
Runtime error |
546 ms |
56644 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
675 ms |
20428 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
813 ms |
32528 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |