# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
221661 | Lawliet | Pipes (CEOI15_pipes) | C++17 | 5 ms | 512 KiB |
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;
typedef pair<short int,short int> pii;
const short int LOG = 8;
const short int MAXN = 101;
short int n, m;
short int sz[MAXN];
short int root[MAXN];
short int prof[MAXN];
short int dp[LOG][MAXN];
short int lastNode[MAXN];
vector< short int > adj[MAXN];
set< pii > bridges;
void DFS(short int cur, short int p, short int curRoot)
{
dp[0][cur] = p;
root[cur] = curRoot;
prof[cur] = prof[p] + 1;
for(short int k = 1 ; k < LOG ; k++)
{
short int jump = dp[k - 1][cur];
dp[k][cur] = dp[k - 1][jump];
}
for(short int i = 0 ; i < adj[cur].size() ; i++)
{
short int viz = adj[cur][i];
if( viz != p )
DFS( viz , cur , curRoot );
}
}
short int getLastNode(short int cur)
{
if( lastNode[cur] == cur ) return cur;
return lastNode[cur] = getLastNode( lastNode[cur] );
}
short int LCA(short int U, short int V)
{
if( prof[U] < prof[V] ) swap( U , V );
short int d = prof[U] - prof[V];
for(short int k = 0 ; k < LOG ; k++)
if( d & (1 << k) ) U = dp[k][U];
if( U == V ) return U;
for(short int k = LOG - 1 ; k >= 0 ; k--)
{
if( dp[k][U] != dp[k][V] )
{
U = dp[k][U];
V = dp[k][V];
}
}
return dp[0][U];
}
void mergePath(short int U, short int L)
{
U = getLastNode( U );
while( prof[U] > prof[L] )
{
bridges.erase( { U , dp[0][U] } );
bridges.erase( { dp[0][U] , U } );
lastNode[U] = dp[0][U];
U = getLastNode( U );
}
}
void addEdge(short int U, short int V)
{
if( root[U] != root[V] )
{
adj[U].push_back( V );
adj[V].push_back( U );
bridges.insert( { U , V } );
if( sz[ root[U] ] < sz[ root[V] ] )
swap( U , V );
sz[U] += sz[V];
DFS( V , U , root[U] );
return;
}
short int L = LCA( U , V );
mergePath( U , L );
mergePath( V , L );
}
int main()
{
scanf("%d %d",&n,&m);
for(short int i = 1 ; i <= n ; i++)
{
sz[i] = 1;
root[i] = i;
lastNode[i] = i;
}
for(short int i = 1 ; i <= m ; i++)
{
short int U, V;
scanf("%d %d",&U,&V);
addEdge( U , V );
}
for(auto it = bridges.begin() ; it != bridges.end() ; it++)
printf("%d %d\n",it->first,it->second);
}
Compilation message (stderr)
# | 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... |