#include <bits/stdc++.h>
using namespace std;
const int N = 16;
int n, mr[N], sz[N];
vector<int> adj[N];
string s;
void dfs1(int x, int pr) {
sz[x] = (mr[x] == 1 && s[x] == '1');
for(auto e : adj[x]) {
if(e != pr) {
dfs1(e, x);
sz[x] += sz[e];
}
}
}
int res = 0;
void dfs2(int x, int pr) {
int cnt = (sz[0] - sz[x] > 0);
for(auto e : adj[x]) {
if(e != pr) {
cnt += (sz[e] > 0);
dfs2(e, x);
}
}
if(s[x] == '1' && mr[x] && !(cnt > 1)) res ++;
if(s[x] == '1' && (cnt > 1)) res --;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for(int i = 0;i < n - 1;i ++) {
int x, y;
cin >> x >> y;
x --,y --;
adj[x].push_back(y);
adj[y].push_back(x);
}
cin >> s;
int ans = 0;
int ans_mask = -1;
for(int mask = 0;mask < (1<<n);mask ++) {
for(int i = 0;i < n;i ++) {
if(((1<<i)&mask) && s[i] == '1') {
mr[i] = 1;
} else {
mr[i] = 0;
}
}
res = 0;
dfs1(0, -1);
dfs2(0, -1);
if(res > ans) {
ans = res;
ans_mask = mask;
}
}
cout << ans << '\n';
}
Compilation message
power.cpp: In function 'int main()':
power.cpp:49:6: warning: variable 'ans_mask' set but not used [-Wunused-but-set-variable]
49 | int ans_mask = -1;
| ^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
23 ms |
364 KB |
Output is correct |
3 |
Correct |
20 ms |
364 KB |
Output is correct |
4 |
Correct |
22 ms |
364 KB |
Output is correct |
5 |
Correct |
23 ms |
364 KB |
Output is correct |
6 |
Correct |
23 ms |
364 KB |
Output is correct |
7 |
Correct |
25 ms |
364 KB |
Output is correct |
8 |
Correct |
27 ms |
364 KB |
Output is correct |
9 |
Correct |
12 ms |
364 KB |
Output is correct |
10 |
Correct |
24 ms |
364 KB |
Output is correct |
11 |
Correct |
25 ms |
364 KB |
Output is correct |
12 |
Correct |
23 ms |
364 KB |
Output is correct |
13 |
Correct |
22 ms |
364 KB |
Output is correct |
14 |
Correct |
25 ms |
364 KB |
Output is correct |
15 |
Correct |
25 ms |
364 KB |
Output is correct |
16 |
Correct |
26 ms |
364 KB |
Output is correct |
17 |
Correct |
24 ms |
364 KB |
Output is correct |
18 |
Correct |
24 ms |
364 KB |
Output is correct |
19 |
Correct |
22 ms |
364 KB |
Output is correct |
20 |
Correct |
23 ms |
364 KB |
Output is correct |
21 |
Correct |
24 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
23 ms |
364 KB |
Output is correct |
3 |
Correct |
20 ms |
364 KB |
Output is correct |
4 |
Correct |
22 ms |
364 KB |
Output is correct |
5 |
Correct |
23 ms |
364 KB |
Output is correct |
6 |
Correct |
23 ms |
364 KB |
Output is correct |
7 |
Correct |
25 ms |
364 KB |
Output is correct |
8 |
Correct |
27 ms |
364 KB |
Output is correct |
9 |
Correct |
12 ms |
364 KB |
Output is correct |
10 |
Correct |
24 ms |
364 KB |
Output is correct |
11 |
Correct |
25 ms |
364 KB |
Output is correct |
12 |
Correct |
23 ms |
364 KB |
Output is correct |
13 |
Correct |
22 ms |
364 KB |
Output is correct |
14 |
Correct |
25 ms |
364 KB |
Output is correct |
15 |
Correct |
25 ms |
364 KB |
Output is correct |
16 |
Correct |
26 ms |
364 KB |
Output is correct |
17 |
Correct |
24 ms |
364 KB |
Output is correct |
18 |
Correct |
24 ms |
364 KB |
Output is correct |
19 |
Correct |
22 ms |
364 KB |
Output is correct |
20 |
Correct |
23 ms |
364 KB |
Output is correct |
21 |
Correct |
24 ms |
364 KB |
Output is correct |
22 |
Runtime error |
1 ms |
492 KB |
Execution killed with signal 11 |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
23 ms |
364 KB |
Output is correct |
3 |
Correct |
20 ms |
364 KB |
Output is correct |
4 |
Correct |
22 ms |
364 KB |
Output is correct |
5 |
Correct |
23 ms |
364 KB |
Output is correct |
6 |
Correct |
23 ms |
364 KB |
Output is correct |
7 |
Correct |
25 ms |
364 KB |
Output is correct |
8 |
Correct |
27 ms |
364 KB |
Output is correct |
9 |
Correct |
12 ms |
364 KB |
Output is correct |
10 |
Correct |
24 ms |
364 KB |
Output is correct |
11 |
Correct |
25 ms |
364 KB |
Output is correct |
12 |
Correct |
23 ms |
364 KB |
Output is correct |
13 |
Correct |
22 ms |
364 KB |
Output is correct |
14 |
Correct |
25 ms |
364 KB |
Output is correct |
15 |
Correct |
25 ms |
364 KB |
Output is correct |
16 |
Correct |
26 ms |
364 KB |
Output is correct |
17 |
Correct |
24 ms |
364 KB |
Output is correct |
18 |
Correct |
24 ms |
364 KB |
Output is correct |
19 |
Correct |
22 ms |
364 KB |
Output is correct |
20 |
Correct |
23 ms |
364 KB |
Output is correct |
21 |
Correct |
24 ms |
364 KB |
Output is correct |
22 |
Runtime error |
1 ms |
492 KB |
Execution killed with signal 11 |
23 |
Halted |
0 ms |
0 KB |
- |