답안 #221660

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
221660 2020-04-10T20:47:00 Z Lawliet Pipes (CEOI15_pipes) C++17
0 / 100
52 ms 65540 KB
#include <bits/stdc++.h>

using namespace std;
typedef pair<int,int> pii;

const int LOG = 8;
const int MAXN = 101;

int n, m;

int sz[MAXN];
int root[MAXN];
int prof[MAXN];
int dp[LOG][MAXN];
int lastNode[MAXN];

vector< int > adj[MAXN];

set< pii > bridges;

void DFS(int cur, int p, int curRoot)
{
	dp[0][cur] = p;
	root[cur] = curRoot;
	prof[cur] = prof[p] + 1;

	for(int k = 1 ; k < LOG ; k++)
	{
		int jump = dp[k - 1][cur];
		dp[k][cur] = dp[k - 1][jump];
	}

	for(int i = 0 ; i < adj[cur].size() ; i++)
	{
		int viz = adj[cur][i];

		if( viz != p )
			DFS( viz , cur , curRoot );
	}
}

int getLastNode(int cur)
{
	if( lastNode[cur] == cur ) return cur;
	return lastNode[cur] = getLastNode( lastNode[cur] );
}

int LCA(int U, int V)
{
	if( prof[U] < prof[V] ) swap( U , V );

	int d = prof[U] - prof[V];

	for(int k = 0 ; k < LOG ; k++)
		if( d & (1 << k) ) U = dp[k][U];

	if( U == V ) return U;

	for(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(int U, 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(int U, 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;
	}

	int L = LCA( U , V );

	mergePath( U , L );
	mergePath( V , L );
}

int main()
{
	scanf("%d %d",&n,&m);

	for(int i = 1 ; i <= n ; i++)
	{
		sz[i] = 1;
		root[i] = i;
		lastNode[i] = i;
	}

	for(int i = 1 ; i <= m ; i++)
	{
		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

pipes.cpp: In function 'void DFS(int, int, int)':
pipes.cpp:33:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0 ; i < adj[cur].size() ; i++)
                  ~~^~~~~~~~~~~~~~~~~
pipes.cpp: In function 'int main()':
pipes.cpp:113:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&m);
  ~~~~~^~~~~~~~~~~~~~~
pipes.cpp:125:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&U,&V);
   ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 52 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5 ms 384 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 5 ms 384 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 8 ms 544 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 5 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 384 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -