#include <bits/stdc++.h>
using namespace std;
int parent[100000];
int depth[100000];
int root[100000];
int union_parent[100000];
vector<vector<int>> children;
int find(int a)
{
if (union_parent[a] == a)
return a;
return union_parent[a] = find(union_parent[a]);
}
void unite(int a, int b)
{
a = find(a);
b = find(b);
// if (rand() % 2)
// swap(a, b);
union_parent[a] = b;
}
vector<vector<int>> adj;
void dfs_reconstruct(int i, int d, int p, int r)
{
root[i] = r;
parent[i] = r;
depth[i] = d;
for (int c : adj[i])
{
if (c == p)
continue;
dfs_reconstruct(c, d + 1, i, r);
}
}
signed main()
{
cin.tie(0);
ios_base::sync_with_stdio(false);
int n, m;
cin >> n >> m;
children.resize(n);
adj.resize(n);
for (int i = 0; i < n; i++)
{
parent[i] = root[i] = union_parent[i] = i;
children[i] = vector<int>{i};
depth[i] = 1;
}
while (m--)
{
int a, b;
cin >> a >> b;
a--, b--;
a = find(a);
b = find(b);
if (root[a] == root[b])
{
a = find(a);
b = find(b);
while (a != b)
{
if (depth[a] < depth[b])
swap(a, b);
unite(a, parent[a]);
a = find(a);
b = find(b);
}
}
else
{
if (children[root[a]].size() < children[root[b]].size())
swap(a, b);
vector<pair<int, int>> inserts;
vector<int> new_nodes;
for (int i : children[root[b]])
{
i = find(i);
int p = find(parent[i]);
new_nodes.push_back(i);
if (i == p)
continue;
inserts.push_back({i, p});
inserts.push_back({p, i});
}
sort(inserts.begin(), inserts.end());
inserts.erase(unique(inserts.begin(), inserts.end()), inserts.end());
sort(new_nodes.begin(), new_nodes.end());
new_nodes.erase(unique(new_nodes.begin(), new_nodes.end()), new_nodes.end());
for (int node : new_nodes)
{
children[root[a]].push_back(node);
}
for (auto [a, b] : inserts)
{
adj[a].push_back(b);
}
dfs_reconstruct(b, depth[a] + 1, a, root[a]);
for (auto [a, b] : inserts)
{
adj[a].clear();
}
}
}
for (int i = 0; i < n; i++)
{
if (parent[i] != i && find(i) != find(parent[i]))
cout << i+1 << " " << parent[i]+1 << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
1116 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
6264 KB |
Output is correct |
2 |
Incorrect |
61 ms |
6144 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
11176 KB |
Output is correct |
2 |
Incorrect |
125 ms |
12888 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
178 ms |
19540 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
246 ms |
29880 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
366 ms |
43720 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
472 ms |
56776 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
605 ms |
65536 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
713 ms |
65536 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |