#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cassert>
using namespace std;
const int maxn=1e5+5;
set < pair < int, int > > ms[maxn];
struct union_find{
int parent[maxn];
union_find(){
for(int i=0; i<maxn; i++){
parent[i]=i;
}
}
int find(int x){
if(parent[x]==x){
return x;
}
return parent[x]=find(parent[x]);
}
void update(int x, int y){
x=find(x);
y=find(y);
if(ms[x].size()>ms[y].size()){
swap(x, y);
}
parent[x]=y;
pair < int, int > p;
while(!ms[x].empty()){
ms[y].insert(*ms[x].begin());
ms[x].erase(ms[x].begin());
}
}
bool query(int x, int y){
return find(x)==find(y);
}
};
struct union_find2{
int parent[maxn];
int sz[maxn];
union_find2(){
for(int i=0; i<maxn; i++){
parent[i]=i;
sz[i]=1;
}
}
int find(int x){
if(x==parent[x]){
return x;
}
return parent[x]=find(parent[x]);
}
void update(int x, int y){
x=find(x);
y=find(y);
if(sz[x]>sz[y]){
swap(x, y);
}
parent[x]=y;
sz[y]+=sz[x];
}
bool query(int x, int y){
return find(x)==find(y);
}
};
union_find U1;
union_find2 U2;
vector < int > put;
vector < pair < int, int > > edge;
bool bio[maxn];
bool dfs(int x, int prosl){
bool p=0;
// cout << x << endl;
if(bio[x]){
int ind1, ind2;
for(int i=0; i<(int)put.size(); i++){
if(put[i]==x){
p=1;
ind1=i;
ind2=put.size()-1;
// cout << "rez " << ms[put[i]].size() << ' ';
ms[put[i]].erase(edge[ind1]);
ms[put[i]].erase({edge[ind2].second, edge[ind2].first});
// cout << ms[put[i]].size() << '\n';
}
else if(p){
ind1=i;
ind2=i-1;
// cout << "rez " << ms[put[i]].size() << ' ';
ms[put[i]].erase(edge[ind1]);
ms[put[i]].erase({edge[ind2].second, edge[ind2].first});
// cout << ms[put[i]].size() << '\n';
// cout << "upd " << put[i] << ' ' << put[i-1] << endl;
U1.update(put[i], put[i-1]);
}
}
return 1;
}
bio[x]=1;
put.push_back(x);
for(auto i=ms[x].begin(); i!=ms[x].end(); i++){
if(!p && U1.find(i->second)==prosl){
p=1;
}
else{
edge.push_back(*i);
if(dfs(U1.find(i->second), x)){
bio[x]=0;
put.pop_back();
edge.pop_back();
return 1;
}
edge.pop_back();
}
}
put.pop_back();
bio[x]=0;
return 0;
}
int main(){
int n, m;
scanf("%d%d", &n, &m);
int a, b;
for(int i=0; i<m; i++){
scanf("%d%d", &a, &b);
if(!U2.query(a, b)){
ms[U1.find(a)].insert({a, b});
ms[U1.find(b)].insert({b, a});
U2.update(a, b);
}
else if(!U1.query(a, b)){
ms[U1.find(a)].insert({a, b});
ms[U1.find(b)].insert({b, a});
assert(dfs(U1.find(a), -1));
}
// cout << "na " << i << endl;
}
for(int i=1; i<=n; i++){
for(auto j=ms[i].begin(); j!=ms[i].end(); j++){
if(j->first<j->second){
printf("%d %d\n", j->first, j->second);
}
}
}
return 0;
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:135:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:138:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
13 ms |
12364 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
6404 KB |
Output is correct |
2 |
Runtime error |
10 ms |
12352 KB |
Execution killed with signal 6 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
203 ms |
6252 KB |
Output is correct |
2 |
Runtime error |
11 ms |
12512 KB |
Execution killed with signal 6 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
332 ms |
13320 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2885 ms |
7372 KB |
Output is correct |
2 |
Runtime error |
16 ms |
14156 KB |
Execution killed with signal 6 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5074 ms |
10448 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
33 ms |
16716 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
37 ms |
19424 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
41 ms |
19368 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
425 ms |
22792 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |