#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cassert>
using namespace std;
const int maxn=1e5+1;
vector < int > ms[maxn];
int gore[maxn];
int dep[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){
parent[find(x)]=find(y);
}
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;
void lca(int x, int y){
while(x!=y){
x=U1.find(x);
y=U1.find(y);
if(x==y){
break;
}
if(dep[x]>dep[y]){
U1.update(x, gore[x]);
}
else{
U1.update(y, gore[y]);
}
}
}
int novi[maxn];
void dfs(int x, int prosl){
gore[x]=prosl;
dep[x]=dep[prosl]+1;
novi[U1.find(x)]=0;
for(int i=0; i<(int)ms[x].size(); i++){
if(ms[x][i]!=prosl){
dfs(ms[x][i], x);
}
}
}
void rokaj(int x, int prosl){
if(!novi[U1.find(x)]){
novi[U1.find(x)]=x;
}
U1.parent[x]=novi[U1.parent[x]];
for(int i=0; i<(int)ms[x].size(); i++){
if(ms[x][i]!=prosl){
rokaj(ms[x][i], x);
}
}
}
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[a].push_back(b);
ms[b].push_back(a);
if(U2.sz[U2.find(a)]>U2.sz[U2.find(b)]){
swap(a, b);
}
dfs(a, b);
rokaj(a, b);
U2.update(a, b);
}
else if(!U1.query(a, b)){
lca(a, b);
}
// cout << "na " << i << endl;
}
for(int i=1; i<=n; i++){
if(gore[i] && !U1.query(i, gore[i])){
printf("%d %d\n", i, gore[i]);
}
}
return 0;
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:116:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:119:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
119 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3788 KB |
Output is correct |
2 |
Correct |
2 ms |
3788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3916 KB |
Output is correct |
2 |
Correct |
7 ms |
3916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
157 ms |
3916 KB |
Output is correct |
2 |
Correct |
130 ms |
3940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
226 ms |
4172 KB |
Output is correct |
2 |
Correct |
261 ms |
4172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
358 ms |
4800 KB |
Output is correct |
2 |
Correct |
315 ms |
5080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
467 ms |
6820 KB |
Output is correct |
2 |
Correct |
446 ms |
10312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
747 ms |
7236 KB |
Output is correct |
2 |
Runtime error |
757 ms |
23404 KB |
Memory limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
979 ms |
8028 KB |
Output is correct |
2 |
Correct |
962 ms |
12900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1197 ms |
8132 KB |
Output is correct |
2 |
Correct |
1229 ms |
13916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1472 ms |
7860 KB |
Output is correct |
2 |
Correct |
1489 ms |
14988 KB |
Output is correct |