답안 #542131

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
542131 2022-03-25T13:12:59 Z blue Power Plant (JOI20_power) C++17
100 / 100
193 ms 40328 KB
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

using vi = vector<int>;
using vvi = vector<vi>;

const int mx = 200'000;

int N;
vi edge[1+mx];

int DP[1+mx];
vi power;
vi children[1+mx];

int res = 1;

void dfs(int u, int p)
{
	for(int v : edge[u])
	{
		if(v == p) continue;
		children[u].push_back(v);
		dfs(v, u);
	}

	if(power[u]) DP[u] = 1;
	else DP[u] = 0;

	int curr = -power[u];
	for(int v : children[u])
		curr += max(0, DP[v]);

	DP[u] = max(DP[u], curr);

	for(int v : children[u])
		res = max(res, power[u] + DP[v]);

	curr = -power[u];
	for(int v : children[u])
		curr += max(0, DP[v]);

	res = max(res, curr);
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> N;
	for(int e = 1; e <= N-1; e++)
	{
		int a, b;
		cin >> a >> b;
		edge[a].push_back(b);
		edge[b].push_back(a);
	}

	power = vi(1+N);
	for(int i = 1; i <= N; i++)
	{
		char c;
		cin >> c;
		if(c == '1') power[i] = 1;
		else power[i] = 0;
	}

	dfs(1, 0);

	cout << res << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 6 ms 9720 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9744 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 6 ms 9684 KB Output is correct
13 Correct 7 ms 9684 KB Output is correct
14 Correct 8 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 7 ms 9664 KB Output is correct
17 Correct 7 ms 9684 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 5 ms 9684 KB Output is correct
21 Correct 5 ms 9716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 6 ms 9720 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9744 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 6 ms 9684 KB Output is correct
13 Correct 7 ms 9684 KB Output is correct
14 Correct 8 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 7 ms 9664 KB Output is correct
17 Correct 7 ms 9684 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 5 ms 9684 KB Output is correct
21 Correct 5 ms 9716 KB Output is correct
22 Correct 5 ms 9812 KB Output is correct
23 Correct 6 ms 9732 KB Output is correct
24 Correct 6 ms 9812 KB Output is correct
25 Correct 6 ms 9736 KB Output is correct
26 Correct 6 ms 9812 KB Output is correct
27 Correct 8 ms 9812 KB Output is correct
28 Correct 7 ms 9812 KB Output is correct
29 Correct 7 ms 9984 KB Output is correct
30 Correct 6 ms 9920 KB Output is correct
31 Correct 6 ms 9812 KB Output is correct
32 Correct 6 ms 9812 KB Output is correct
33 Correct 6 ms 9860 KB Output is correct
34 Correct 6 ms 9812 KB Output is correct
35 Correct 6 ms 9864 KB Output is correct
36 Correct 6 ms 9812 KB Output is correct
37 Correct 6 ms 9736 KB Output is correct
38 Correct 6 ms 9776 KB Output is correct
39 Correct 8 ms 9812 KB Output is correct
40 Correct 6 ms 9852 KB Output is correct
41 Correct 6 ms 9812 KB Output is correct
42 Correct 7 ms 9860 KB Output is correct
43 Correct 8 ms 9856 KB Output is correct
44 Correct 7 ms 9856 KB Output is correct
45 Correct 7 ms 9776 KB Output is correct
46 Correct 8 ms 9772 KB Output is correct
47 Correct 7 ms 9860 KB Output is correct
48 Correct 6 ms 9916 KB Output is correct
49 Correct 6 ms 9864 KB Output is correct
50 Correct 6 ms 9852 KB Output is correct
51 Correct 6 ms 9864 KB Output is correct
52 Correct 7 ms 9860 KB Output is correct
53 Correct 6 ms 9812 KB Output is correct
54 Correct 6 ms 9812 KB Output is correct
55 Correct 6 ms 9812 KB Output is correct
56 Correct 6 ms 9728 KB Output is correct
57 Correct 6 ms 9992 KB Output is correct
58 Correct 6 ms 9812 KB Output is correct
59 Correct 6 ms 9788 KB Output is correct
60 Correct 6 ms 9940 KB Output is correct
61 Correct 6 ms 9728 KB Output is correct
62 Correct 6 ms 9980 KB Output is correct
63 Correct 5 ms 9812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 6 ms 9720 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9744 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 6 ms 9684 KB Output is correct
13 Correct 7 ms 9684 KB Output is correct
14 Correct 8 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 7 ms 9664 KB Output is correct
17 Correct 7 ms 9684 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 5 ms 9684 KB Output is correct
21 Correct 5 ms 9716 KB Output is correct
22 Correct 5 ms 9812 KB Output is correct
23 Correct 6 ms 9732 KB Output is correct
24 Correct 6 ms 9812 KB Output is correct
25 Correct 6 ms 9736 KB Output is correct
26 Correct 6 ms 9812 KB Output is correct
27 Correct 8 ms 9812 KB Output is correct
28 Correct 7 ms 9812 KB Output is correct
29 Correct 7 ms 9984 KB Output is correct
30 Correct 6 ms 9920 KB Output is correct
31 Correct 6 ms 9812 KB Output is correct
32 Correct 6 ms 9812 KB Output is correct
33 Correct 6 ms 9860 KB Output is correct
34 Correct 6 ms 9812 KB Output is correct
35 Correct 6 ms 9864 KB Output is correct
36 Correct 6 ms 9812 KB Output is correct
37 Correct 6 ms 9736 KB Output is correct
38 Correct 6 ms 9776 KB Output is correct
39 Correct 8 ms 9812 KB Output is correct
40 Correct 6 ms 9852 KB Output is correct
41 Correct 6 ms 9812 KB Output is correct
42 Correct 7 ms 9860 KB Output is correct
43 Correct 8 ms 9856 KB Output is correct
44 Correct 7 ms 9856 KB Output is correct
45 Correct 7 ms 9776 KB Output is correct
46 Correct 8 ms 9772 KB Output is correct
47 Correct 7 ms 9860 KB Output is correct
48 Correct 6 ms 9916 KB Output is correct
49 Correct 6 ms 9864 KB Output is correct
50 Correct 6 ms 9852 KB Output is correct
51 Correct 6 ms 9864 KB Output is correct
52 Correct 7 ms 9860 KB Output is correct
53 Correct 6 ms 9812 KB Output is correct
54 Correct 6 ms 9812 KB Output is correct
55 Correct 6 ms 9812 KB Output is correct
56 Correct 6 ms 9728 KB Output is correct
57 Correct 6 ms 9992 KB Output is correct
58 Correct 6 ms 9812 KB Output is correct
59 Correct 6 ms 9788 KB Output is correct
60 Correct 6 ms 9940 KB Output is correct
61 Correct 6 ms 9728 KB Output is correct
62 Correct 6 ms 9980 KB Output is correct
63 Correct 5 ms 9812 KB Output is correct
64 Correct 148 ms 24088 KB Output is correct
65 Correct 168 ms 24056 KB Output is correct
66 Correct 181 ms 24072 KB Output is correct
67 Correct 177 ms 24100 KB Output is correct
68 Correct 186 ms 24028 KB Output is correct
69 Correct 165 ms 24048 KB Output is correct
70 Correct 169 ms 24056 KB Output is correct
71 Correct 172 ms 24140 KB Output is correct
72 Correct 142 ms 23980 KB Output is correct
73 Correct 174 ms 39228 KB Output is correct
74 Correct 185 ms 39164 KB Output is correct
75 Correct 160 ms 23316 KB Output is correct
76 Correct 188 ms 23432 KB Output is correct
77 Correct 170 ms 24956 KB Output is correct
78 Correct 161 ms 24940 KB Output is correct
79 Correct 163 ms 24916 KB Output is correct
80 Correct 169 ms 24828 KB Output is correct
81 Correct 118 ms 23332 KB Output is correct
82 Correct 153 ms 23860 KB Output is correct
83 Correct 183 ms 29520 KB Output is correct
84 Correct 159 ms 31712 KB Output is correct
85 Correct 183 ms 31416 KB Output is correct
86 Correct 181 ms 25360 KB Output is correct
87 Correct 163 ms 24836 KB Output is correct
88 Correct 178 ms 25264 KB Output is correct
89 Correct 135 ms 25264 KB Output is correct
90 Correct 151 ms 25024 KB Output is correct
91 Correct 141 ms 25360 KB Output is correct
92 Correct 144 ms 25588 KB Output is correct
93 Correct 159 ms 28252 KB Output is correct
94 Correct 155 ms 29472 KB Output is correct
95 Correct 133 ms 28344 KB Output is correct
96 Correct 173 ms 26556 KB Output is correct
97 Correct 136 ms 25004 KB Output is correct
98 Correct 143 ms 24616 KB Output is correct
99 Correct 170 ms 24352 KB Output is correct
100 Correct 137 ms 27332 KB Output is correct
101 Correct 140 ms 25308 KB Output is correct
102 Correct 168 ms 24724 KB Output is correct
103 Correct 141 ms 24604 KB Output is correct
104 Correct 80 ms 22564 KB Output is correct
105 Correct 173 ms 35984 KB Output is correct
106 Correct 117 ms 22396 KB Output is correct
107 Correct 88 ms 22412 KB Output is correct
108 Correct 170 ms 39980 KB Output is correct
109 Correct 119 ms 22676 KB Output is correct
110 Correct 193 ms 40328 KB Output is correct
111 Correct 150 ms 22284 KB Output is correct