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>
#define ll long long
using namespace std;
const int N = 1e5 + 2;
int par1[N + 2] , par2[N + 2];
int nxt[N + 2] , depth[N + 2];
vector <int > adj[N + 2];
int find_par1(int u){
if(par1[u] < 0) return u;
par1[u] = find_par1(par1[u]);
return par1[u];
}
int find_par2(int u){
if(par2[u] < 0) return u;
par2[u] = find_par2(par2[u]);
return par2[u];
}
void union_set2(int u , int v){
u = find_par2(u);
v = find_par2(v);
if(u != v){
if(par2[u] > par2[v])swap(u , v);
par2[u] += par2[v];
par2[v] = u;
depth[u] = min(depth[u] , depth[v]);
if(find_par2(nxt[u]) == u){
nxt[u] = nxt[v];
}
}
}
void dfs(int u , int p ,int h ){
depth[find_par2(u)] = h;
nxt[find_par2(u)] = find_par2(p);
for(auto v: adj[u]){
if(v == p)continue;
if(find_par2(v) == find_par2(u))dfs(v , u , h);
else dfs(v , u , h + 1);
}
}
bool union_set1(int u , int v){
int u_old = u;
int v_old = v;
u = find_par1(u);
v = find_par1(v);
if(u != v){
if(par1[u] > par1[v]){
swap(u , v);
swap(v_old , u_old);
}
par1[u] += par1[v];
par1[v] = u;
dfs(v_old , u_old , depth[u_old] + 1);
return true;
}
return false;
}
void lca(int u , int v){
u = find_par2(u);
v = find_par2(v);
while(u != v){
if(depth[u] > depth[v])swap(u , v);
int v_new = nxt[v];
union_set2(v_new , v);
v = v_new;
}
}
void dfs2(int u){
par1[u] = 1;
for(auto v: adj[u]){
if(par1[v])continue;
if(find_par2(v) != find_par2(u)){
cout << u << ' ' << v << '\n';
}
dfs2(v);
}
}
void solve() {
int n , m;
cin >> n >> m;
memset(par1 , -1 , sizeof(par1));
memset(par2 , -1 , sizeof(par2));
for(int i = 1; i <= m; i ++){
int u , v;
cin >>u >>v ;
if(union_set1(u , v)){
adj[u].push_back(v);
adj[v].push_back(u);
}
else{
// lca(u , v);
}
}
memset(par1 , 0 , sizeof(par1));
for(int i = 1 ; i <= n ; i++){
if(!par1[i])dfs2(i);
}
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
#define _ "maxseq."
if (fopen(_ "inp", "r")) {
freopen(_ "inp", "r", stdin);
freopen(_ "out", "w", stdout);
}
solve();
}
Compilation message (stderr)
pipes.cpp: In function 'int main()':
pipes.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(_ "inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
pipes.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen(_ "out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |