// 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,u;
vector<pair<int,int>> g[B];
bitset<B> seen;
bool ans[BB];
pair<int,int> dat[B];
int father[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;}
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;
vector<pair<int,int>> check;
for(auto cur : g[v]){
u = cur.first;
if (seen[u] == 0){
father[u] = v;
check.push_back(cur);
dfs(u);
}
else if (father[u] != v && father[v] != u){
dat[v].first++;
dat[u].second++;
}
}
for(auto cur : check){
u = cur.first;
if (val[u] == 0){ans[cur.second] = 1;}
dat[v].first += dat[u].first;
dat[v].second += dat[u].second;
}
val[v] = dat[v].first - dat[v].second;
}
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;
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 |
1 ms |
2644 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3540 KB |
Output is correct |
2 |
Incorrect |
4 ms |
3156 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
7960 KB |
Output is correct |
2 |
Incorrect |
79 ms |
7608 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
134 ms |
12356 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
231 ms |
18756 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
332 ms |
29724 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
501 ms |
40076 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
637 ms |
50880 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
798 ms |
58684 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
921 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |