Submission #320598

# Submission time Handle Problem Language Result Execution time Memory
320598 2020-11-09T08:25:03 Z GioChkhaidze Power Plant (JOI20_power) C++14
100 / 100
232 ms 26148 KB
#include <bits/stdc++.h>

#define pb push_back

using namespace std;
 
const int N=2e5+5;

string s; 
int n,a,b,dp[N];
vector < int > v[N];

void dfs(int x,int p) {
  int t=(s[x-1]=='1'),res=0;
  dp[x]=t;
  for (int i=0; i<v[x].size(); i++) {
    int to=v[x][i];
    if (to==p) continue;
    dfs(to,x);
    res+=dp[to];
  }
  res-=t;
  dp[x]=max(res,dp[x]);
}

int ans;
void Dfs(int x,int p,int res) {
  int t=(s[x-1]=='1'),S=res-t;
  
  for (int i=0; i<v[x].size(); i++) {
    int to=v[x][i];
    if (to==p) continue;
    S+=dp[to];
  }

  ans=max(ans,S);
  for (int i=0; i<v[x].size(); i++) {
    int to=v[x][i];
    if (to==p) continue;
    Dfs(to,x,max(t,S-dp[to]));
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL),cout.tie(NULL);
 
  cin>>n;
  for (int i=1; i<n; i++) {
    cin>>a>>b;
    v[a].pb(b);
    v[b].pb(a);
  }
  
  cin>>s;
  
  int cnt=0;
  for (int i=0; i<s.size(); i++) 
    if (s[i]=='1') cnt++;
    
  ans=min(2,cnt);
  
  dfs(1,1);
  Dfs(1,1,0);
  
  cout<<ans<<"\n";
}

Compilation message

power.cpp: In function 'void dfs(int, int)':
power.cpp:16:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |   for (int i=0; i<v[x].size(); i++) {
      |                 ~^~~~~~~~~~~~
power.cpp: In function 'void Dfs(int, int, int)':
power.cpp:30:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |   for (int i=0; i<v[x].size(); i++) {
      |                 ~^~~~~~~~~~~~
power.cpp:37:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |   for (int i=0; i<v[x].size(); i++) {
      |                 ~^~~~~~~~~~~~
power.cpp: In function 'int main()':
power.cpp:58:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |   for (int i=0; i<s.size(); i++)
      |                 ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4972 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 3 ms 5100 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 3 ms 4972 KB Output is correct
6 Correct 3 ms 4972 KB Output is correct
7 Correct 3 ms 5100 KB Output is correct
8 Correct 4 ms 4972 KB Output is correct
9 Correct 4 ms 5120 KB Output is correct
10 Correct 3 ms 5100 KB Output is correct
11 Correct 3 ms 5100 KB Output is correct
12 Correct 3 ms 4972 KB Output is correct
13 Correct 3 ms 4972 KB Output is correct
14 Correct 3 ms 5100 KB Output is correct
15 Correct 4 ms 4972 KB Output is correct
16 Correct 3 ms 5100 KB Output is correct
17 Correct 3 ms 4972 KB Output is correct
18 Correct 3 ms 4972 KB Output is correct
19 Correct 3 ms 4972 KB Output is correct
20 Correct 3 ms 5100 KB Output is correct
21 Correct 4 ms 4972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4972 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 3 ms 5100 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 3 ms 4972 KB Output is correct
6 Correct 3 ms 4972 KB Output is correct
7 Correct 3 ms 5100 KB Output is correct
8 Correct 4 ms 4972 KB Output is correct
9 Correct 4 ms 5120 KB Output is correct
10 Correct 3 ms 5100 KB Output is correct
11 Correct 3 ms 5100 KB Output is correct
12 Correct 3 ms 4972 KB Output is correct
13 Correct 3 ms 4972 KB Output is correct
14 Correct 3 ms 5100 KB Output is correct
15 Correct 4 ms 4972 KB Output is correct
16 Correct 3 ms 5100 KB Output is correct
17 Correct 3 ms 4972 KB Output is correct
18 Correct 3 ms 4972 KB Output is correct
19 Correct 3 ms 4972 KB Output is correct
20 Correct 3 ms 5100 KB Output is correct
21 Correct 4 ms 4972 KB Output is correct
22 Correct 4 ms 5100 KB Output is correct
23 Correct 5 ms 5100 KB Output is correct
24 Correct 4 ms 5100 KB Output is correct
25 Correct 5 ms 5100 KB Output is correct
26 Correct 4 ms 5100 KB Output is correct
27 Correct 4 ms 5100 KB Output is correct
28 Correct 5 ms 5228 KB Output is correct
29 Correct 4 ms 5228 KB Output is correct
30 Correct 4 ms 5228 KB Output is correct
31 Correct 4 ms 5100 KB Output is correct
32 Correct 4 ms 5100 KB Output is correct
33 Correct 4 ms 5100 KB Output is correct
34 Correct 4 ms 5100 KB Output is correct
35 Correct 4 ms 5100 KB Output is correct
36 Correct 4 ms 5100 KB Output is correct
37 Correct 5 ms 5100 KB Output is correct
38 Correct 4 ms 5100 KB Output is correct
39 Correct 4 ms 5228 KB Output is correct
40 Correct 4 ms 5100 KB Output is correct
41 Correct 4 ms 5228 KB Output is correct
42 Correct 4 ms 5100 KB Output is correct
43 Correct 4 ms 5100 KB Output is correct
44 Correct 4 ms 5100 KB Output is correct
45 Correct 4 ms 5100 KB Output is correct
46 Correct 4 ms 5100 KB Output is correct
47 Correct 4 ms 5100 KB Output is correct
48 Correct 4 ms 5228 KB Output is correct
49 Correct 4 ms 5228 KB Output is correct
50 Correct 4 ms 5100 KB Output is correct
51 Correct 4 ms 5100 KB Output is correct
52 Correct 4 ms 5100 KB Output is correct
53 Correct 4 ms 5100 KB Output is correct
54 Correct 4 ms 5100 KB Output is correct
55 Correct 4 ms 5100 KB Output is correct
56 Correct 4 ms 5100 KB Output is correct
57 Correct 4 ms 5228 KB Output is correct
58 Correct 4 ms 5100 KB Output is correct
59 Correct 4 ms 5100 KB Output is correct
60 Correct 4 ms 5228 KB Output is correct
61 Correct 4 ms 5100 KB Output is correct
62 Correct 4 ms 5228 KB Output is correct
63 Correct 4 ms 5100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4972 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 3 ms 5100 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 3 ms 4972 KB Output is correct
6 Correct 3 ms 4972 KB Output is correct
7 Correct 3 ms 5100 KB Output is correct
8 Correct 4 ms 4972 KB Output is correct
9 Correct 4 ms 5120 KB Output is correct
10 Correct 3 ms 5100 KB Output is correct
11 Correct 3 ms 5100 KB Output is correct
12 Correct 3 ms 4972 KB Output is correct
13 Correct 3 ms 4972 KB Output is correct
14 Correct 3 ms 5100 KB Output is correct
15 Correct 4 ms 4972 KB Output is correct
16 Correct 3 ms 5100 KB Output is correct
17 Correct 3 ms 4972 KB Output is correct
18 Correct 3 ms 4972 KB Output is correct
19 Correct 3 ms 4972 KB Output is correct
20 Correct 3 ms 5100 KB Output is correct
21 Correct 4 ms 4972 KB Output is correct
22 Correct 4 ms 5100 KB Output is correct
23 Correct 5 ms 5100 KB Output is correct
24 Correct 4 ms 5100 KB Output is correct
25 Correct 5 ms 5100 KB Output is correct
26 Correct 4 ms 5100 KB Output is correct
27 Correct 4 ms 5100 KB Output is correct
28 Correct 5 ms 5228 KB Output is correct
29 Correct 4 ms 5228 KB Output is correct
30 Correct 4 ms 5228 KB Output is correct
31 Correct 4 ms 5100 KB Output is correct
32 Correct 4 ms 5100 KB Output is correct
33 Correct 4 ms 5100 KB Output is correct
34 Correct 4 ms 5100 KB Output is correct
35 Correct 4 ms 5100 KB Output is correct
36 Correct 4 ms 5100 KB Output is correct
37 Correct 5 ms 5100 KB Output is correct
38 Correct 4 ms 5100 KB Output is correct
39 Correct 4 ms 5228 KB Output is correct
40 Correct 4 ms 5100 KB Output is correct
41 Correct 4 ms 5228 KB Output is correct
42 Correct 4 ms 5100 KB Output is correct
43 Correct 4 ms 5100 KB Output is correct
44 Correct 4 ms 5100 KB Output is correct
45 Correct 4 ms 5100 KB Output is correct
46 Correct 4 ms 5100 KB Output is correct
47 Correct 4 ms 5100 KB Output is correct
48 Correct 4 ms 5228 KB Output is correct
49 Correct 4 ms 5228 KB Output is correct
50 Correct 4 ms 5100 KB Output is correct
51 Correct 4 ms 5100 KB Output is correct
52 Correct 4 ms 5100 KB Output is correct
53 Correct 4 ms 5100 KB Output is correct
54 Correct 4 ms 5100 KB Output is correct
55 Correct 4 ms 5100 KB Output is correct
56 Correct 4 ms 5100 KB Output is correct
57 Correct 4 ms 5228 KB Output is correct
58 Correct 4 ms 5100 KB Output is correct
59 Correct 4 ms 5100 KB Output is correct
60 Correct 4 ms 5228 KB Output is correct
61 Correct 4 ms 5100 KB Output is correct
62 Correct 4 ms 5228 KB Output is correct
63 Correct 4 ms 5100 KB Output is correct
64 Correct 191 ms 12456 KB Output is correct
65 Correct 195 ms 12580 KB Output is correct
66 Correct 192 ms 12560 KB Output is correct
67 Correct 185 ms 12452 KB Output is correct
68 Correct 195 ms 12492 KB Output is correct
69 Correct 186 ms 12448 KB Output is correct
70 Correct 189 ms 12512 KB Output is correct
71 Correct 185 ms 12464 KB Output is correct
72 Correct 188 ms 12576 KB Output is correct
73 Correct 228 ms 25448 KB Output is correct
74 Correct 225 ms 25124 KB Output is correct
75 Correct 184 ms 12448 KB Output is correct
76 Correct 192 ms 12456 KB Output is correct
77 Correct 200 ms 12448 KB Output is correct
78 Correct 220 ms 12448 KB Output is correct
79 Correct 224 ms 12608 KB Output is correct
80 Correct 200 ms 12448 KB Output is correct
81 Correct 177 ms 12892 KB Output is correct
82 Correct 178 ms 12644 KB Output is correct
83 Correct 209 ms 17576 KB Output is correct
84 Correct 212 ms 19760 KB Output is correct
85 Correct 214 ms 19376 KB Output is correct
86 Correct 202 ms 14116 KB Output is correct
87 Correct 195 ms 13216 KB Output is correct
88 Correct 184 ms 13792 KB Output is correct
89 Correct 190 ms 13788 KB Output is correct
90 Correct 190 ms 13572 KB Output is correct
91 Correct 189 ms 13832 KB Output is correct
92 Correct 195 ms 14096 KB Output is correct
93 Correct 183 ms 16500 KB Output is correct
94 Correct 187 ms 18400 KB Output is correct
95 Correct 193 ms 17244 KB Output is correct
96 Correct 182 ms 15120 KB Output is correct
97 Correct 191 ms 13692 KB Output is correct
98 Correct 178 ms 13268 KB Output is correct
99 Correct 183 ms 12968 KB Output is correct
100 Correct 197 ms 15992 KB Output is correct
101 Correct 193 ms 14112 KB Output is correct
102 Correct 181 ms 13300 KB Output is correct
103 Correct 183 ms 13124 KB Output is correct
104 Correct 121 ms 13272 KB Output is correct
105 Correct 226 ms 21924 KB Output is correct
106 Correct 132 ms 13400 KB Output is correct
107 Correct 114 ms 13400 KB Output is correct
108 Correct 232 ms 26148 KB Output is correct
109 Correct 113 ms 13272 KB Output is correct
110 Correct 232 ms 26148 KB Output is correct
111 Correct 185 ms 12464 KB Output is correct