답안 #457731

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
457731 2021-08-07T10:53:31 Z vanic Pipes (CEOI15_pipes) C++14
50 / 100
1524 ms 52196 KB
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cassert>

using namespace std;

const int maxn=1e5+5;

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)]=0;
	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)]){
		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);
	int a, b;
	for(int i=0; i<m; i++){
		scanf("%d%d", &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);
		}
//		cout << "na " << i << endl;
	}
	for(int i=1; i<=n; i++){
		if(gore[i] && !U1.query(i, gore[i])){
			printf("%d %d\n", i, gore[i]);
		}
	}
	return 0;
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:116:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:119:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  119 |   scanf("%d%d", &a, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 3788 KB Output is correct
2 Correct 2 ms 3788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 3996 KB Output is correct
2 Correct 8 ms 3916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 133 ms 3916 KB Output is correct
2 Correct 130 ms 3916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 235 ms 4232 KB Output is correct
2 Correct 260 ms 4204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 371 ms 4712 KB Output is correct
2 Correct 302 ms 5020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 469 ms 26664 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 748 ms 22472 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1000 ms 29004 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1219 ms 39456 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1524 ms 52196 KB Memory limit exceeded
2 Halted 0 ms 0 KB -