답안 #24681

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
24681 2017-06-11T16:01:46 Z Bruteforceman Pipes (CEOI15_pipes) C++11
100 / 100
337 ms 13796 KB
#include "bits/stdc++.h"
using namespace std;

/** Interface */
inline int readChar();
template <class T = int> inline T readInt(); 
template <class T> inline void writeInt( T x, char end = 0 );
inline void writeChar( int x ); 
inline void writeWord( const char *s );
/** Read */

static const int buf_size = 4096;
inline int getChar() {
	static char buf[buf_size];
	static int len = 0, pos = 0;
	if (pos == len)
		pos = 0, len = fread(buf, 1, buf_size, stdin);
	if (pos == len)
		return -1;
	return buf[pos++];
}

inline int readChar() {
	int c = getChar();
	while (c <= 32)
		c = getChar();
	return c;
}

template <class T>
inline T readInt() {
	int s = 1, c = readChar();
	T x = 0;
	if (c == '-')
		s = -1, c = getChar();
	while ('0' <= c && c <= '9')
		x = x * 10 + c - '0', c = getChar();
	return s == 1 ? x : -x;
}

/** Write */

static int write_pos = 0;
static char write_buf[buf_size];

inline void writeChar( int x ) {
	if (write_pos == buf_size)
		fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
	write_buf[write_pos++] = x;
}

template <class T> 
inline void writeInt( T x, char end ) {
	if (x < 0)
		writeChar('-'), x = -x;

	char s[24];
	int n = 0;
	while (x || !n)
		s[n++] = '0' + x % 10, x /= 10;
	while (n--)
		writeChar(s[n]);
	if (end)
		writeChar(end);
}

inline void writeWord( const char *s ) {
	while (*s)
		writeChar(*s++);
}

struct Flusher {
	~Flusher() {
		if (write_pos)
			fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
	}
} flusher;
/** Example */


int *par;
int *bic;
int *subPar;
int *subBic;

vector <int> *g;
int *low, *disc;
bitset <100005> vis;
vector <int> l, r;

int rootPar(int x) {
	if(par[x] == x) return x;
	return par[x] = rootPar(par[x]);
}
int rootBic(int x) {
	if(bic[x] == x) return x;
	return bic[x] = rootBic(bic[x]);
}

void joinPar(int x, int y) {
	x = rootPar(x);
	y = rootPar(y);
	if(subPar[x] > subPar[y]) swap(x, y);
	if(x != y) {
		par[x] = y;
		subPar[y] += subPar[x]; 
	}
}
void joinBic(int x, int y) {
	x = rootBic(x);
	y = rootBic(y);
	if(subBic[x] > subBic[y]) swap(x, y);
	if(x != y) {
		bic[x] = y;
		subBic[y] += subBic[x];
	}
}

int tym;
void dfs(int x, int parent) {
	vis[x] = 1;
	low[x] = disc[x] = ++tym;
	bool see = false;
	for(auto i : g[x]) {
		if (!see && i == parent) {
			see = true;
			continue;
		}
		if(vis[i] == 0) {
			dfs(i, x);
			low[x] = min(low[x], low[i]);
			if(disc[x] < low[i]) {
				l.push_back(x);
				r.push_back(i);
			}
		} else if (vis[i] == 1) {
			low[x] = min(low[x], disc[i]);
		}
	}
}

int main(int argc, char const *argv[])
{
	int n, m;
	n = readInt();
	m = readInt();

	par = new int [n + 5];
	bic = new int [n + 5];
	subPar = new int [n + 5];
	subBic = new int [n + 5];

	for(int i = 1; i <= n; i++) {	
		par[i] = i;
		bic[i] = i;
		subPar[i] = subBic[i] = 1;
	}
	for(int i = 1; i <= m; i++) {
		int p, q;
		p = readInt();
		q = readInt();
		if(rootPar(p) != rootPar(q)) {
			joinPar(p, q);
			l.push_back(p);
			r.push_back(q);
		} else if (rootBic(q) != rootBic(p)) {
			joinBic(p, q);
			l.push_back(p);
			r.push_back(q);
		}
	}

	delete par;
	delete bic;
	delete subPar;
	delete subBic;

	g = new vector <int> [n + 5];
	for(size_t i = 0; i < l.size(); i++) {
		g[l[i]].push_back(r[i]);
		g[r[i]].push_back(l[i]);
	}
	l.clear();
	r.clear();

	low = new int [n + 5];
	disc = new int [n + 5];

	for(int i = 1; i <= n; i++) {
		if(vis[i] == 0) {
			dfs(i, 0);
		}
	}	
	for(size_t i = 0; i < l.size(); i++) {
		printf("%d %d\n", l[i], r[i]);
	}
	delete low;
	delete disc;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 896 KB Output is correct
2 Correct 3 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 768 KB Output is correct
2 Correct 22 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 1656 KB Output is correct
2 Correct 39 ms 1276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 3480 KB Output is correct
2 Correct 61 ms 3288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 117 ms 9760 KB Output is correct
2 Correct 114 ms 6948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 169 ms 11192 KB Output is correct
2 Correct 158 ms 8456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 238 ms 13796 KB Output is correct
2 Correct 225 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 293 ms 13796 KB Output is correct
2 Correct 269 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 337 ms 13088 KB Output is correct
2 Correct 323 ms 10476 KB Output is correct