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>
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 (stderr)
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] )
| ^
# | 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... |