답안 #96871

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
96871 2019-02-12T14:34:13 Z Retro3014 Pipes (CEOI15_pipes) C++17
90 / 100
5000 ms 15092 KB
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
 
using namespace std;
#define MAX_N 100000
#define LN 17
typedef long long ll;
typedef pair<int, int> pii;
 
int N, M;
vector<pii > gp[MAX_N+1];
int num[MAX_N+1];
int p[MAX_N+1][LN];
int up[MAX_N+1];
int lv[MAX_N+1];
bool vst[MAX_N+1];
 
 
 
pii edge[MAX_N+1];
bool chk[MAX_N+1];
int sz = 0;
 
 
 
void dfs(int x){
	for(int i=0; i<gp[x].size(); i++){
		if(gp[x][i].first==p[x][0])	continue;
		dfs(gp[x][i].first);
		if(up[gp[x][i].first]<=lv[x])	chk[gp[x][i].second] = false;
		up[x] = min(up[x], up[gp[x][i].first]);
	}
}
 
void init_graph(int x){
	up[x] = lv[x];
	for(int i=1; i<LN; i++)	{
		p[x][i] = p[p[x][i-1]][i-1];
		//if(p[x][i]==0)	break;
	}
	for(int i=0; i<gp[x].size(); i++){
		if(gp[x][i].first==p[x][0])	continue;
		lv[gp[x][i].first] = lv[x]+1;
		p[gp[x][i].first][0] = x;
		init_graph(gp[x][i].first);
	}
}
 
 
 
int main(){
	scanf("%d%d", &N, &M);
	for(int i=1; i<=N; i++)	num[i] = 1;
	int a, b, k, tmp, t;
	int A, B;
	for(int i=1; i<=N; i++){
		lv[i] = up[i] = 1;
	}
	while(M--){
		scanf("%d%d", &a, &b);
		A = a; B = b;
		for(int i=LN-1; i>=0; i--){
			if(p[A][i]!=0)	A = p[A][i];
			if(p[B][i]!=0)	B = p[B][i];
		}
		if(A==B){
			//cout<<'*'<<a<<' '<<b<<endl;
			A = a; B = b;
			for(int i=LN-1; i>=0; i--){
				if(lv[p[A][i]]>=lv[B]){
					A = p[A][i];
				}
				if(lv[p[B][i]]>=lv[A]){
					B = p[B][i];
				}
			}
			for(int i=LN-1; i>=0; i--){
				if(p[A][i]!=p[B][i]){
					A = p[A][i]; B = p[B][i];
				}
			}
			if(A!=B)	A = p[B][0];
			k = A;
			//cout<<k<<endl;
			up[a] = min(up[a], lv[k]);
			up[b] = min(up[b], lv[k]);
		}else{
			if(num[A]>=num[B]){
				tmp = a; a = b; b = tmp;
				tmp = A; A = B; B = tmp;
			}
			t = num[A];
			dfs(A);
			for(int i=1; i<LN; i++){
				p[a][i] = 0;
			}p[a][0] = b;
			lv[a] = lv[b]+1;
			//printf("%d %d\n", a, b);
			gp[a].push_back({b, sz});
			gp[b].push_back({a, sz});
			init_graph(a);
			edge[sz] = {a, b};
			chk[sz] = true;
			sz++;
			num[B] += t;
		}
	}
	for(int i=1; i<=N; i++){
		int k = i;
		for(int j=LN-1; j>=0; j--){
			if(p[k][j]!=0)	k = p[k][j];
		}
		if(!vst[k]){
			vst[k] = true;
			dfs(k);
		}
	}
	for(int i=0; i<sz; i++){
		if(chk[i]){
			printf("%d %d\n", edge[i].first, edge[i].second);
		}
	}
	return 0;
}

Compilation message

pipes.cpp: In function 'void dfs(int)':
pipes.cpp:29:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<gp[x].size(); i++){
               ~^~~~~~~~~~~~~
pipes.cpp: In function 'void init_graph(int)':
pipes.cpp:43:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<gp[x].size(); i++){
               ~^~~~~~~~~~~~~
pipes.cpp: In function 'int main()':
pipes.cpp:54: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:62:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 3 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 3200 KB Output is correct
2 Correct 10 ms 3376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 253 ms 3176 KB Output is correct
2 Correct 248 ms 3152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 512 ms 3912 KB Output is correct
2 Correct 594 ms 3896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 841 ms 5648 KB Output is correct
2 Correct 709 ms 6332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1572 ms 11392 KB Output is correct
2 Correct 1323 ms 11308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2406 ms 12620 KB Output is correct
2 Correct 1984 ms 12512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3520 ms 14920 KB Output is correct
2 Correct 3539 ms 15092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4309 ms 15052 KB Output is correct
2 Correct 4499 ms 14968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5018 ms 14200 KB Time limit exceeded
2 Halted 0 ms 0 KB -