#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#pragma GCC target ("avx,tune=native")
//Use above if bruteforcing with lots of small operations. Or just use it anytime, there's no downside. AVX is better slightly
/*
TASK: hidden
LANG: C++11
*/
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> pair2;
typedef pair<int, pair<int, int> > pair3;
typedef pair<int, pair<int, pair<int, int> > > pair4;
#define MAXN 100013
#define INF 1000000000000000000LL
#define mp make_pair
#define add push_back
#define remove pop
int n, m;
vector<int> adj[MAXN];
//vector<pair<int, int>> answer;
int depth[MAXN], lowestAdjDepth[MAXN];
struct UnionFind {
vector<int> id;
UnionFind(int sz) {
id = vector<int>(sz + 1);
for (int i = 0; i < sz; i++) {
id[i] = i;
}
}
UnionFind(){}
bool sameset(int p, int q) {
return find(p) == find(q);
}
int find(int p) {
if (p != id[p]) {
id[p] = find(id[p]);
}
return id[p];
}
void merge(int p, int q) {
int rootp = find(p);
int rootq = find(q);
if (rootp == rootq) return;
id[rootp] = rootq;
}
void erase() {
id.clear();
id.shrink_to_fit();
}
} uf[2];
//returns subtree size
void solve(int current, int parent) {
assert(depth[current] >= 1);
lowestAdjDepth[current] = depth[current];
//cout << "solving " << current << " lad dis " << lowestAdjDepth[current] << endl;
bool t = false;
for (int next : adj[current]) {
if (next == parent && !t) {t = true; continue;}
if (depth[next] >= 1) {
lowestAdjDepth[current] = min(lowestAdjDepth[current], depth[next]);
} else {
//unvisited, let's go visit!
depth[next] = depth[current] + 1;
solve(next, current);
lowestAdjDepth[current] = min(lowestAdjDepth[current], lowestAdjDepth[next]);
//cout << "current is " << current << " lad next is " << lowestAdjDepth[next] << " depth current is " << depth[current] << endl;
if (lowestAdjDepth[next] > depth[current]) {
//the next node is in a biconnected component unaffected by current node
//answer.add(mp(current, next));
cout << current + 1 << ' ' << next + 1 << '\n';
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
uf[0] = UnionFind(n);
uf[1] = UnionFind(n);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;b--;
if (!uf[0].sameset(a, b)) {
adj[a].add(b);
adj[b].add(a);
uf[0].merge(a, b);
} else if (!uf[1].sameset(a, b)) {
adj[a].add(b);
adj[b].add(a);
uf[1].merge(a, b);
}
}
uf[0].erase();
uf[1].erase();
for (int i = 0; i < n; i++) {
if (depth[i] == 0) {
depth[i] = 1;
solve(i, -1);
}
}
/*
for (auto t : answer) {
cout << t.first + 1 << ' ' << t.second + 1<< endl;
}*/
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2660 KB |
Output is correct |
2 |
Correct |
3 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
3248 KB |
Output is correct |
2 |
Correct |
7 ms |
2944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
100 ms |
3040 KB |
Output is correct |
2 |
Correct |
96 ms |
2896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
163 ms |
3856 KB |
Output is correct |
2 |
Correct |
188 ms |
3312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
282 ms |
5416 KB |
Output is correct |
2 |
Correct |
250 ms |
5156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
462 ms |
10192 KB |
Output is correct |
2 |
Correct |
378 ms |
6596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
645 ms |
11212 KB |
Output is correct |
2 |
Correct |
599 ms |
8476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
838 ms |
13320 KB |
Output is correct |
2 |
Correct |
897 ms |
8428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1094 ms |
13264 KB |
Output is correct |
2 |
Correct |
994 ms |
8308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1239 ms |
12756 KB |
Output is correct |
2 |
Correct |
1188 ms |
9876 KB |
Output is correct |