#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+1 << " " << ele+1 << "\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);
int a, b;
for (int i = 0; i < m; i++)
{
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 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1116 KB |
Output is correct |
2 |
Correct |
3 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
712 KB |
Output is correct |
2 |
Correct |
64 ms |
688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
1872 KB |
Output is correct |
2 |
Correct |
144 ms |
1872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
203 ms |
3748 KB |
Output is correct |
2 |
Correct |
171 ms |
4684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
308 ms |
10812 KB |
Output is correct |
2 |
Correct |
249 ms |
10760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
419 ms |
12368 KB |
Output is correct |
2 |
Correct |
414 ms |
12244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
552 ms |
15124 KB |
Output is correct |
2 |
Correct |
536 ms |
15136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
685 ms |
15128 KB |
Output is correct |
2 |
Runtime error |
647 ms |
17708 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
834 ms |
14372 KB |
Output is correct |
2 |
Runtime error |
796 ms |
32172 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |