답안 #457738

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
457738 2021-08-07T11:04:25 Z vanic Pipes (CEOI15_pipes) C++14
100 / 100
1547 ms 8116 KB
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;

const int maxn=1e5;

vector < int > ms[maxn];
int gore[maxn];
int dep[maxn];

struct union_find{
	int parent[maxn];
	union_find(){
		for(int i=0; i<maxn; i++){
			parent[i]=i;
		}
	}
	int find(int x){
		if(parent[x]==x){
			return x;
		}
		return parent[x]=find(parent[x]);
	}
	void update(int x, int y){
		parent[find(x)]=find(y);
	}
	bool query(int x, int y){
		return find(x)==find(y);
	}
};


struct union_find2{
	int parent[maxn];
	int sz[maxn];
	union_find2(){
		for(int i=0; i<maxn; i++){
			parent[i]=i;
			sz[i]=1;
		}
	}
	int find(int x){
		if(x==parent[x]){
			return x;
		}
		return parent[x]=find(parent[x]);
	}
	void update(int x, int y){
		x=find(x);
		y=find(y);
		if(sz[x]>sz[y]){
			swap(x, y);
		}
		parent[x]=y;
		sz[y]+=sz[x];
	}
	bool query(int x, int y){
		return find(x)==find(y);
	}
};

union_find U1;
union_find2 U2;


void lca(int x, int y){
	while(x!=y){
		x=U1.find(x);
		y=U1.find(y);
		if(x==y){
			break;
		}
		if(dep[x]>dep[y]){
			U1.update(x, gore[x]);
		}
		else{
			U1.update(y, gore[y]);
		}
	}
}


int novi[maxn];

void dfs(int x, int prosl){
	gore[x]=prosl;
	dep[x]=dep[prosl]+1;
	novi[U1.find(x)]=-1;
	for(int i=0; i<(int)ms[x].size(); i++){
		if(ms[x][i]!=prosl){
			dfs(ms[x][i], x);
		}
	}	
}

void rokaj(int x, int prosl){
	if(novi[U1.find(x)]==-1){
		novi[U1.find(x)]=x;
	}
	U1.parent[x]=novi[U1.parent[x]];
	for(int i=0; i<(int)ms[x].size(); i++){
		if(ms[x][i]!=prosl){
			rokaj(ms[x][i], x);
		}
	}
}

int main(){
	int n, m;
	scanf("%d%d", &n, &m);
	memset(gore, -1, sizeof(gore));
	int a, b;
	for(int i=0; i<m; i++){
		scanf("%d%d", &a, &b);
		a--;
		b--;
		if(!U2.query(a, b)){
			ms[a].push_back(b);
			ms[b].push_back(a);
			if(U2.sz[U2.find(a)]>U2.sz[U2.find(b)]){
				swap(a, b);
			}
			dfs(a, b);
			rokaj(a, b);
			U2.update(a, b);
		}
		else if(!U1.query(a, b)){
			lca(a, b);
		}
	}
	for(int i=0; i<n; i++){
		if(gore[i]!=-1 && !U1.query(i, gore[i])){
			printf("%d %d\n", i+1, gore[i]+1);
		}
	}
	return 0;
}

Compilation message

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]
  113 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:117:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |   scanf("%d%d", &a, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4172 KB Output is correct
2 Correct 3 ms 4172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4300 KB Output is correct
2 Correct 6 ms 4300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 134 ms 4300 KB Output is correct
2 Correct 129 ms 4300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 220 ms 4572 KB Output is correct
2 Correct 279 ms 4556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 359 ms 5032 KB Output is correct
2 Correct 321 ms 5316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 469 ms 6824 KB Output is correct
2 Correct 437 ms 7036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 733 ms 7236 KB Output is correct
2 Correct 711 ms 7328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 974 ms 7968 KB Output is correct
2 Correct 923 ms 8112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1202 ms 8028 KB Output is correct
2 Correct 1239 ms 8116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1547 ms 7876 KB Output is correct
2 Correct 1495 ms 8052 KB Output is correct