Submission #97148

# Submission time Handle Problem Language Result Execution time Memory
97148 2019-02-14T04:46:43 Z dndhk Pipes (CEOI15_pipes) C++14
20 / 100
86 ms 4220 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];

vector<int> v;

int find_g1(int x){
	while(x!=g1[x]){
		v.push_back(x);
		x = g1[x];
	}
	while(!v.empty()){
		g1[v.back()] = x;
		v.pop_back();
	}
	return x;
}

int find_g2(int x){
	if(x==g2[x])	return x;
	return g2[x] = find_g2(g2[x]);
}

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;
	int a, b, A, B;
	while(M--){
		scanf("%d%d", &a, &b);
		A = a;
		while(!g1[A]==A){
			v.push_back(A); A = g1[A];
		}
		while(!v.empty()){
			g1[v.back()] = A; v.pop_back();
		}
		B = b;
		while(!g1[B]==B){
			v.push_back(B); B = g1[B];
		}
		while(!v.empty()){
			g1[v.back()] = B; v.pop_back();
		}
		if(A!=B){
			gp[sz++] = {a, b}; gp[sz++] = {b, a};
			g1[A] = B;
			continue;
		}
		A = a; B = b;
		while(!g2[A]==A){
			v.push_back(A); A = g2[A];
		}
		while(!v.empty()){
			g2[v.back()] = A; v.pop_back();
		}
		while(!g2[B]==B){
			v.push_back(B); B = g2[B];
		}
		while(!v.empty()){
			g2[v.back()] = B; v.pop_back();
		}
		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:70:15: warning: logical not is only applied to the left hand side of comparison [-Wlogical-not-parentheses]
   while(!g1[A]==A){
               ^~
pipes.cpp:70:9: note: add parentheses around left hand side expression to silence this warning
   while(!g1[A]==A){
         ^~~~~~
         (     )
pipes.cpp:77:15: warning: logical not is only applied to the left hand side of comparison [-Wlogical-not-parentheses]
   while(!g1[B]==B){
               ^~
pipes.cpp:77:9: note: add parentheses around left hand side expression to silence this warning
   while(!g1[B]==B){
         ^~~~~~
         (     )
pipes.cpp:89:15: warning: logical not is only applied to the left hand side of comparison [-Wlogical-not-parentheses]
   while(!g2[A]==A){
               ^~
pipes.cpp:89:9: note: add parentheses around left hand side expression to silence this warning
   while(!g2[A]==A){
         ^~~~~~
         (     )
pipes.cpp:95:15: warning: logical not is only applied to the left hand side of comparison [-Wlogical-not-parentheses]
   while(!g2[B]==B){
               ^~
pipes.cpp:95:9: note: add parentheses around left hand side expression to silence this warning
   while(!g2[B]==B){
         ^~~~~~
         (     )
pipes.cpp:64: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:68: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 740 KB Output is correct
2 Correct 7 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 3684 KB Output is correct
2 Execution timed out 44 ms 3548 KB Time limit exceeded (wall clock)
# Verdict Execution time Memory Grader output
1 Execution timed out 44 ms 3448 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 47 ms 3676 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 48 ms 4028 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 53 ms 4024 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 52 ms 4216 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 53 ms 4208 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 48 ms 4220 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -