#include <bits/stdc++.h>
using namespace std;
#define vt vector
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
template<class T> bool chmin(T& a, const T& b) {
return b<a?a=b, 1:0;
}
template<class T> bool chmax(T& a, const T& b) {
return a<b?a=b, 1:0;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng_64(chrono::steady_clock::now().time_since_epoch().count());
//#pragma GCC optimize("O3,unroll-loops")
/**
* Author: Lukas Polacek
* Date: 2009-10-26
* License: CC0
* Source: folklore
* Description: Disjoint-set data structure.
* Time: $O(\alpha(N))$
*/
const int N = 100'010;
namespace d1 {
int e[N];
int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); }
bool sameSet(int a, int b) { return find(a) == find(b); }
bool join(int a, int b) {
a = find(a), b = find(b);
if (a == b) return false;
if (e[a] > e[b]) swap(a, b);
e[a] += e[b]; e[b] = a;
return true;
}
};
namespace d2 {
int e[N];
int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); }
bool sameSet(int a, int b) { return find(a) == find(b); }
bool join(int a, int b) {
a = find(a), b = find(b);
if (a == b) return false;
if (e[a] > e[b]) swap(a, b);
e[a] += e[b]; e[b] = a;
return true;
}
};
vt<int> g[N+1];
int low[N+1], id[N+1];
int timer=0;
void dfs(int &at, int &par) {
bool ck=0;
id[at]=low[at]=++timer;
for(int &to:g[at]) {
if(to == par and ck==0) {
ck=1;
continue;
}
if(id[to]) {
low[at]=min(low[at], id[to]);
} else {
dfs(to, at);
low[at]=min(low[at], low[to]);
}
}
if(low[at] == id[at] and at!=par) {
cout << at << " " << par << "\n";
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int n, m;
cin >> n >> m;
int u, v;
memset(d1::e, -1, sizeof d1::e);
memset(d2::e, -1, sizeof d2::e);
rep(i, 0, m) {
cin >> u >> v;
if(d1::join(u, v) || d2::join(u, v)) {
g[u].pb(v);
g[v].pb(u);
}
}
for(int i= 1;i<=n;i++) {
if(!id[i])
dfs(i, i);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3420 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3932 KB |
Output is correct |
2 |
Correct |
3 ms |
3724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
3920 KB |
Output is correct |
2 |
Correct |
54 ms |
3732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
4444 KB |
Output is correct |
2 |
Correct |
114 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
5840 KB |
Output is correct |
2 |
Runtime error |
270 ms |
19264 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
245 ms |
10320 KB |
Output is correct |
2 |
Runtime error |
195 ms |
25460 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
342 ms |
11092 KB |
Output is correct |
2 |
Runtime error |
346 ms |
41972 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
416 ms |
13136 KB |
Output is correct |
2 |
Runtime error |
437 ms |
50312 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
558 ms |
13136 KB |
Output is correct |
2 |
Runtime error |
522 ms |
61776 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
691 ms |
23096 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |