This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of god
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template < class T > using ordered_set = tree < T , null_type , less < T > , rb_tree_tag , tree_order_statistics_node_update >;
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 lc(x) (x) << 1
#define rc(x) (x) << 1 | 1
#define F first
#define S second
#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);
#define mp make_pair
ll poww(ll a, ll b, ll md) {
ll ans = 1;
for(; b; b >>= 1, a = a * a % md){
if(b & 1){
ans = ans * a % md;
}
}
return ans % md;
}
const int MAXN = 1e5 + 10;
const int MAXLG = 18;
const int MOD = 1e9 + 7;
const int MOD2 = 998244353;
const ll INF = 8e18;
int n, m, par[2][MAXN], sz[2][MAXN], st[MAXN], dp[MAXN], cnt;
pii pr[MAXN];
bool mark[MAXN];
vector<pii> adj[MAXN];
int getroot(int ind, int v){
if(par[ind][v] == v){
return v;
}
return par[ind][v] = getroot(ind, par[ind][v]);
}
bool merg(int ind, int v, int u){
v = getroot(ind, v);
u = getroot(ind, u);
if(v == u){
return false;
}
if(sz[ind][v] < sz[ind][u]){
swap(v, u);
}
par[ind][u] = v;
sz[ind][v] += sz[ind][u];
return true;
}
void dfs(int v){
st[v] = cnt++;
dp[v] = v;
mark[v] = true;
for(auto u : adj[v]){
if(!mark[u.S]){
pr[u.S] = mp(u.F, v);
dfs(u.S);
if(st[dp[u.S]] < st[dp[v]]){
dp[v] = dp[u.S];
}
}
else{
if(u.F == pr[v].F){
continue;
}
if(st[u.S] < st[dp[v]]){
dp[v] = u.S;
}
}
}
if(dp[v] == v && pr[v].S != 0){
printf("%d %d\n", v, pr[v].S);
}
}
int main()
{
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++){
par[0][i] = par[1][i] = i;
sz[0][i] = sz[1][i] = 1;
}
for(int i = 0; i < m; i++){
int x, y;
scanf("%d %d", &x, &y);
if(merg(0, x, y)){
adj[x].push_back(mp(i, y));
adj[y].push_back(mp(i, x));
}
else{
if(merg(1, x, y)){
adj[x].push_back(mp(i, y));
adj[y].push_back(mp(i, x));
}
}
}
for(int i = 1; i <= n; i++){
if(!mark[i]){
dfs(i);
}
}
}
Compilation message (stderr)
pipes.cpp:3: warning: ignoring #pragma comment [-Wunknown-pragmas]
3 | #pragma comment(linker, "/stack:200000000")
|
pipes.cpp: In function 'int main()':
pipes.cpp:95:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
95 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
pipes.cpp:102:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
102 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
# | 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... |