#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], in[maxn], low[maxn];
bool vis[maxn];
int findp(int x){
if(par[x]==x)return x;
else return par[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){
if(par2[x]==x)return x;
else return par2[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];
int cur;
void dfs(int u,int p){
vis[u] = 1, low[u] = in[u] = ++cur;
bool flag = 1;
for(auto v:g[u]){
if(v==p&&flag){
flag = 0;continue;
}else if(vis[v]){
ckmin(low[u],in[v]);
}else{
dfs(v,u);
ckmin(low[u],low[v]);
if(low[v]>in[u])cout<<v<<" "<<u<<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:29:9: warning: unused variable 'a' [-Wunused-variable]
29 | int a = x, b = y;
| ^
pipes.cpp:29:16: warning: unused variable 'b' [-Wunused-variable]
29 | int a = x, b = y;
| ^
pipes.cpp: In function 'bool merge2(int, int)':
pipes.cpp:43:9: warning: unused variable 'a' [-Wunused-variable]
43 | int a = x, b = y;
| ^
pipes.cpp:43:16: warning: unused variable 'b' [-Wunused-variable]
43 | int a = x, b = y;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3276 KB |
Output is correct |
2 |
Correct |
7 ms |
3012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
3176 KB |
Output is correct |
2 |
Correct |
98 ms |
3060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
188 ms |
4156 KB |
Output is correct |
2 |
Correct |
199 ms |
3788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
261 ms |
6120 KB |
Output is correct |
2 |
Correct |
202 ms |
5828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
317 ms |
12120 KB |
Output is correct |
2 |
Correct |
278 ms |
7888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
558 ms |
13740 KB |
Output is correct |
2 |
Correct |
453 ms |
9944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
648 ms |
15856 KB |
Output is correct |
2 |
Correct |
630 ms |
10048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
877 ms |
15908 KB |
Output is correct |
2 |
Correct |
817 ms |
10056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1064 ms |
15496 KB |
Output is correct |
2 |
Runtime error |
1059 ms |
65536 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |