답안 #624383

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
624383 2022-08-08T05:44:17 Z pooyashams Power Plant (JOI20_power) C++14
100 / 100
153 ms 35172 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstring>
#include <iomanip>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#define endl '\n'
#define X first
#define Y second

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

const int maxn = 2e5+10;

vector<int> G[maxn];
int dp[maxn][2];
string s;

void dfs(int v, int p)
{
	int a = 0;
	int b = 0;
	int c = 0;
	int k = 0;
	for(int u: G[v])
	{
		if(u == p) continue;
		dfs(u, v);
		a += dp[u][1];
		b = max(b, dp[u][0]);
		c = max(c, dp[u][1]);
		k++;
	}
	if(k == 0)
	{
		dp[v][0] = dp[v][1] = (s[v]-'0');
	}
	else if(k == 1)
	{
		if(s[v] == '1')
		{
			dp[v][0] = max(c+1, b);
			dp[v][1] = max(1, a-1);
		}
		else
		{
			dp[v][0] = b;
			dp[v][1] = c;
		}
	}
	else
	{
		if(s[v] == '1')
		{
			dp[v][0] = max( max(1, a-1), max(c+1, b) );
			dp[v][1] = max(1, a-1);
		}
		else
		{
			dp[v][0] = max(a, b);
			dp[v][1] = a;
		}
	}
}

int32_t main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n; cin >> n;
	for(int i = 1; i < n; i++)
	{
		int x, y; cin >> x >> y;
		x--; y--;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	cin >> s;
	dfs(0, 0);
	cout << dp[0][0] << endl;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5020 KB Output is correct
4 Correct 3 ms 5028 KB Output is correct
5 Correct 3 ms 5020 KB Output is correct
6 Correct 2 ms 5024 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 5020 KB Output is correct
10 Correct 3 ms 5024 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 5020 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 5020 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5020 KB Output is correct
4 Correct 3 ms 5028 KB Output is correct
5 Correct 3 ms 5020 KB Output is correct
6 Correct 2 ms 5024 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 5020 KB Output is correct
10 Correct 3 ms 5024 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 5020 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 5020 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 5032 KB Output is correct
23 Correct 4 ms 5040 KB Output is correct
24 Correct 4 ms 5076 KB Output is correct
25 Correct 6 ms 5076 KB Output is correct
26 Correct 4 ms 5076 KB Output is correct
27 Correct 4 ms 5036 KB Output is correct
28 Correct 4 ms 5076 KB Output is correct
29 Correct 3 ms 5204 KB Output is correct
30 Correct 3 ms 5204 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 4 ms 5076 KB Output is correct
33 Correct 4 ms 5036 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 3 ms 5032 KB Output is correct
37 Correct 3 ms 5076 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 4 ms 5204 KB Output is correct
40 Correct 4 ms 5076 KB Output is correct
41 Correct 3 ms 5204 KB Output is correct
42 Correct 5 ms 5076 KB Output is correct
43 Correct 3 ms 5040 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
45 Correct 5 ms 5076 KB Output is correct
46 Correct 3 ms 5076 KB Output is correct
47 Correct 3 ms 5076 KB Output is correct
48 Correct 3 ms 5160 KB Output is correct
49 Correct 3 ms 5160 KB Output is correct
50 Correct 4 ms 5076 KB Output is correct
51 Correct 3 ms 5076 KB Output is correct
52 Correct 3 ms 5032 KB Output is correct
53 Correct 3 ms 5080 KB Output is correct
54 Correct 3 ms 5076 KB Output is correct
55 Correct 4 ms 5076 KB Output is correct
56 Correct 4 ms 5040 KB Output is correct
57 Correct 3 ms 5204 KB Output is correct
58 Correct 3 ms 5076 KB Output is correct
59 Correct 4 ms 5076 KB Output is correct
60 Correct 3 ms 5204 KB Output is correct
61 Correct 4 ms 5076 KB Output is correct
62 Correct 3 ms 5204 KB Output is correct
63 Correct 3 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5020 KB Output is correct
4 Correct 3 ms 5028 KB Output is correct
5 Correct 3 ms 5020 KB Output is correct
6 Correct 2 ms 5024 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 5020 KB Output is correct
10 Correct 3 ms 5024 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 5020 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 5020 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 5032 KB Output is correct
23 Correct 4 ms 5040 KB Output is correct
24 Correct 4 ms 5076 KB Output is correct
25 Correct 6 ms 5076 KB Output is correct
26 Correct 4 ms 5076 KB Output is correct
27 Correct 4 ms 5036 KB Output is correct
28 Correct 4 ms 5076 KB Output is correct
29 Correct 3 ms 5204 KB Output is correct
30 Correct 3 ms 5204 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 4 ms 5076 KB Output is correct
33 Correct 4 ms 5036 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 3 ms 5032 KB Output is correct
37 Correct 3 ms 5076 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 4 ms 5204 KB Output is correct
40 Correct 4 ms 5076 KB Output is correct
41 Correct 3 ms 5204 KB Output is correct
42 Correct 5 ms 5076 KB Output is correct
43 Correct 3 ms 5040 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
45 Correct 5 ms 5076 KB Output is correct
46 Correct 3 ms 5076 KB Output is correct
47 Correct 3 ms 5076 KB Output is correct
48 Correct 3 ms 5160 KB Output is correct
49 Correct 3 ms 5160 KB Output is correct
50 Correct 4 ms 5076 KB Output is correct
51 Correct 3 ms 5076 KB Output is correct
52 Correct 3 ms 5032 KB Output is correct
53 Correct 3 ms 5080 KB Output is correct
54 Correct 3 ms 5076 KB Output is correct
55 Correct 4 ms 5076 KB Output is correct
56 Correct 4 ms 5040 KB Output is correct
57 Correct 3 ms 5204 KB Output is correct
58 Correct 3 ms 5076 KB Output is correct
59 Correct 4 ms 5076 KB Output is correct
60 Correct 3 ms 5204 KB Output is correct
61 Correct 4 ms 5076 KB Output is correct
62 Correct 3 ms 5204 KB Output is correct
63 Correct 3 ms 5076 KB Output is correct
64 Correct 122 ms 16000 KB Output is correct
65 Correct 111 ms 15888 KB Output is correct
66 Correct 147 ms 16004 KB Output is correct
67 Correct 122 ms 15984 KB Output is correct
68 Correct 116 ms 15984 KB Output is correct
69 Correct 130 ms 15984 KB Output is correct
70 Correct 116 ms 15992 KB Output is correct
71 Correct 113 ms 15900 KB Output is correct
72 Correct 114 ms 15988 KB Output is correct
73 Correct 126 ms 33568 KB Output is correct
74 Correct 147 ms 33604 KB Output is correct
75 Correct 106 ms 15844 KB Output is correct
76 Correct 113 ms 15936 KB Output is correct
77 Correct 112 ms 15848 KB Output is correct
78 Correct 105 ms 15784 KB Output is correct
79 Correct 122 ms 15860 KB Output is correct
80 Correct 116 ms 15788 KB Output is correct
81 Correct 99 ms 16260 KB Output is correct
82 Correct 105 ms 15976 KB Output is correct
83 Correct 119 ms 23136 KB Output is correct
84 Correct 153 ms 26136 KB Output is correct
85 Correct 130 ms 25776 KB Output is correct
86 Correct 114 ms 17784 KB Output is correct
87 Correct 118 ms 16968 KB Output is correct
88 Correct 121 ms 17352 KB Output is correct
89 Correct 102 ms 17472 KB Output is correct
90 Correct 107 ms 17104 KB Output is correct
91 Correct 114 ms 17556 KB Output is correct
92 Correct 114 ms 17984 KB Output is correct
93 Correct 107 ms 21336 KB Output is correct
94 Correct 113 ms 23844 KB Output is correct
95 Correct 102 ms 21896 KB Output is correct
96 Correct 137 ms 19424 KB Output is correct
97 Correct 113 ms 17232 KB Output is correct
98 Correct 110 ms 16572 KB Output is correct
99 Correct 110 ms 16296 KB Output is correct
100 Correct 99 ms 20732 KB Output is correct
101 Correct 98 ms 17960 KB Output is correct
102 Correct 104 ms 16840 KB Output is correct
103 Correct 136 ms 16512 KB Output is correct
104 Correct 86 ms 16760 KB Output is correct
105 Correct 125 ms 29196 KB Output is correct
106 Correct 63 ms 16688 KB Output is correct
107 Correct 66 ms 16644 KB Output is correct
108 Correct 144 ms 34700 KB Output is correct
109 Correct 83 ms 16748 KB Output is correct
110 Correct 123 ms 35172 KB Output is correct
111 Correct 111 ms 15936 KB Output is correct