답안 #151102

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
151102 2019-09-01T17:39:38 Z pichulia Bulb Game (FXCUP4_bulb) C++17
100 / 100
299 ms 21640 KB
#include "bulb.h"
#include<algorithm>
using namespace std;
int n;
int parent[600009];
int value[600009];
int m;
int root;
int res;
vector<int> a;
vector<int> b;
vector<int> w;
void dfs(int si, int sum)
{
	if (si >= n) {
		if (value[si] == -1)
		{
			value[si] = sum;
		}
		return;
	}
	dfs(a[si], sum);
	dfs(b[si], sum + 1);
	value[si] = min(value[a[si]], value[b[si]]);
}
void dfs2(int si)
{
	if (si >= n) {
		return;
	}
	dfs2(a[si]);
	dfs2(b[si]);
	value[si] = min(value[a[si]], value[b[si]]);
}
void add_naive(int si, int val) {
	if (si >= n) {
		value[si] += val;
		return;
	}
	add_naive(a[si], val);
	add_naive(b[si], val);
	value[si] = min(value[a[si]], value[b[si]]);
}
void flip_naive(int si, int type) {
	if (type == 0) {
		add_naive(a[si], 1);
		add_naive(b[si], -1);
	}
	else {
		add_naive(a[si], -1);
		add_naive(b[si], 1);
	}
}
void dfs_type2(int si)
{
	if (si >= n)return;
	if (value[b[si]] > 2) { res = 1; return; }
	dfs_type2(b[si]);
	if (res == 1)return;
	dfs_type2(a[si]);
}
void precal(int si) {
	if (si >= n)return;
	if (value[a[si]] == 1) {
		w.push_back(si);
		return;
	}
	if(value[b[si]] == 1)
		precal(b[si]);
}
void deepdark(int si, int wi)
{
	if (si >= n)return;
	if (si != wi)
	{
		if (b[si] < n) {
			res = 0;
			return;
		}
		deepdark(a[si], wi);
	}
	else {
		if (a[si] < n)
		{
			res = 0;
			return;
		}
		deepdark(b[si], wi);
	}
}
int FindWinner(int T, std::vector<int> L, std::vector<int> R){
	n = L.size();
	a = L;
	b = R;
	m = n;
	int i, j, k;
	for (i = 0; i < n; i++) {
		parent[i] = i;
	}
	for (i = 0; i < n; i++) {
		if (L[i] >= 0)
		{
			parent[L[i]] = i;
		}
		else {
			if (L[i] == -1)
				value[m] = 999999;
			else
				value[m] = -1;
			a[i] = m;
			parent[m] = i;
			m++;
		}
		if (R[i] >= 0)
		{
			parent[R[i]] = i;
		}
		else {
			if (R[i] == -1)
				value[m] = 999999;
			else
				value[m] = -1;
			b[i] = m;
			parent[m] = i;
			m++;
		}
	}
	for (i = 0; i < n; i++) { if (parent[i] == i)break; }
	root = i;
	res = 0;
	dfs(root, 0);
	if (value[root] == 0) return 0;
	if (n <= 100) {
		for (i = 0; i < n; i++) {
			flip_naive(i, 0);
			for (j = 0; j < n; j++) {
				if (i == j) { flip_naive(j, 1); }
				else { flip_naive(j, 0); }
				dfs2(root);
				bool nono = false;
				if (value[root] == 0) nono = true;

				if (i == j) { flip_naive(j, 0); }
				else { flip_naive(j, 1); }

				if (nono)break;
			}
			flip_naive(i, 1);
			if (j == n) {
				//printf("%d is answer\n", i);
				break;
			}
		}
		return (i < n);
	}
	if (value[root] > 2)return 1;
	else if (value[root] == 2) {
		dfs_type2(root);
	}
	else {
		int superroot = root;
		while (root < n)
		{
			w.clear();
			precal(root);
			if (w.size() > 0)
			{
				flip_naive(w[0], 0);
				dfs2(superroot);
				//	printf("%d --> %d\n", w[0], value[root]);
				if (value[superroot] == 1) res = 0;
				else if (value[superroot] >= 2) res = 1;
				else {
					res = 1;
					deepdark(superroot, w[0]);
				}
				if (res == 1)
					break;
				flip_naive(w[0], 1);
			}
			root = a[root];
		}
	}
	return res;
}

Compilation message

bulb.cpp: In function 'int FindWinner(int, std::vector<int>, std::vector<int>)':
bulb.cpp:96:12: warning: unused variable 'k' [-Wunused-variable]
  int i, j, k;
            ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 348 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 424 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 380 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 276 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 424 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 380 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 380 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 380 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 276 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 424 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 380 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 380 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 380 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 101 ms 12408 KB Output is correct
28 Correct 103 ms 12792 KB Output is correct
29 Correct 182 ms 12684 KB Output is correct
30 Correct 135 ms 21632 KB Output is correct
31 Correct 136 ms 21640 KB Output is correct
32 Correct 252 ms 12252 KB Output is correct
33 Correct 100 ms 12536 KB Output is correct
34 Correct 299 ms 12536 KB Output is correct
35 Correct 285 ms 12352 KB Output is correct
36 Correct 254 ms 12588 KB Output is correct
37 Correct 226 ms 12376 KB Output is correct
38 Correct 265 ms 12616 KB Output is correct
39 Correct 102 ms 12676 KB Output is correct
40 Correct 104 ms 12300 KB Output is correct
41 Correct 270 ms 12364 KB Output is correct
42 Correct 268 ms 12620 KB Output is correct
43 Correct 188 ms 12536 KB Output is correct
44 Correct 170 ms 12536 KB Output is correct
45 Correct 251 ms 12280 KB Output is correct
46 Correct 131 ms 12664 KB Output is correct
47 Correct 137 ms 14332 KB Output is correct
48 Correct 102 ms 14588 KB Output is correct
49 Correct 138 ms 14328 KB Output is correct
50 Correct 103 ms 14200 KB Output is correct