#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;
for(int i = 1; i <= n ;i ++){
par1[i] = par2[i] = -1;
}
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
pipes.cpp: In function 'int main()':
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(_ "inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
pipes.cpp:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | freopen(_ "out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
3160 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
3420 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
8788 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
94 ms |
13140 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
178 ms |
20564 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
222 ms |
27608 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
334 ms |
40532 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
436 ms |
52388 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
571 ms |
63880 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
707 ms |
65536 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |