답안 #696199

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
696199 2023-02-05T19:47:15 Z rainboy Snowy Roads (JOI16_snowy) C++17
100 / 100
18 ms 1820 KB
#include "Anyalib.h"
#include <stdlib.h>
#include <string.h>

static const int N = 500, L = 9;	/* L = ceil(log2(N)) */

static int ij[N - 1], cc[N - 1];
static int *eh[N], eo[N], n;

static void append(int i, int h) {
	int o = eo[i]++;
	if (o >= 2 && (o & o - 1) == 0)
		eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
	eh[i][o] = h;
}

static int ll[N], hh[N], qq[N];

static int dfs1(int p, int l, int q, int i) {
	if (ll[i]) {
		bool win = true;
		for (int o = eo[i]; o--; ) {
			int h = eh[i][o], j = i ^ ij[h];
			if (j != p && !dfs1(i, l, q, j))
				win = false;
		}
		return win;
	} else {
		bool win = q == -1;
		for (int o = eo[i]; o--; ) {
			int h = eh[i][o], j = i ^ ij[h];
			if (j != p && !dfs1(i, l, i, j))
				win = true;
		}
		if (!win)
			ll[i] = l, qq[i] = q;
		return win;
	}
}

static void dfs2(int p, int i, int c) {
	for (int l = 0; l < ll[i]; l++)
		Save(hh[i] + l, c >> l & 1);
	for (int o = eo[i]; o--; ) {
		int h = eh[i][o], j = i ^ ij[h];
		if (j != p)
			dfs2(i, j, c + cc[h]);
	}
}

void InitAnya(int n_, int *ii, int *jj) {
	n = n_;
	for (int i = 0; i < n; i++)
		eh[i] = (int *) malloc(2 * sizeof *eh[i]);
	for (int h = 0; h < n - 1; h++)
		ij[h] = ii[h] ^ jj[h];
	for (int h = 0; h < n - 1; h++)
		append(ii[h], h), append(jj[h], h);
	for (int l = 1; l <= L; l++)
		dfs1(-1, l, -1, 0);
	hh[0] = 0;
	for (int i = 1; i < n; i++)
		hh[i] = hh[i - 1] + ll[i - 1];
}

void Anya(int *cc_) {
	memcpy(cc, cc_, (n - 1) * sizeof *cc_);
	dfs2(-1, 0, 0);
}
#include "Borislib.h"
#include <stdlib.h>

static const int N = 500, L = 9;	/* L = ceil(log2(N)) */

static int ij[N - 1];
static int *eh[N], eo[N], n;

static void append(int i, int h) {
	int o = eo[i]++;
	if (o >= 2 && (o & o - 1) == 0)
		eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
	eh[i][o] = h;
}

static int ll[N], hh[N], qq[N];

static int dfs1(int p, int l, int q, int i) {
	if (ll[i]) {
		bool win = true;
		for (int o = eo[i]; o--; ) {
			int h = eh[i][o], j = i ^ ij[h];
			if (j != p && !dfs1(i, l, q, j))
				win = false;
		}
		return win;
	} else {
		bool win = q == -1;
		for (int o = eo[i]; o--; ) {
			int h = eh[i][o], j = i ^ ij[h];
			if (j != p && !dfs1(i, l, i, j))
				win = true;
		}
		if (!win)
			ll[i] = l, qq[i] = q;
		return win;
	}
}

static int query(int i, int l) {
	if (i == 0)
		return 0;
	int d = query(qq[i], ll[i] - 1);
	if (Ask(hh[i] + ll[i] - 1) != (d >> ll[i] - 1 & 1))
		d += 1 << ll[i] - 1;
	for (int l_ = ll[i] - 2; l_ >= l; l_--)
		if (Ask(hh[i] + l_))
			d |= 1 << l_;
	return d;
}

void InitBoris(int n_, int *ii, int *jj) {
	n = n_;
	for (int i = 0; i < n; i++)
		eh[i] = (int *) malloc(2 * sizeof *eh[i]);
	for (int h = 0; h < n - 1; h++)
		ij[h] = ii[h] ^ jj[h];
	for (int h = 0; h < n - 1; h++)
		append(ii[h], h), append(jj[h], h);
	for (int l = 1; l <= L; l++)
		dfs1(-1, l, -1, 0);
	hh[0] = 0;
	for (int i = 1; i < n; i++)
		hh[i] = hh[i - 1] + ll[i - 1];
}

int Boris(int i) {
	return query(i, 0);
}

Compilation message

Anya.cpp: In function 'void append(int, int)':
Anya.cpp:12:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   12 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~

Boris.cpp: In function 'void append(int, int)':
Boris.cpp:11:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   11 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
Boris.cpp: In function 'int query(int, int)':
Boris.cpp:44:44: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
   44 |  if (Ask(hh[i] + ll[i] - 1) != (d >> ll[i] - 1 & 1))
      |                                      ~~~~~~^~~
Boris.cpp:45:19: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   45 |   d += 1 << ll[i] - 1;
      |             ~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 520 KB Output is correct
2 Correct 1 ms 660 KB Output is correct
3 Correct 2 ms 704 KB Output is correct
4 Correct 3 ms 740 KB Output is correct
5 Correct 5 ms 940 KB Output is correct
6 Correct 5 ms 940 KB Output is correct
7 Correct 5 ms 980 KB Output is correct
8 Correct 5 ms 928 KB Output is correct
9 Correct 5 ms 1020 KB Output is correct
10 Correct 6 ms 928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1048 KB Output is correct
2 Correct 6 ms 1104 KB Output is correct
3 Correct 7 ms 1124 KB Output is correct
4 Correct 7 ms 1084 KB Output is correct
5 Correct 7 ms 1100 KB Output is correct
6 Correct 6 ms 1112 KB Output is correct
7 Correct 7 ms 1112 KB Output is correct
8 Correct 7 ms 1100 KB Output is correct
9 Correct 7 ms 1076 KB Output is correct
10 Correct 7 ms 1100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 1680 KB Output is correct
2 Correct 16 ms 1680 KB Output is correct
3 Correct 13 ms 1660 KB Output is correct
4 Correct 13 ms 1688 KB Output is correct
5 Correct 15 ms 1648 KB Output is correct
6 Correct 15 ms 1692 KB Output is correct
7 Correct 14 ms 1684 KB Output is correct
8 Correct 15 ms 1668 KB Output is correct
9 Correct 15 ms 1692 KB Output is correct
10 Correct 15 ms 1608 KB Output is correct
11 Correct 15 ms 1656 KB Output is correct
12 Correct 14 ms 1712 KB Output is correct
13 Correct 13 ms 1660 KB Output is correct
14 Correct 14 ms 1648 KB Output is correct
15 Correct 15 ms 1644 KB Output is correct
16 Correct 13 ms 1696 KB Output is correct
17 Correct 15 ms 1688 KB Output is correct
18 Correct 15 ms 1664 KB Output is correct
19 Correct 15 ms 1716 KB Output is correct
20 Correct 15 ms 1676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 1492 KB Output is correct
2 Correct 16 ms 1524 KB Output is correct
3 Correct 15 ms 1436 KB Output is correct
4 Correct 14 ms 1688 KB Output is correct
5 Correct 17 ms 1720 KB Output is correct
6 Correct 14 ms 1536 KB Output is correct
7 Correct 16 ms 1552 KB Output is correct
8 Correct 15 ms 1464 KB Output is correct
9 Correct 16 ms 1656 KB Output is correct
10 Correct 15 ms 1448 KB Output is correct
11 Correct 16 ms 1704 KB Output is correct
12 Correct 15 ms 1556 KB Output is correct
13 Correct 18 ms 1796 KB Output is correct
14 Correct 16 ms 1724 KB Output is correct
15 Correct 17 ms 1672 KB Output is correct
16 Correct 13 ms 1460 KB Output is correct
17 Correct 16 ms 1612 KB Output is correct
18 Correct 16 ms 1668 KB Output is correct
19 Correct 15 ms 1652 KB Output is correct
20 Correct 18 ms 1708 KB Output is correct
21 Correct 16 ms 1720 KB Output is correct
22 Correct 16 ms 1536 KB Output is correct
23 Correct 15 ms 1652 KB Output is correct
24 Correct 15 ms 1820 KB Output is correct
25 Correct 15 ms 1700 KB Output is correct
26 Correct 14 ms 1544 KB Output is correct
27 Correct 14 ms 1692 KB Output is correct
28 Correct 15 ms 1692 KB Output is correct
29 Correct 15 ms 1680 KB Output is correct
30 Correct 15 ms 1676 KB Output is correct
31 Correct 15 ms 1720 KB Output is correct
32 Correct 15 ms 1544 KB Output is correct
33 Correct 16 ms 1628 KB Output is correct
34 Correct 15 ms 1508 KB Output is correct
35 Correct 15 ms 1516 KB Output is correct
36 Correct 16 ms 1632 KB Output is correct
37 Correct 14 ms 1436 KB Output is correct
38 Correct 16 ms 1692 KB Output is correct
39 Correct 13 ms 1548 KB Output is correct
40 Correct 13 ms 1504 KB Output is correct
41 Correct 13 ms 1448 KB Output is correct
42 Correct 13 ms 1548 KB Output is correct
43 Correct 13 ms 1448 KB Output is correct
44 Correct 13 ms 1536 KB Output is correct
45 Correct 15 ms 1612 KB Output is correct
46 Correct 13 ms 1640 KB Output is correct
47 Correct 16 ms 1544 KB Output is correct
48 Correct 13 ms 1488 KB Output is correct
49 Correct 13 ms 1428 KB Output is correct
50 Correct 13 ms 1572 KB Output is correct