#include <bits/stdc++.h>
using namespace std;
#define bug(x) cout << #x << " " << x << endl;
const int maxn = 1e5 + 10;
vector<int> v, pai( maxn ), tam( maxn, 1 ), tempo( maxn, -1 );
vector<pair< int, int >> edge( maxn, pair< int, int>( -1, -1 ) );
int find( int a ){ return (( a == pai[a] ) ? a : find(pai[a]) ); }
bool query( int a, int b ){
v.clear();
while( (pai[a] != a || pai[b] != b) && a != b ){
if( tempo[a] > tempo[b] ){
if( edge[a] != make_pair( -1, -1 ) ) v.push_back(a);
a = pai[a];
}
else{
if( edge[b] != make_pair( -1, -1 ) ) v.push_back(b);
b = pai[b];
}
}
return ( a == b );
}
void join( int a, int b, int t ){
int _a = find(a);
int _b = find(b);
if( tam[_a] < tam[_b] ) swap( _a, _b );
tam[_a] += tam[_b];
pai[_b] = _a;
tempo[_b] = t;
edge[_b] = make_pair(a, b);
}
int main(){
int n, m; cin >> n >> m;
for( int i = 1; i <= n; i++ ) pai[i] = i;
while( m-- ){
int a, b; cin >> a >> b;
if( !query( a, b ) ) join( a, b, m + 10 );
else for( int x : v ) edge[x] = make_pair( -1, -1 );
}
for( int i = 1; i <= n; i++ ){
if( edge[i] != make_pair( -1, -1 ) ) cout << edge[i].first << " " << edge[i].second << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
2396 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
222 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
218 ms |
5460 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
378 ms |
2396 KB |
Output is correct |
2 |
Correct |
458 ms |
8532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
594 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
533 ms |
9684 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
783 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
742 ms |
11612 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1278 ms |
2396 KB |
Output is correct |
2 |
Runtime error |
1325 ms |
36096 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1669 ms |
2396 KB |
Output is correct |
2 |
Runtime error |
1614 ms |
43644 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2044 ms |
2396 KB |
Output is correct |
2 |
Runtime error |
2037 ms |
55184 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2706 ms |
2396 KB |
Output is correct |
2 |
Runtime error |
2543 ms |
65536 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |