답안 #96799

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
96799 2019-02-12T03:54:09 Z Retro3014 Pipes (CEOI15_pipes) C++17
90 / 100
5000 ms 15508 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 g[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];

void init_g(){
	for(int i=1; i<=N; i++)	g[i] = i;
	for(int i=1; i<=N; i++)	num[i] = 1;
}

int find_g(int x){
	if(x==g[x])	return x;
	return g[x] = find_g(g[x]);
}

void union_g(int x, int y){
	x = find_g(x); y = find_g(y);
	g[x] = y;
	num[y] += num[x];
}

pii edge[MAX_N+1];
bool chk[MAX_N+1];
int sz = 0;

int lca(int x, int y){
	for(int i=LN-1; i>=0; i--){
		//cout<<i<<' '<<x<<' '<<y<<endl;
		if(lv[p[x][i]]>=lv[y]){
			x = p[x][i];
		}
		if(lv[p[y][i]]>=lv[x]){
			y = p[y][i];
		}
	}
	for(int i=LN-1; i>=0; i--){
		if(p[x][i]!=p[y][i]){
			x = p[x][i]; y = p[y][i];
		}
	}
	if(x!=y)	x = p[x][0];
	return x;
}

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];
	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);
	int a, b, k, tmp;
	init_g();
	for(int i=1; i<=N; i++){
		lv[i] = up[i] = 1;
	}
	while(M--){
		scanf("%d%d", &a, &b);
		if(find_g(a)==find_g(b)){
			//cout<<'*'<<a<<' '<<b<<endl;
			k = lca(a, b);
			//cout<<k<<endl;
			up[a] = min(up[a], lv[k]);
			up[b] = min(up[b], lv[k]);
		}else{
			if(num[find_g(a)]>=num[find_g(b)]){
				tmp = a; a = b; b = tmp;
			}
			dfs(find_g(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++;
			union_g(a, b);
		}
	}
	for(int i=1; i<=N; i++){
		if(!vst[find_g(i)]){
			vst[find_g(i)] = true;
			dfs(find_g(i));
		}
	}
	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:61: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:72: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:83: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:90: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 3 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 3284 KB Output is correct
2 Correct 9 ms 3328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 237 ms 3192 KB Output is correct
2 Correct 233 ms 3072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 441 ms 3964 KB Output is correct
2 Correct 544 ms 3840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 770 ms 5748 KB Output is correct
2 Correct 645 ms 6520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1372 ms 11496 KB Output is correct
2 Correct 1258 ms 11636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2155 ms 12940 KB Output is correct
2 Correct 1819 ms 12664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3142 ms 15284 KB Output is correct
2 Correct 3329 ms 15508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4572 ms 15292 KB Output is correct
2 Correct 4281 ms 15360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5019 ms 14656 KB Time limit exceeded
2 Halted 0 ms 0 KB -