답안 #457696

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
457696 2021-08-07T09:57:16 Z vanic Pipes (CEOI15_pipes) C++14
40 / 100
5000 ms 21188 KB
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cassert>

using namespace std;

const int maxn=1e5+5;

vector < pair < int, int > > ms[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){
		x=find(x);
		y=find(y);
		if(ms[x].size()>ms[y].size()){
			swap(x, y);
		}
		parent[x]=y;
		pair < int, int > p;
		while(!ms[x].empty()){
			p=ms[x].back();
			ms[x].pop_back();
			if(y==find(p.second)){
				continue;
			}
			ms[y].push_back(p);
		}
	}
	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;

vector < int > put;
bool bio[maxn];

bool dfs(int x, int prosl){
	bool p=0;
//	cout << x << endl;
	if(bio[x]){
		for(int i=0; i<(int)put.size(); i++){
			if(put[i]==x){
				p=1;
			}
			else if(p){
//				cout << "upd " << put[i] << ' ' << put[i-1] << endl;
				U1.update(put[i], put[i-1]);
			}
		}
		return 1;
	}
	bio[x]=1;
	put.push_back(x);
	for(int i=0; i<(int)ms[x].size(); i++){
		if(!p && U1.find(ms[x][i].second)==prosl){
			p=1;
		}
		else if(U1.find(ms[x][i].second)!=x){
			if(dfs(U1.find(ms[x][i].second), x)){
				bio[x]=0;
				put.pop_back();
				return 1;
			}
		}
	}
	put.pop_back();
	bio[x]=0;
	return 0;
}

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[U1.find(a)].push_back({a, b});
			ms[U1.find(b)].push_back({b, a});
			U2.update(a, b);
		}
		else if(!U1.query(a, b)){
			ms[U1.find(a)].push_back({a, b});
			ms[U1.find(b)].push_back({b, a});
			assert(dfs(U1.find(a), -1));
		}
//		cout << "na " << i << endl;
	}
	for(int i=1; i<=n; i++){
		for(int j=0; j<(int)ms[i].size(); j++){
			if(U1.find(ms[i][j].first)<U1.find(ms[i][j].second)){
				printf("%d %d\n", ms[i][j].first, ms[i][j].second);
			}
		}
	}
	return 0;
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:122:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:125:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |   scanf("%d%d", &a, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 3788 KB Output is correct
2 Correct 3 ms 3788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 121 ms 4160 KB Output is correct
2 Correct 60 ms 4132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 208 ms 9412 KB Output is correct
2 Correct 142 ms 9236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 856 ms 13928 KB Output is correct
2 Correct 361 ms 15560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4485 ms 21188 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5095 ms 6252 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5069 ms 7272 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5069 ms 6944 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5070 ms 6988 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5086 ms 7092 KB Time limit exceeded
2 Halted 0 ms 0 KB -