This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/** I can do this all day **/
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define Mp make_pair
#define SZ(x) (int)x.size()
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
const int N = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 8e18;
const int LOG = 22;
ll pw(ll a , ll b, ll M) { return (!b ? 1 : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M : pw(a * a % M, b / 2, M))); }
struct dsu
{
int par[N];
void init()
{
iota(par, par + N, 0);
}
int get(int x)
{
return (x == par[x]? x : par[x] = get(par[x]));
}
int unify(int v, int u)
{
v = get(v), u = get(u);
if(v == u)
{
return 0;
}
par[u] = v;
return 1;
}
} A, B;
int n, m, H[N], Up[N];
vector < pii > G[N];
void dfs(int v, int last)
{
Up[v] = H[v];
for(auto y : G[v])
{
int u = y.F, id = y.S;
if(id == last) continue;
if(H[u])
{
Up[v] = min(Up[v], H[u]);
}
else
{
H[u] = H[v] + 1;
dfs(u, id);
if(Up[u] > H[v])
{
printf("%d %d\n", v, u);
}
Up[v] = min(Up[v], Up[u]);
}
}
}
int main()
{
A.init(), B.init();
scanf("%d%d", &n, &m);
int ptr = 0;
for(int i = 1; i <= m; i ++)
{
int a, b;
scanf("%d%d", &a, &b);
if(A.unify(a, b))
{
G[a].push_back(Mp(b, ++ ptr));
G[b].push_back(Mp(a, ptr));
}
else
{
if(B.unify(a, b))
{
G[a].push_back(Mp(b, ++ ptr));
G[b].push_back(Mp(a, ptr));
}
}
}
for(int i = 1; i <= n; i ++)
{
if(H[i]) continue;
H[i] = 1;
dfs(i, 0);
}
return 0;
}
/** test corner cases(n = 1?) watch for overflow or minus indices **/
Compilation message (stderr)
pipes.cpp: In function 'int main()':
pipes.cpp:81:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
81 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:86:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
86 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
# | 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... |