답안 #651543

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
651543 2022-10-19T09:23:08 Z czhang2718 Power Plant (JOI20_power) C++17
100 / 100
154 ms 35992 KB
#include<bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i=a; i<=b; i++)
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define pb push_back
#define f first
#define s second
#define nl '\n'
typedef long long ll;
// #define int ll
typedef vector<int> vi;
typedef pair<int, int> pii;
#define nl '\n'

const int N=5e5;
int n;
vi adj[N];
string on;
int dp[N];
int ans=-1e9;

void dfs(int x, int par){
    int mx=0;
    for(int k:adj[x]){
        if(k==par) continue;
        dfs(k, x);
        mx=max(mx, dp[k]);
        dp[x]+=max(0, dp[k]);
    }
    if(on[x]=='1') ans=max(ans, mx+1);
    ans=max(ans, dp[x]-(on[x]=='1'));
    dp[x]=max(dp[x]-(on[x]=='1'), int(on[x]=='1'?1:-1e9));
}

signed main(){
    cin.tie(0)->sync_with_stdio(0);

    cin >> n;
    rep(i,1,n-1){
        int u,v; cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    cin >> on;
    on='0'+on;

    dfs(1, 0);
    cout << ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Correct 7 ms 11968 KB Output is correct
3 Correct 7 ms 11988 KB Output is correct
4 Correct 7 ms 11964 KB Output is correct
5 Correct 8 ms 11948 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 7 ms 11988 KB Output is correct
8 Correct 7 ms 12072 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 6 ms 11988 KB Output is correct
12 Correct 7 ms 12068 KB Output is correct
13 Correct 6 ms 11988 KB Output is correct
14 Correct 6 ms 12072 KB Output is correct
15 Correct 7 ms 11988 KB Output is correct
16 Correct 7 ms 11988 KB Output is correct
17 Correct 7 ms 11988 KB Output is correct
18 Correct 7 ms 11988 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 7 ms 11988 KB Output is correct
21 Correct 6 ms 12032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Correct 7 ms 11968 KB Output is correct
3 Correct 7 ms 11988 KB Output is correct
4 Correct 7 ms 11964 KB Output is correct
5 Correct 8 ms 11948 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 7 ms 11988 KB Output is correct
8 Correct 7 ms 12072 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 6 ms 11988 KB Output is correct
12 Correct 7 ms 12068 KB Output is correct
13 Correct 6 ms 11988 KB Output is correct
14 Correct 6 ms 12072 KB Output is correct
15 Correct 7 ms 11988 KB Output is correct
16 Correct 7 ms 11988 KB Output is correct
17 Correct 7 ms 11988 KB Output is correct
18 Correct 7 ms 11988 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 7 ms 11988 KB Output is correct
21 Correct 6 ms 12032 KB Output is correct
22 Correct 7 ms 12132 KB Output is correct
23 Correct 7 ms 12116 KB Output is correct
24 Correct 8 ms 12084 KB Output is correct
25 Correct 7 ms 12116 KB Output is correct
26 Correct 8 ms 12084 KB Output is correct
27 Correct 7 ms 12116 KB Output is correct
28 Correct 8 ms 12116 KB Output is correct
29 Correct 7 ms 12212 KB Output is correct
30 Correct 7 ms 12208 KB Output is correct
31 Correct 7 ms 12116 KB Output is correct
32 Correct 7 ms 12080 KB Output is correct
33 Correct 8 ms 12244 KB Output is correct
34 Correct 8 ms 12116 KB Output is correct
35 Correct 7 ms 12116 KB Output is correct
36 Correct 7 ms 12116 KB Output is correct
37 Correct 7 ms 12132 KB Output is correct
38 Correct 7 ms 12132 KB Output is correct
39 Correct 7 ms 12116 KB Output is correct
40 Correct 8 ms 12244 KB Output is correct
41 Correct 8 ms 12116 KB Output is correct
42 Correct 7 ms 12084 KB Output is correct
43 Correct 7 ms 12116 KB Output is correct
44 Correct 7 ms 12216 KB Output is correct
45 Correct 7 ms 12084 KB Output is correct
46 Correct 7 ms 12116 KB Output is correct
47 Correct 7 ms 12084 KB Output is correct
48 Correct 7 ms 12244 KB Output is correct
49 Correct 7 ms 12244 KB Output is correct
50 Correct 7 ms 12116 KB Output is correct
51 Correct 7 ms 12180 KB Output is correct
52 Correct 7 ms 12116 KB Output is correct
53 Correct 7 ms 12080 KB Output is correct
54 Correct 8 ms 12116 KB Output is correct
55 Correct 7 ms 12084 KB Output is correct
56 Correct 7 ms 12116 KB Output is correct
57 Correct 8 ms 12212 KB Output is correct
58 Correct 8 ms 12080 KB Output is correct
59 Correct 7 ms 12116 KB Output is correct
60 Correct 7 ms 12116 KB Output is correct
61 Correct 8 ms 12116 KB Output is correct
62 Correct 7 ms 12208 KB Output is correct
63 Correct 7 ms 12160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Correct 7 ms 11968 KB Output is correct
3 Correct 7 ms 11988 KB Output is correct
4 Correct 7 ms 11964 KB Output is correct
5 Correct 8 ms 11948 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 7 ms 11988 KB Output is correct
8 Correct 7 ms 12072 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 6 ms 11988 KB Output is correct
12 Correct 7 ms 12068 KB Output is correct
13 Correct 6 ms 11988 KB Output is correct
14 Correct 6 ms 12072 KB Output is correct
15 Correct 7 ms 11988 KB Output is correct
16 Correct 7 ms 11988 KB Output is correct
17 Correct 7 ms 11988 KB Output is correct
18 Correct 7 ms 11988 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 7 ms 11988 KB Output is correct
21 Correct 6 ms 12032 KB Output is correct
22 Correct 7 ms 12132 KB Output is correct
23 Correct 7 ms 12116 KB Output is correct
24 Correct 8 ms 12084 KB Output is correct
25 Correct 7 ms 12116 KB Output is correct
26 Correct 8 ms 12084 KB Output is correct
27 Correct 7 ms 12116 KB Output is correct
28 Correct 8 ms 12116 KB Output is correct
29 Correct 7 ms 12212 KB Output is correct
30 Correct 7 ms 12208 KB Output is correct
31 Correct 7 ms 12116 KB Output is correct
32 Correct 7 ms 12080 KB Output is correct
33 Correct 8 ms 12244 KB Output is correct
34 Correct 8 ms 12116 KB Output is correct
35 Correct 7 ms 12116 KB Output is correct
36 Correct 7 ms 12116 KB Output is correct
37 Correct 7 ms 12132 KB Output is correct
38 Correct 7 ms 12132 KB Output is correct
39 Correct 7 ms 12116 KB Output is correct
40 Correct 8 ms 12244 KB Output is correct
41 Correct 8 ms 12116 KB Output is correct
42 Correct 7 ms 12084 KB Output is correct
43 Correct 7 ms 12116 KB Output is correct
44 Correct 7 ms 12216 KB Output is correct
45 Correct 7 ms 12084 KB Output is correct
46 Correct 7 ms 12116 KB Output is correct
47 Correct 7 ms 12084 KB Output is correct
48 Correct 7 ms 12244 KB Output is correct
49 Correct 7 ms 12244 KB Output is correct
50 Correct 7 ms 12116 KB Output is correct
51 Correct 7 ms 12180 KB Output is correct
52 Correct 7 ms 12116 KB Output is correct
53 Correct 7 ms 12080 KB Output is correct
54 Correct 8 ms 12116 KB Output is correct
55 Correct 7 ms 12084 KB Output is correct
56 Correct 7 ms 12116 KB Output is correct
57 Correct 8 ms 12212 KB Output is correct
58 Correct 8 ms 12080 KB Output is correct
59 Correct 7 ms 12116 KB Output is correct
60 Correct 7 ms 12116 KB Output is correct
61 Correct 8 ms 12116 KB Output is correct
62 Correct 7 ms 12208 KB Output is correct
63 Correct 7 ms 12160 KB Output is correct
64 Correct 134 ms 22236 KB Output is correct
65 Correct 129 ms 22196 KB Output is correct
66 Correct 144 ms 22144 KB Output is correct
67 Correct 118 ms 22180 KB Output is correct
68 Correct 134 ms 22248 KB Output is correct
69 Correct 134 ms 22172 KB Output is correct
70 Correct 119 ms 22236 KB Output is correct
71 Correct 128 ms 22200 KB Output is correct
72 Correct 125 ms 22352 KB Output is correct
73 Correct 143 ms 34896 KB Output is correct
74 Correct 154 ms 34800 KB Output is correct
75 Correct 120 ms 22100 KB Output is correct
76 Correct 122 ms 22152 KB Output is correct
77 Correct 119 ms 22060 KB Output is correct
78 Correct 138 ms 22088 KB Output is correct
79 Correct 132 ms 22040 KB Output is correct
80 Correct 132 ms 22116 KB Output is correct
81 Correct 98 ms 22580 KB Output is correct
82 Correct 120 ms 22292 KB Output is correct
83 Correct 129 ms 27344 KB Output is correct
84 Correct 143 ms 29456 KB Output is correct
85 Correct 145 ms 29164 KB Output is correct
86 Correct 116 ms 23580 KB Output is correct
87 Correct 123 ms 22916 KB Output is correct
88 Correct 119 ms 23408 KB Output is correct
89 Correct 126 ms 23508 KB Output is correct
90 Correct 141 ms 23228 KB Output is correct
91 Correct 137 ms 23432 KB Output is correct
92 Correct 143 ms 23820 KB Output is correct
93 Correct 136 ms 26144 KB Output is correct
94 Correct 124 ms 27876 KB Output is correct
95 Correct 123 ms 26560 KB Output is correct
96 Correct 105 ms 24840 KB Output is correct
97 Correct 115 ms 23228 KB Output is correct
98 Correct 127 ms 22884 KB Output is correct
99 Correct 133 ms 22552 KB Output is correct
100 Correct 125 ms 25780 KB Output is correct
101 Correct 125 ms 23724 KB Output is correct
102 Correct 117 ms 22908 KB Output is correct
103 Correct 133 ms 22768 KB Output is correct
104 Correct 70 ms 23052 KB Output is correct
105 Correct 141 ms 31572 KB Output is correct
106 Correct 72 ms 22948 KB Output is correct
107 Correct 74 ms 22972 KB Output is correct
108 Correct 148 ms 35576 KB Output is correct
109 Correct 75 ms 23028 KB Output is correct
110 Correct 147 ms 35992 KB Output is correct
111 Correct 122 ms 22076 KB Output is correct