#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];
int findp(int x){return par[x] = (x==par[x]?x:findp(par[x]));}
bool samep(int x,int y){return findp(x)==findp(y);}
bool merge(int x,int y){
int a = x, b = y, flag=0;
x = findp(x), y = findp(y);
if(x==y)return 0;
if(lvl[x]<lvl[y]){
swap(x,y);
flag = 1;
}
par[y] = x;
lvl[x] += lvl[y]==lvl[x];
if(!flag)lvl2[b]=lvl2[a]+1;
else lvl2[a]=lvl2[b]+1;
return 1;
}
int main(){_
int n,m;cin>>n>>m;
for(int i=1;i<=n;++i)lvl[i] = 0, par[i] = i;
int ans = m;
for(int i=0;i<m;++i){
int a,b;cin>>a>>b;
if(!merge(a,b)){
int cur = a;
while(lvl2[cur]!=0){
--ans, lvl2[cur] = 0;
cur = par[cur];
}
cur = b;
while(lvl2[cur]!=0){
--ans, lvl2[cur] = 0;
cur = par[cur];
}
}
}
for(int i=1;i<=n;++i){
if(lvl2[i])cout<<i<<" "<<par[i]<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
480 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
5700 KB |
Output is correct |
2 |
Incorrect |
80 ms |
5572 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
142 ms |
9288 KB |
Output is correct |
2 |
Incorrect |
156 ms |
11668 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
236 ms |
9240 KB |
Output is correct |
2 |
Incorrect |
201 ms |
14740 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
288 ms |
9972 KB |
Output is correct |
2 |
Runtime error |
249 ms |
19292 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
503 ms |
24772 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
630 ms |
45224 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
777 ms |
56876 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
981 ms |
65536 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |