This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;
typedef pair<int,int> pii;
#define pb push_back
#define rep(i,a,b) for(int i = a; i < b; i++)
int n, m;
vi depth, back_edge;
vvi adj;
vi parent;
vvi children;
vector<pii> ans;
void dfs(int x, int cf) {
parent[x] = cf;
for (auto y : adj[x]) {
if (y == parent[x]) continue;
if (depth[y] != -1) {
back_edge[x] = min(back_edge[x], depth[y]);
continue;
}
children[x].pb(y);
back_edge[y] = depth[y] = depth[x] + 1;
dfs(y, x);
}
}
void dfs2(int x) {
for (auto & y : children[x]) dfs2(y);
for (auto & y : children[x]) back_edge[x] = min(back_edge[x], back_edge[y]);
if (depth[x] != 0) if (back_edge[x] >= depth[x]) ans.pb({x+1, parent[x]+1});
}
int main() {
cin >> n >> m;
adj.resize(n); depth.assign(n,-1); back_edge.assign(n,n);
children.resize(n); parent.assign(n,-1);
rep(i,0,m) {
int a, b;
cin >> a >> b;a--;b--;
adj[a].pb(b);
adj[b].pb(a);
}
rep(i,0,n) {
if (depth[i] != -1) continue;
depth[i] = back_edge[i] = 0;
dfs(i, -1);
dfs2(i);
}
cout << ans.size() << endl;
for (auto [x, y] : ans) {
cout << x << " " << y << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |