#include <bits/stdc++.h>
#define maxn (int)(1e5+51)
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define endl '\n'
#define ll long long
#define pb push_back
#define ull unsigned long long
#define ii pair<int,int>
#define iii tuple<int,int,int>
#define inf 2000000001
#define mod 1000000007 //998244353
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
template<typename X, typename Y> bool ckmin(X& x, const Y& y) { return (y < x) ? (x=y,1):0; }
template<typename X, typename Y> bool ckmax(X& x, const Y& y) { return (x < y) ? (x=y,1):0; }
int lvl[maxn], par[maxn], lvl2[maxn], par2[maxn], dp[maxn], lvl3[maxn];
bool vis[maxn];
int findp(int x){return par[x] = (x==par[x]?x:findp(par[x]));}
bool merge(int x,int y){
int a = x, b = y;
x = findp(x), y = findp(y);
if(x==y)return 0;
if(lvl[x]<lvl[y])swap(x,y);
par[y] = x;
lvl[x] += lvl[y]==lvl[x];
return 1;
}
int findp2(int x){return par2[x] = (x==par2[x]?x:findp2(par2[x]));}
bool merge2(int x,int y){
int a = x, b = y;
x = findp2(x), y = findp2(y);
if(x==y)return 0;
if(lvl2[x]<lvl2[y]){
swap(x,y);
}
par2[y] = x;
lvl2[x] += lvl2[y]==lvl2[x];
return 1;
}
vector<int>g[maxn];
void dfs(int u,int p){
vis[u] = 1;
for(auto v:g[u]){
if(lvl3[v]==0){
lvl3[v] = lvl3[u]+1;
dfs(v,u);
dp[u] += dp[v];
}else if(lvl3[v]<lvl3[u])dp[u]++;
else dp[u]--;
}
dp[u]--;
if(dp[u]==0)cout<<u<<" "<<p<<endl;
}
int main(){_
int n,m;cin>>n>>m;
for(int i=1;i<=n;++i)lvl[i] = lvl2[i] = 0, par[i] = par2[i] = i;
for(int i=0;i<m;++i){
int a,b;cin>>a>>b;
if(merge(a,b)){
g[a].pb(b);g[b].pb(a);
}else if(merge2(a,b)){
g[a].pb(b);g[b].pb(a);
}
}
for(int i=1;i<=n;++i)
if(!vis[i])dfs(i,i);
}
Compilation message
pipes.cpp: In function 'bool merge(int, int)':
pipes.cpp:25:9: warning: unused variable 'a' [-Wunused-variable]
25 | int a = x, b = y;
| ^
pipes.cpp:25:16: warning: unused variable 'b' [-Wunused-variable]
25 | int a = x, b = y;
| ^
pipes.cpp: In function 'bool merge2(int, int)':
pipes.cpp:36:9: warning: unused variable 'a' [-Wunused-variable]
36 | int a = x, b = y;
| ^
pipes.cpp:36:16: warning: unused variable 'b' [-Wunused-variable]
36 | int a = x, b = y;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
3328 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
3396 KB |
Output is correct |
2 |
Correct |
93 ms |
3144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
161 ms |
4264 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
280 ms |
5944 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
355 ms |
11692 KB |
Output is correct |
2 |
Correct |
332 ms |
7748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
582 ms |
13064 KB |
Output is correct |
2 |
Runtime error |
564 ms |
42904 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
757 ms |
14840 KB |
Output is correct |
2 |
Runtime error |
773 ms |
51448 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
934 ms |
14772 KB |
Output is correct |
2 |
Runtime error |
971 ms |
63760 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1109 ms |
19072 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |