Submission #54788

# Submission time Handle Problem Language Result Execution time Memory
54788 2018-07-05T05:34:31 Z 윤교준(#1510) Pipes (CEOI15_pipes) C++11
100 / 100
4496 ms 14072 KB
#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define befv(V) ((V)[sz(V)-2])
#define sorv(V) sort(allv(V))
#define revv(V) reverse(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define upmax(a,b) (a)=max((a),(b))
#define upmin(a,b) (a)=min((a),(b))
#define rb(x) ((x)&(-(x)))
#define INF (0x3f3f3f3f)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

vector<int> G[100001];

int prt[100001][17], dep[100001];
int ud[100001], cnt[2][100001];
bitset<100001> chk;

int N, M;

int uf(int i) { return i == ud[i] ? i : (ud[i] = uf(ud[i])); }
void uf(int a, int b) { ud[uf(b)] = uf(a); }

int lca(int a, int b) {
	if(dep[a] > dep[b]) swap(a, b);
	const int dt = dep[b] - dep[a];
	for(int i = 0; i < 17; i++) if(dt & (1<<i))
		b = prt[b][i];
	if(a == b) return a;
	for(int i = 16; ~i; i--) if(prt[a][i] != prt[b][i]) {
		a = prt[a][i]; b = prt[b][i];
	}
	return prt[a][0];
}

void f(int i) {
	for(int v : G[i]) if(!dep[v]) {
		prt[v][0] = i;
		dep[v] = dep[i] + 1;
		f(v);
	}
}
void g(int i) {
	for(int v : G[i]) if(dep[i] < dep[v]) {
		g(v);
		cnt[1][i] += cnt[1][v];
	}
    cnt[1][i] -= cnt[0][i];
    chk[i] = !!cnt[1][i];
}

int main() {
	//freopen("input.txt", "r", stdin);
	ios::sync_with_stdio(false);

	cin >> N >> M;
	iota(ud, ud+N+1, 0);

	for(int i = 0, a, b; i < M; i++) {
		cin >> a >> b;
		if(uf(a) == uf(b)) continue;
		uf(a, b);
		G[a].pb(b); G[b].pb(a);
	}

	for(int i = 1; i <= N; i++) {
		if(dep[i]) continue;
		dep[i] = 1;
		f(i);
	}

	for(int j = 1; j < 17; j++) for(int i = 1; i <= N; i++)
		prt[i][j] = prt[prt[i][j-1]][j-1];

	std::cin.seekg(0, std::ios::beg);

	cin >> N >> M;
	for(int i = 0, a, b, c; i < M; i++) {
		cin >> a >> b;
		if(dep[a] > dep[b]) swap(a, b);
		if(prt[b][0] == a && !chk[b]) {
			chk[b] = true;
			continue;
		}
		c = lca(a, b);
		if(a == c) {
			cnt[0][a]++;
			cnt[1][b]++;
		} else {
			cnt[0][c] += 2;
			cnt[1][a]++;
			cnt[1][b]++;
		}
	}

	chk.reset();

	for(int i = 1; i <= N; i++) if(1 == dep[i]) g(i);
	for(int i = 1; i <= N; i++) if(1 < dep[i] && !chk[i])
		printf("%d %d\n", i, prt[i][0]);
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 3200 KB Output is correct
2 Correct 11 ms 3200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 275 ms 3156 KB Output is correct
2 Correct 258 ms 3192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 457 ms 3848 KB Output is correct
2 Correct 554 ms 3836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 830 ms 5452 KB Output is correct
2 Correct 685 ms 6104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1295 ms 10744 KB Output is correct
2 Correct 1004 ms 10716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2264 ms 11876 KB Output is correct
2 Correct 1936 ms 11896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3147 ms 14004 KB Output is correct
2 Correct 2936 ms 14044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3998 ms 14060 KB Output is correct
2 Correct 3583 ms 14072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4496 ms 13508 KB Output is correct
2 Correct 3901 ms 14044 KB Output is correct