This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Dost SEFEROĞLU
#include <bits/stdc++.h>
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#pragma GCC optimize("O3,unroll-loops")
using namespace std;
//#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " <<
#define all(cont) cont.begin(),cont.end()
#define vi vector<int>
int MOD = 998244353,inf = 2e18;
const int N = 1e5+50,Q = 2e5+50;
int dad[N],cur[N];
int d1[N],d2[N];
vi edges[N],newedges[N];
vi traverse;
struct DSU {
void bfs(int node) {
d1[node] = 0;
queue<int> q;
q.push(node);
while (!q.empty()) {
int f = q.front();
q.pop();
for (auto it : edges[f]) {
if (d1[cur[it]] == -1) {
d1[cur[it]] = d1[f]+1;
q.push(cur[it]);
}
}
}
}
void bfs2(int node) {
d2[node] = 0;
queue<int> q;
q.push(node);
while (!q.empty()) {
int f = q.front();
//cout << "REACH" sp f sp d2[f] << '\n';
traverse.push_back(f);
q.pop();
for (auto it : edges[f]) {
if (d2[cur[it]] == -1) {
d2[cur[it]] = d2[f]+1;
q.push(cur[it]);
}
}
}
}
int find(int x) {
if (x == dad[x]) return x;
return dad[x] = find(dad[x]);
}
void unite(int a,int b) {
int x = find(a),y = find(b);
if (x == y) {
bfs(a);
bfs2(b);
int dist = d1[b];
int newname = inf;
for (auto it : traverse) if (d1[it]+d2[it] == dist) newname = min(newname,cur[it]);
for (auto it : traverse) {
if (d1[it]+d2[it] != dist) continue;
cur[it] = newname;
for (auto itt : edges[it]) {
if (d1[itt]+d2[itt] != dist) newedges[newname].push_back(cur[itt]);
}
edges[it].clear();
}
for (auto it : traverse) d1[it] = d2[it] = -1;
edges[newname].swap(newedges[newname]);
traverse.clear();
newedges[newname].clear();
}
else {
dad[x] = y;
edges[a].push_back(b);
edges[b].push_back(a);
}
}
};
void solve() {
memset(d1,-1,sizeof d1);
memset(d2,-1,sizeof d2);
int n,m;
cin >> n >> m;
for (int i=1;i<=n;i++) dad[i] = i,cur[i] = i;
DSU dsu;
vector<pii> edg(m);
for (int i=0;i<m;i++) {
int a,b;
cin >> a >> b;
edg.push_back({a,b});
dsu.unite(cur[a],cur[b]);
}
for (auto it : edg) {
if (cur[it.ff] != cur[it.ss]) cout << it.ff sp it.ss << '\n';
}
}
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Dodi
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while (t --> 0) solve();
}
Compilation message (stderr)
pipes.cpp:13:27: warning: overflow in conversion from 'double' to 'int' changes value from '2.0e+18' to '2147483647' [-Woverflow]
13 | int MOD = 998244353,inf = 2e18;
| ^~~~
# | 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... |