#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define F first
#define S second
#define pb push_back
#define endl '\n'
#define Mp make_pair
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " = " << x << endl
#define set_dec(x) cout << fixed << setprecision(x);
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
ll mod = 1e9+7;
ll power(ll a, ll b)
{
return (!b ? 1 : (b & 1 ? a * power(a * a % mod, b / 2) % mod : power(a * a % mod, b / 2) % mod));
}
const int N= 100023;
vector<int> g[N] ;
int mn[N] , h[N] , par1[N] , par2[N] , vis[N], sz1[N] , sz2[N] ;
int getPar1(int v){
return (par1[v]==-1)?v: getPar1(par1[v]);
}
int getPar2(int v){
return (par2[v]==-1)? v: getPar2(par2[v]);
}
bool Union1(int v, int u){
v= getPar1(v); u = getPar1(u);
if(u==v) return 0;
if(sz1[u]> sz1[v]) swap(u,v);
sz1[v]+=sz1 [u];
par1[u]=v;
return 1;
}
bool Union2(int v, int u){
v= getPar2(v); u = getPar2(u);
if(u==v) return 0;
if(sz2[u]> sz2[v]) swap(u,v);
sz2[v]+=sz2[u];
par2[u]=v;
return 1;
}
void dfs(int v, int p ){
vis[v]=1;
mn[v]=h[v];
for(int u: g[v]){
if(u==p) continue;
if(!vis[u]){
h[u]= h[v]+1;
dfs(u,v);
mn[v]= min (mn[v], mn[u]);
}
else{
mn[v]= min(mn[v] , h[u]);
}
}
if(p && mn[v]==h[v]) cout<<p<<" "<<v<<endl;
}
int main(){
fast_io
int n , m; cin>>n>>m;
for(int i=1; i<=n; i++){
par1[i]=-1;
par2[i]=-1;
sz1[i]=1;
sz2[i]=1;
}
for(int i=1 ;i<=m; i++){
int u ,v; cin>>v>>u;
if(Union1(v,u)){
g[v].pb(u); g[u].pb(v);
}
else if(Union2(u,v)){
g[v].pb(u); g[u].pb(v);
}
}
for(int i=1; i<=n; i++) if(!vis[i]) dfs(i,0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3156 KB |
Output is correct |
2 |
Incorrect |
5 ms |
3028 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
2996 KB |
Output is correct |
2 |
Incorrect |
106 ms |
2856 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
179 ms |
3848 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
330 ms |
5540 KB |
Output is correct |
2 |
Runtime error |
281 ms |
19148 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
440 ms |
11504 KB |
Output is correct |
2 |
Runtime error |
402 ms |
26096 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
673 ms |
12680 KB |
Output is correct |
2 |
Runtime error |
664 ms |
43196 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
875 ms |
15128 KB |
Output is correct |
2 |
Runtime error |
874 ms |
51656 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1085 ms |
18436 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1430 ms |
30696 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |