#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[rep[a]] = rep[b];
graph[a].push_back(b);
graph[b].push_back(a);
}
else
{
if (finde2(a) != finde2(b))
{
comp[comp[a]] = comp[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 |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1032 KB |
Output is correct |
2 |
Correct |
4 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
852 KB |
Output is correct |
2 |
Correct |
69 ms |
848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
1872 KB |
Output is correct |
2 |
Correct |
139 ms |
1616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
3756 KB |
Output is correct |
2 |
Correct |
164 ms |
4676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
286 ms |
10692 KB |
Output is correct |
2 |
Correct |
240 ms |
10764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
445 ms |
12336 KB |
Output is correct |
2 |
Correct |
441 ms |
12184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
597 ms |
15296 KB |
Output is correct |
2 |
Correct |
546 ms |
15188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
724 ms |
15224 KB |
Output is correct |
2 |
Correct |
687 ms |
15152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
873 ms |
14368 KB |
Output is correct |
2 |
Correct |
791 ms |
15216 KB |
Output is correct |