#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <array>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define FOR(i,start,end) for(int i=start;i<(int)(end);i++)
#define FORE(i,start,end) for(int i=start;i<=(int)end;i++)
#define RFOR(i,start,end) for(int i = start; i>end; i--)
#define RFORE(i,start,end) for(int i = start; i>=end; i--)
#define all(a) a.begin(), a.end()
#define mt make_tuple
#define mp make_pair
#define v vector
#define sf scanf
#define pf printf
#define dvar(x) cout << #x << " := " << x << "\n"
#define darr(x,n) FOR(i,0,n) cout << #x << "[" << i << "]" << " := " << x[i] << "\n"
typedef long long ll;
typedef long double ld;
typedef pair<int, int > pii;
typedef pair<ll, ll> pll;
//template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> void minn(T &a, T b) { a = min(a, b); }
template<class T> void maxx(T &a, T b) { a = max(a, b); }
void io() {
#ifdef LOCAL_PROJECT
freopen("input.in", "r", stdin); freopen("output.out", "w", stdout);
#else
#endif
ios_base::sync_with_stdio(false); cin.tie(NULL);
}
const ll INF = 1e14;
/***********************CEOI 2015 D1 Pipes*****************************************/
template<int NSZ, int ESZ> struct BCC {
int NN, MM; // Nodes, Edges
void init(int n, int m) {
NN = n, MM = m;
}
v<pii> adj[NSZ];
void addEdge(int a, int b, int id) {
adj[a].push_back({ b,id }), adj[b].push_back({ a,id });
}
int lo[NSZ], ind[NSZ], t = 0;
void tarjan(int cur, int par = -1) {
ind[cur] = lo[cur] = t++;
for (pii yi : adj[cur]) {
int nex = yi.first, id = yi.second;
if (id == par) continue;
if (ind[nex] == -1) {
tarjan(nex, id);
lo[cur] = min(lo[cur], lo[nex]);
if (lo[nex] > ind[cur]) {
cout << cur << " " << nex << "\n";
}
}
else lo[cur] = min(lo[cur], ind[nex]);
}
}
void findBridges() {
memset(ind, -1, sizeof ind);
memset(lo, 0, sizeof lo);
FOR(i, 0, NN) if (ind[i] == -1)
tarjan(i);
}
};
template <int SZ> struct UnionFind {
int par[SZ];
void init(int NN) {
FOR(i, 0, NN) par[i] = i;
}
int find(int x) {
if (par[x] != x) par[x] = find(par[x]);
return par[x];
}
bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) return false;
par[y] = x;
return true;
}
};
int n, m, esz = 0;
UnionFind<100001> uf1, uf2;
BCC<100001, 200001> bcc;
int main() {
io();
cin >> n >> m;
uf1.init(n + 1), uf2.init(n + 1);
FOR(i, 0, m) {
int a, b; cin >> a >> b;
if (uf1.unite(a, b) || uf2.unite(a, b)) {
bcc.addEdge(a, b, esz++);
// cout << a << " " << b << " - \n";
}
}
assert(esz < 2 * n + 1);
bcc.init(n + 1, esz);
bcc.findBridges();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3456 KB |
Output is correct |
2 |
Correct |
4 ms |
3456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
3968 KB |
Output is correct |
2 |
Correct |
8 ms |
3840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
3900 KB |
Output is correct |
2 |
Correct |
93 ms |
3704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
4452 KB |
Output is correct |
2 |
Correct |
183 ms |
4176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
279 ms |
5896 KB |
Output is correct |
2 |
Correct |
247 ms |
5532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
417 ms |
10580 KB |
Output is correct |
2 |
Correct |
362 ms |
8572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
628 ms |
11680 KB |
Output is correct |
2 |
Correct |
608 ms |
9088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
887 ms |
13592 KB |
Output is correct |
2 |
Correct |
836 ms |
10808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1018 ms |
13600 KB |
Output is correct |
2 |
Correct |
971 ms |
10664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1241 ms |
12988 KB |
Output is correct |
2 |
Correct |
1168 ms |
10292 KB |
Output is correct |