Submission #633442

# Submission time Handle Problem Language Result Execution time Memory
633442 2022-08-22T13:21:23 Z iomoon191 Power Plant (JOI20_power) C++17
47 / 100
4 ms 5264 KB
#include<bits/stdc++.h>
// #define int long long
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define pb push_back
#define eb emplace_back
#define For(i,a,b) for(int i=(a); i<=(b); ++i)
#define roF(i,a,b) for(int i=(a); i>=(b); --i)
#define fi first
#define se second
#define mod 998244353
using namespace std;
// using namespace atcoder;
#ifdef DEBUG__
struct db_os{
    ostream& os;
    bool chk;
    template<class T> auto operator<<(T&& x){
        if(!chk) os << ", ";
        chk=0;
        os << x;
        return *this;
    }
};
template<class... T> void db_out(T&&... t){
    (db_os{cerr, 1} << ... << t);
}
#define dbg(...)                                       \
    do{                                                 \
        cerr << __LINE__ << ":" << #__VA_ARGS__ << "=";  \
        db_out(__VA_ARGS__);                              \
        cerr << "\n";                                      \
    }while(0);
#else
#define dbg(...)
#endif
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
typedef long double ld;
const int N=100005;
const ll inf=0x3f3f3f3f;
mt19937 rng(random_device {}()); 
int rand(int a){
    return rng()%a;
}
int n, par[N], le[N], dp[N];
vi adj[N], vc;
char s[N];
void dfs(int u, int p=-1){
    vc.pb(u);
    for(auto &i: adj[u]){
        if(i == p) continue;
        par[i]=u;
        dfs(i, u);
    }
}
void rmain(){
    cin >> n;
    For(i,2,n){
        int u, v; cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    //string s; cin >> s;
    For(i,1,n){
        cin >> s[i];
        s[i]-='0';
    }
    int res=min((int)count(s+1, s+n+1, 1), 2);
    dfs(1);
    reverse(all(vc));
    for(auto &v: vc){
        dp[v]=-s[v];
        for(auto &u: adj[v]){
            dp[v]+=(u != par[v] ? dp[u] : 0);
        }
        dp[v]=max(dp[v], (int)s[v]);
    }
    reverse(all(vc));
    for(auto &v: vc){
        int ne=-s[v];
        for(auto &i: adj[v]){
            ne+=(i == par[v] ? le[v] : dp[i]);
        }
        for(auto &i: adj[v]){
            if(i == par[v]) continue;
            le[i]=max((int)s[v], ne-dp[i]);
        }
    }
    For(i,1,n){
        int ne=-s[i];
        for(auto &j: adj[i]){
            ne+=(j == par[i] ? le[i] : dp[j]);
        }
        res=max(res, ne);
    }
    cout << res;
}

signed main(int argc, char *argv[]){
    iostream::sync_with_stdio(0);
    int T=1;
    while(T--) rmain();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2692 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 3 ms 2688 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2696 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2696 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2696 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2692 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 3 ms 2688 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2696 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2696 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2696 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2744 KB Output is correct
24 Correct 3 ms 2772 KB Output is correct
25 Correct 2 ms 2772 KB Output is correct
26 Correct 3 ms 2696 KB Output is correct
27 Correct 2 ms 2772 KB Output is correct
28 Correct 3 ms 2748 KB Output is correct
29 Correct 3 ms 2900 KB Output is correct
30 Correct 2 ms 2828 KB Output is correct
31 Correct 3 ms 2772 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 2 ms 2772 KB Output is correct
34 Correct 2 ms 2772 KB Output is correct
35 Correct 2 ms 2772 KB Output is correct
36 Correct 3 ms 2700 KB Output is correct
37 Correct 2 ms 2772 KB Output is correct
38 Correct 2 ms 2772 KB Output is correct
39 Correct 3 ms 2832 KB Output is correct
40 Correct 3 ms 2772 KB Output is correct
41 Correct 3 ms 2864 KB Output is correct
42 Correct 3 ms 2772 KB Output is correct
43 Correct 3 ms 2772 KB Output is correct
44 Correct 3 ms 2772 KB Output is correct
45 Correct 2 ms 2772 KB Output is correct
46 Correct 2 ms 2772 KB Output is correct
47 Correct 3 ms 2772 KB Output is correct
48 Correct 3 ms 2832 KB Output is correct
49 Correct 2 ms 2772 KB Output is correct
50 Correct 2 ms 2752 KB Output is correct
51 Correct 2 ms 2772 KB Output is correct
52 Correct 3 ms 2772 KB Output is correct
53 Correct 2 ms 2772 KB Output is correct
54 Correct 2 ms 2772 KB Output is correct
55 Correct 4 ms 2828 KB Output is correct
56 Correct 2 ms 2772 KB Output is correct
57 Correct 3 ms 2900 KB Output is correct
58 Correct 3 ms 2772 KB Output is correct
59 Correct 2 ms 2772 KB Output is correct
60 Correct 2 ms 2828 KB Output is correct
61 Correct 2 ms 2828 KB Output is correct
62 Correct 2 ms 2900 KB Output is correct
63 Correct 2 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2692 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 3 ms 2688 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2696 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2696 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2696 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2744 KB Output is correct
24 Correct 3 ms 2772 KB Output is correct
25 Correct 2 ms 2772 KB Output is correct
26 Correct 3 ms 2696 KB Output is correct
27 Correct 2 ms 2772 KB Output is correct
28 Correct 3 ms 2748 KB Output is correct
29 Correct 3 ms 2900 KB Output is correct
30 Correct 2 ms 2828 KB Output is correct
31 Correct 3 ms 2772 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 2 ms 2772 KB Output is correct
34 Correct 2 ms 2772 KB Output is correct
35 Correct 2 ms 2772 KB Output is correct
36 Correct 3 ms 2700 KB Output is correct
37 Correct 2 ms 2772 KB Output is correct
38 Correct 2 ms 2772 KB Output is correct
39 Correct 3 ms 2832 KB Output is correct
40 Correct 3 ms 2772 KB Output is correct
41 Correct 3 ms 2864 KB Output is correct
42 Correct 3 ms 2772 KB Output is correct
43 Correct 3 ms 2772 KB Output is correct
44 Correct 3 ms 2772 KB Output is correct
45 Correct 2 ms 2772 KB Output is correct
46 Correct 2 ms 2772 KB Output is correct
47 Correct 3 ms 2772 KB Output is correct
48 Correct 3 ms 2832 KB Output is correct
49 Correct 2 ms 2772 KB Output is correct
50 Correct 2 ms 2752 KB Output is correct
51 Correct 2 ms 2772 KB Output is correct
52 Correct 3 ms 2772 KB Output is correct
53 Correct 2 ms 2772 KB Output is correct
54 Correct 2 ms 2772 KB Output is correct
55 Correct 4 ms 2828 KB Output is correct
56 Correct 2 ms 2772 KB Output is correct
57 Correct 3 ms 2900 KB Output is correct
58 Correct 3 ms 2772 KB Output is correct
59 Correct 2 ms 2772 KB Output is correct
60 Correct 2 ms 2828 KB Output is correct
61 Correct 2 ms 2828 KB Output is correct
62 Correct 2 ms 2900 KB Output is correct
63 Correct 2 ms 2772 KB Output is correct
64 Runtime error 4 ms 5264 KB Execution killed with signal 11
65 Halted 0 ms 0 KB -