#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int leader[N];
int ssize[N];
int get_leader ( int v )
{
if ( leader[v] == v )
return v;
leader[v] = get_leader( leader[v] );
return leader[v];
}
vector< pair<int,int> > adj[N];
int a[N];
int jmp[N];
int par[N];
int depth[N];
int lca ( int u, int v )
{
if ( u == v )
return u;
if ( depth[u] > depth[v] )
swap( u, v );
while ( depth[u] < depth[v] )
{
if ( depth[u] <= depth[ jmp[v] ] )
v = jmp[v];
else
v = par[v];
}
while ( u != v )
{
if ( jmp[u] != jmp[v] )
{
u = jmp[u];
v = jmp[v];
}
else
{
u = par[u];
v = par[v];
}
}
return v;
}
void add_leaf ( int v, int p )
{
depth[v] = depth[p] + 1;
par[v] = p;
if ( jmp[p] == -1 || jmp[ jmp[p] ] == -1 )
jmp[v] = p;
else if ( depth[p] - depth[ jmp[p] ] == depth[ jmp[p] ] - depth[ jmp[ jmp[p] ] ] )
jmp[v] = jmp[ jmp[p] ];
else
jmp[v] = p;
}
void dfs_build_jmp ( int v, int p )
{
add_leaf( v, p );
for ( auto& [to,w] : adj[v] )
if ( to != p )
dfs_build_jmp( to, v );
}
int dfs_a ( int v, int p )
{
for ( auto& [to,w] : adj[v] )
if ( to != p )
{
int res = dfs_a( to, v );
a[v] += res;
if ( res )
w = 1;
}
int res = a[v];
a[v] = 0;
return res;
}
int main ()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for ( int i=1; i<=n; i++ )
{
leader[i] = i;
ssize[i] = 1;
adj[i].clear();
a[i] = 0;
jmp[i] = par[i] = -1;
depth[i] = 1;
}
while ( m-- )
{
int u, v;
cin >> u >> v;
int lu = get_leader( u );
int lv = get_leader( v );
if ( lu == lv )
{
int w = lca( u, v );
a[u]++;
a[v]++;
a[w] -= 2;
continue;
}
if ( ssize[lu] < ssize[lv] )
{
swap( lu, lv );
swap( u, v );
}
dfs_a( lv, -1 );
dfs_build_jmp( v, u );
adj[u].push_back({ v, 0 });
adj[v].push_back({ u, 0 });
leader[lv] = lu;
ssize[lu] += ssize[lv];
}
for ( int i=1; i<=n; i++ )
if ( leader[i] == i )
dfs_a( i, -1 );
for ( int i=1; i<=n; i++ )
for ( auto& [to,w] : adj[i] )
if ( !w && i < to )
cout << i << ' ' << to << '\n';
return 0;
}
Compilation message
pipes.cpp: In function 'void dfs_build_jmp(int, int)':
pipes.cpp:79:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
79 | for ( auto& [to,w] : adj[v] )
| ^
pipes.cpp: In function 'int dfs_a(int, int)':
pipes.cpp:86:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
86 | for ( auto& [to,w] : adj[v] )
| ^
pipes.cpp: In function 'int main()':
pipes.cpp:164:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
164 | for ( auto& [to,w] : adj[i] )
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
3028 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
130 ms |
4088 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
245 ms |
4468 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
456 ms |
6940 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
692 ms |
7944 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1053 ms |
14336 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1436 ms |
12428 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1913 ms |
13352 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2148 ms |
23432 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |