// 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 = 1e5+7;
const int BB = 6e6+7;;
pair<int,int> v[BB];
int par1[B];
int siz1[B];
int par2[B];
int siz2[B];
int n,m,a,b,one,two;
vector<pair<int,int>> g[B];
bitset<B> seen;
int father[B];
int dist[B];
int val[B];
bool ans[BB];
// 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;}
if (siz1[one] > siz1[two]){swap(one,two);}
siz1[two] += siz1[one];
par1[one] = two;
return 1;
}
bool merge2(int v, int u){
one = get_root2(v);
two = get_root2(u);
if (one == two){return 0;}
if (siz2[one] > siz2[two]){swap(one,two);}
siz2[two] += siz2[one];
par2[one] = two;
return 1;
}
void dfs(int v){
seen[v] = 1;
for(auto cur : g[v]){
int u = cur.first;
if (seen[u] == 0){
father[u] = v;
dist[u] = dist[v] + 1;
dfs(u);
val[v] += val[u];
if (val[u] == 0){ans[cur.second] = 1;}
}
else if (father[u] != v && father[v] != u && dist[u] < dist[v]){
val[u]--;
val[v]++;
}
}
}
int32_t main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i=1; i<=n; i++){siz1[i] = siz2[i] = 1; par1[i] = par2[i] = i;}
for(int i=1; i<=m; i++){
cin >> v[i].first >> v[i].second;
if (merge1(v[i].first,v[i].second)){
g[v[i].first].push_back({v[i].second,i});
g[v[i].second].push_back({v[i].first,i});
}
else if (merge2(v[i].first,v[i].second)){
g[v[i].first].push_back({v[i].second,i});
g[v[i].second].push_back({v[i].first,i});
}
}
for(int i=1; i<=n; i++){
if (seen[i] == 0){
father[i] = 0;
dist[i] = 0;
dfs(i);
}
}
for(int i=1; i<=m; i++){
if (ans[i]){
cout << v[i].first << " " << v[i].second << endl;
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3284 KB |
Output is correct |
2 |
Incorrect |
5 ms |
3156 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
7756 KB |
Output is correct |
2 |
Incorrect |
85 ms |
7628 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
147 ms |
11728 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
257 ms |
17312 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
337 ms |
25736 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
530 ms |
35460 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
658 ms |
45116 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
819 ms |
52876 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1000 ms |
61632 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |