Submission #457723

# Submission time Handle Problem Language Result Execution time Memory
457723 2021-08-07T10:44:04 Z vanic Pipes (CEOI15_pipes) C++14
0 / 100
5000 ms 6476 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 < 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]);
		}
	}
}



void dfs(int x, int prosl){
	gore[x]=prosl;
	dep[x]=dep[prosl]+1;
	for(int i=0; i<(int)ms[x].size(); i++){
		if(ms[x][i]!=prosl){
			dfs(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);
			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])){
			cout << i << ' ' << gore[i] << '\n';
		}
	}
	return 0;
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:104:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:107:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |   scanf("%d%d", &a, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Execution timed out 5053 ms 3788 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5080 ms 3916 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 125 ms 3948 KB Output is correct
2 Execution timed out 5082 ms 3788 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Execution timed out 5081 ms 4044 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5099 ms 4556 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5094 ms 5776 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5092 ms 6092 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5097 ms 6476 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5042 ms 6468 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5094 ms 6452 KB Time limit exceeded
2 Halted 0 ms 0 KB -