Submission #27169

# Submission time Handle Problem Language Result Execution time Memory
27169 2017-07-09T18:03:39 Z bill_kondo Pipes (CEOI15_pipes) C++14
100 / 100
495 ms 12824 KB
#include "bits/stdc++.h"
using namespace std;

#define gc getchar_unlocked
#define pb push_back
#define debug(args...) fprintf(stderr,args)
#define FOR(i,a,b) for(int i = a; i <= b; ++i)
#define REP(i,a,b) for(int i = a; i >= b; --i)

typedef long long ll;
typedef pair<int,int>pii;

const int maxn = 1e5 + 10;

int n, m;

struct dsu{
	vector<int>par;

	void init(){
		par.resize(n+1);
		FOR(i,1,n)
			par[i] = i;
	}

	int root(int v){
		return par[v] = (par[v] == v ? v : root(par[v]));
	}

	void merge(int x, int y){
		x = root(x);
		y = root(y);
		if(x == y) return;
		par[y] = x;
	}

	void end(){
		par.clear();
		par.shrink_to_fit();
	}

} T[2];

vector<pii>adj[maxn];

static int cnt, cur;
vector<int>h, low;

inline void dfs(int v, int par){
	low[v] = h[v] = ++cur;
	for(int i = 0; i < adj[v].size(); ++i){
		int u = adj[v][i].first;
		int e = adj[v][i].second;
		if(e == par) continue;
		if(h[u] == -1){
			dfs(u,e);
			low[v] = min(low[v],low[u]);
			if(low[u] > h[v]) printf("%d %d\n",u,v);
		}
		else low[v] = min(low[v],h[u]);
	}
}

inline void tarjan(){
	h.resize(n+1);
	low.resize(n+1);
	FOR(i,1,n) h[i] = -1;
	FOR(i,1,n)
		if(h[i] == -1)
			dfs(i,-1);
}

void read (int &x)
{
  register int c = gc();
 	x = 0;
	for(;((c<'0'||c>'9')&&c!='-');c=gc());     
  for(;c>='0'&&c<='9';c=gc())x=(x<<1)+(x<<3)+c-'0';    
}

int main(){
	read(n);
	read(m);

	T[0].init();
	T[1].init();

	FOR(i,1,m){
		int a, b;
		read(a);
		read(b);
		if(T[0].root(a) != T[0].root(b)){	
			++cnt;
			adj[a].pb(pii(b,cnt));
			adj[b].pb(pii(a,cnt));
			T[0].merge(a,b);
		}
		else if(T[1].root(a) != T[1].root(b)){
			++cnt;
			adj[a].pb(pii(b,cnt));
			adj[b].pb(pii(a,cnt));
			T[1].merge(a,b);
		}
	}

	T[0].end();
	T[1].end();

	tarjan();
	return 0;
}

Compilation message

pipes.cpp: In function 'void dfs(int, int)':
pipes.cpp:51:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < adj[v].size(); ++i){
                 ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 3 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3200 KB Output is correct
2 Correct 6 ms 3072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 3064 KB Output is correct
2 Correct 29 ms 2948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 3628 KB Output is correct
2 Correct 54 ms 3328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 96 ms 5104 KB Output is correct
2 Correct 100 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 180 ms 9756 KB Output is correct
2 Correct 165 ms 7696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 277 ms 10896 KB Output is correct
2 Correct 251 ms 8136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 361 ms 12800 KB Output is correct
2 Correct 356 ms 10048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 399 ms 12824 KB Output is correct
2 Correct 391 ms 9972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 486 ms 12308 KB Output is correct
2 Correct 495 ms 9652 KB Output is correct