#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
vector<vi> G;
string x;
set<int> br;
bool dfs(int u,int p,int b){
for(auto &v:G[u]){
if(v==b) return 1;
if(v!=p){
if(dfs(v,u,b)){
if(x[v]=='1') br.insert(v);
return 1;
}
}
}
return 0;
}
int main(){
int n;
cin>>n;
G.resize(n+1);
for(int i=0;i<n-1;i++){
int a,b; cin>>a>>b;
a--; b--;
G[a].push_back(b);
G[b].push_back(a);
}
cin>>x;
int rp=-1;
for(int i=0;i<(1<<n);i++){
//cout<<i<<endl;
vector<int> po;
int c=0;
for(int j=0;j<n;j++){
if(i&(1<<j) && x[j]=='1'){
c++;
po.push_back(j);
}
}
br.clear();
for(int j=0;j<po.size();j++){
for(int k=j+1;k<po.size();k++){
dfs(po[j],-1,po[k]);
}
}
for(int j=0;j<po.size();j++){
if(br.find(po[j])!=br.end()) c--;
}
rp=max(rp,c-(int)br.size());
}
cout<<rp<<endl;
}
Compilation message
power.cpp: In function 'int main()':
power.cpp:43:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int j=0;j<po.size();j++){
| ~^~~~~~~~~~
power.cpp:44:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int k=j+1;k<po.size();k++){
| ~^~~~~~~~~~
power.cpp:48:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for(int j=0;j<po.size();j++){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
6 ms |
204 KB |
Output is correct |
3 |
Correct |
17 ms |
292 KB |
Output is correct |
4 |
Correct |
47 ms |
272 KB |
Output is correct |
5 |
Correct |
116 ms |
272 KB |
Output is correct |
6 |
Correct |
81 ms |
204 KB |
Output is correct |
7 |
Correct |
266 ms |
204 KB |
Output is correct |
8 |
Correct |
327 ms |
272 KB |
Output is correct |
9 |
Correct |
105 ms |
272 KB |
Output is correct |
10 |
Correct |
99 ms |
204 KB |
Output is correct |
11 |
Correct |
288 ms |
276 KB |
Output is correct |
12 |
Correct |
128 ms |
268 KB |
Output is correct |
13 |
Correct |
114 ms |
268 KB |
Output is correct |
14 |
Correct |
133 ms |
268 KB |
Output is correct |
15 |
Correct |
23 ms |
204 KB |
Output is correct |
16 |
Correct |
27 ms |
296 KB |
Output is correct |
17 |
Correct |
139 ms |
204 KB |
Output is correct |
18 |
Correct |
133 ms |
280 KB |
Output is correct |
19 |
Correct |
12 ms |
204 KB |
Output is correct |
20 |
Correct |
67 ms |
280 KB |
Output is correct |
21 |
Correct |
241 ms |
284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
6 ms |
204 KB |
Output is correct |
3 |
Correct |
17 ms |
292 KB |
Output is correct |
4 |
Correct |
47 ms |
272 KB |
Output is correct |
5 |
Correct |
116 ms |
272 KB |
Output is correct |
6 |
Correct |
81 ms |
204 KB |
Output is correct |
7 |
Correct |
266 ms |
204 KB |
Output is correct |
8 |
Correct |
327 ms |
272 KB |
Output is correct |
9 |
Correct |
105 ms |
272 KB |
Output is correct |
10 |
Correct |
99 ms |
204 KB |
Output is correct |
11 |
Correct |
288 ms |
276 KB |
Output is correct |
12 |
Correct |
128 ms |
268 KB |
Output is correct |
13 |
Correct |
114 ms |
268 KB |
Output is correct |
14 |
Correct |
133 ms |
268 KB |
Output is correct |
15 |
Correct |
23 ms |
204 KB |
Output is correct |
16 |
Correct |
27 ms |
296 KB |
Output is correct |
17 |
Correct |
139 ms |
204 KB |
Output is correct |
18 |
Correct |
133 ms |
280 KB |
Output is correct |
19 |
Correct |
12 ms |
204 KB |
Output is correct |
20 |
Correct |
67 ms |
280 KB |
Output is correct |
21 |
Correct |
241 ms |
284 KB |
Output is correct |
22 |
Execution timed out |
1580 ms |
332 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
6 ms |
204 KB |
Output is correct |
3 |
Correct |
17 ms |
292 KB |
Output is correct |
4 |
Correct |
47 ms |
272 KB |
Output is correct |
5 |
Correct |
116 ms |
272 KB |
Output is correct |
6 |
Correct |
81 ms |
204 KB |
Output is correct |
7 |
Correct |
266 ms |
204 KB |
Output is correct |
8 |
Correct |
327 ms |
272 KB |
Output is correct |
9 |
Correct |
105 ms |
272 KB |
Output is correct |
10 |
Correct |
99 ms |
204 KB |
Output is correct |
11 |
Correct |
288 ms |
276 KB |
Output is correct |
12 |
Correct |
128 ms |
268 KB |
Output is correct |
13 |
Correct |
114 ms |
268 KB |
Output is correct |
14 |
Correct |
133 ms |
268 KB |
Output is correct |
15 |
Correct |
23 ms |
204 KB |
Output is correct |
16 |
Correct |
27 ms |
296 KB |
Output is correct |
17 |
Correct |
139 ms |
204 KB |
Output is correct |
18 |
Correct |
133 ms |
280 KB |
Output is correct |
19 |
Correct |
12 ms |
204 KB |
Output is correct |
20 |
Correct |
67 ms |
280 KB |
Output is correct |
21 |
Correct |
241 ms |
284 KB |
Output is correct |
22 |
Execution timed out |
1580 ms |
332 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |