답안 #96888

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
96888 2019-02-12T15:48:53 Z Retro3014 Pipes (CEOI15_pipes) C++17
100 / 100
1508 ms 12624 KB
#include <iostream>
#include <vector>
#include <stdio.h>
#include <algorithm>

#define MAX_N 100000

using namespace std;

int N, M;
vector<int> gp[MAX_N+1];
int g1[MAX_N+1], g2[MAX_N+1];
int up[MAX_N+1], lv[MAX_N+1], p[MAX_N+1];

void init(){
	for(int i=1; i<=N; i++)	g1[i] = g2[i] = i;
}

int find_g1(int x){
	if(x==g1[x])	return x;
	return g1[x] = find_g1(g1[x]);
}

int find_g2(int x){
	if(x==g2[x])	return x;
	return g2[x] = find_g2(g2[x]);
}

void union_g1(int x, int y){
	x = find_g1(x); y = find_g1(y);
	g1[x] = y;
}

void union_g2(int x, int y){
	x = find_g2(x); y = find_g2(y);
	g2[x] = y;
}

bool vst[MAX_N+1];

void dfs(int x){
	bool tf = true;
	up[x] = lv[x];
	vst[x] = true;
	for(int i=0; i<gp[x].size(); i++){
		if(gp[x][i]==p[x] && tf){
			tf = false; continue;
		}	
		if(vst[gp[x][i]]){
			up[x] = min(up[x], lv[gp[x][i]]);
		}else{
			p[gp[x][i]] = x;
			lv[gp[x][i]] = lv[x]+1;
			dfs(gp[x][i]);
			if(up[gp[x][i]]>lv[x]){
				printf("%d %d\n", gp[x][i], x);
			}
			up[x] = min(up[x], up[gp[x][i]]);
		}
	}
}

int main(){
	scanf("%d %d", &N, &M);
	init();
	int a, b;
	while(M--){
		scanf("%d%d", &a, &b);
		if(find_g1(a)!=find_g1(b)){
			union_g1(a, b);
			gp[a].push_back(b);
			gp[b].push_back(a);
		}
		else if(find_g2(a)!=find_g2(b)){
			union_g2(a, b);
			gp[a].push_back(b);
			gp[b].push_back(a);
		}
	}
	for(int i=1; i<=N; i++){
		if(!vst[i]){
			lv[i] = 1;
			dfs(i);
		}
	}
	return 0;
}

Compilation message

pipes.cpp: In function 'void dfs(int)':
pipes.cpp:45: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:64: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:68: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 8 ms 3072 KB Output is correct
2 Correct 10 ms 2944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 126 ms 3028 KB Output is correct
2 Correct 123 ms 2888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 220 ms 3588 KB Output is correct
2 Correct 245 ms 3304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 347 ms 5032 KB Output is correct
2 Correct 305 ms 4916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 489 ms 9684 KB Output is correct
2 Correct 441 ms 7448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 812 ms 10676 KB Output is correct
2 Correct 799 ms 8596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1349 ms 12624 KB Output is correct
2 Correct 1036 ms 9336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1274 ms 12488 KB Output is correct
2 Correct 1209 ms 9432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1508 ms 11984 KB Output is correct
2 Correct 1455 ms 10056 KB Output is correct