Submission #97156

# Submission time Handle Problem Language Result Execution time Memory
97156 2019-02-14T07:21:52 Z dndhk Pipes (CEOI15_pipes) C++14
100 / 100
1478 ms 8392 KB
#include <iostream>
#include <vector>
#include <stdio.h>
#include <algorithm>

#define MAX_N 100000

using namespace std;

int N, M;
pair<int, int> gp[MAX_N*4+1];
int sz = 0;
int g1[MAX_N+1], g2[MAX_N+1];
int up[MAX_N+1], lv[MAX_N+1], p[MAX_N+1];

int v[MAX_N+1];
int sz2;
int a, b, A, B;



int s, e, m;

void dfs(int x){
	bool tf = true;
	up[x] = lv[x];
	s = 0, e = sz-1;
	while(s<e){
		m = (s+e)/2;
		if(gp[m].first<x) s = m+1;
		else	e = m;
	}
	for(int i=s; i<sz; i++){
		if(gp[i].first!=x)	break;
		if(gp[i].second==p[x] && tf){
			tf = false; continue;
		}	
		if(lv[gp[i].second]!=0){
			up[x] = min(up[x], lv[gp[i].second]);
		}else{
			p[gp[i].second] = x;
			lv[gp[i].second] = lv[x]+1;
			dfs(gp[i].second);
			if(up[gp[i].second]>lv[x])	printf("%d %d\n", gp[i].second, x);
			up[x] = min(up[x], up[gp[i].second]);
		}
	}
}

int main(){
	scanf("%d %d", &N, &M);
	for(int i=1; i<=N; i++)	g1[i] = g2[i] = i;
	while(M--){
		scanf("%d%d", &a, &b);
		A = a;
		while(g1[A]!=A){
			v[sz2++] = A; A = g1[A];
		}
		while(sz2>0){
			g1[v[--sz2]] = A;
		}
		B = b;
		while(g1[B]!=B){
			v[sz2++] = B; B = g1[B];
		}
		while(sz2>0){
			g1[v[--sz2]] = B;
		}
		if(A!=B){
			gp[sz++] = {a, b}; gp[sz++] = {b, a};
			g1[A] = B;
			continue;
		}
		A = a; B = b;
		while(g2[A]!=A){
			v[sz2++] = A; A = g2[A];
		}
		while(sz2>0){
			g2[v[--sz2]] = A;
		}
		while(g2[B]!=B){
			v[sz2++] = B; B = g2[B];
		}
		while(sz2>0){
			g2[v[--sz2]] = B;
		}
		if(A!=B){
			gp[sz++] = {a, b}; gp[sz++] = {b, a};;
			g2[A] = B;
		}
	}sort(gp, gp+sz);
	for(int i=1; i<=N; i++){
		if(lv[i]==0){
			lv[i] = 1;
			dfs(i);
		}
	}
	return 0;
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:51: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:54:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 768 KB Output is correct
2 Correct 7 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 124 ms 648 KB Output is correct
2 Correct 121 ms 552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 211 ms 1132 KB Output is correct
2 Correct 243 ms 888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 355 ms 2168 KB Output is correct
2 Correct 290 ms 2084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 486 ms 5880 KB Output is correct
2 Correct 451 ms 4088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1006 ms 6836 KB Output is correct
2 Correct 761 ms 5184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1149 ms 8392 KB Output is correct
2 Correct 1027 ms 5824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1290 ms 8328 KB Output is correct
2 Correct 1176 ms 5908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1478 ms 7868 KB Output is correct
2 Correct 1475 ms 6168 KB Output is correct