// IN THE NAME OF GOD
#include<bits/stdc++.h>
//#pragma GCC optimize ("O2,unroll-loops")
#define endl '\n';
#define file_reading freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
using namespace std;
typedef long long ll;
typedef long double dll;
typedef unsigned long long ull;
// Variables
const int B = 100000 + 7;
int par1[B];
int par2[B];
int n,m,a,b,one,two;
vector<int> g[B];
bool seen[B];
int father[B];
int dist[B];
int val[B];
// main functions
int get_root1(int v){
return (par1[v] == v ? v : par1[v] = get_root1(par1[v]));
}
int get_root2(int v){
return (par2[v] == v ? v : par2[v] = get_root2(par2[v]));
}
bool merge1(int v, int u){
one = get_root1(v);
two = get_root1(u);
if (one == two){return 0;}
par1[one] = two;
return 1;
}
bool merge2(int v, int u){
one = get_root2(v);
two = get_root2(u);
if (one == two){return 0;}
par2[one] = two;
return 1;
}
void dfs(int vv){
seen[vv] = 1;
for(auto u : g[vv]){
if (seen[u] == 0){
father[u] = vv;
dist[u] = dist[vv] + 1;
dfs(u);
val[vv] += val[u];
if (val[u] == 0){cout << vv << " " << u << endl;}
}
else if (father[u] != vv && father[vv] != u && dist[u] < dist[vv]){
val[u]--;
val[vv]++;
}
}
}
int32_t main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i=1; i<=n; i++){par1[i] = par2[i] = i;}
for(int i=1; i<=m; i++){
cin >> a >> b;
if (merge1(a,b)){
g[a].push_back(b);
g[b].push_back(a);
}
else if (merge2(a,b)){
g[a].push_back(b);
g[b].push_back(a);
}
}
for(int i=1; i<=n; i++){
if (seen[i] == 0){
father[i] = 0;
dist[i] = 0;
dfs(i);
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2644 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3228 KB |
Output is correct |
2 |
Incorrect |
5 ms |
2900 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
3100 KB |
Output is correct |
2 |
Incorrect |
79 ms |
2836 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
139 ms |
3776 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
228 ms |
5580 KB |
Output is correct |
2 |
Correct |
197 ms |
5048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
316 ms |
10908 KB |
Output is correct |
2 |
Incorrect |
330 ms |
7440 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
488 ms |
12108 KB |
Output is correct |
2 |
Incorrect |
447 ms |
9336 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
641 ms |
14396 KB |
Output is correct |
2 |
Incorrect |
611 ms |
9524 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
812 ms |
14352 KB |
Output is correct |
2 |
Incorrect |
734 ms |
9548 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
944 ms |
13860 KB |
Output is correct |
2 |
Correct |
880 ms |
11100 KB |
Output is correct |