답안 #294629

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
294629 2020-09-09T07:39:13 Z Alexa2001 Power Plant (JOI20_power) C++17
100 / 100
1190 ms 29940 KB
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 2e5 + 5;

string is_plant;
vector<int> v[Nmax];

bool used[Nmax];
int dp[Nmax];
int w[Nmax];
int ans = 1;
int n, All;



void dfs0(int node, int dad = 0)
{
    w[node] = 1;
    for(auto it : v[node])
        if(!used[it] && it != dad)
        {
            dfs0(it, node);
            w[node] += w[it];
        }
}

int centroid(int node, int dad = 0)
{
    int worst = All - w[node];

    for(auto it : v[node])
        if(!used[it] && it != dad)
        {
            int res = centroid(it, node);
            if(res != -1) return res;
            worst = max(worst, w[it]);
        }

    if(worst <= All/2) return node;
    return -1;
}

void calc(int node, int dad = 0)
{
    int sum = 0;

    for(auto it : v[node])
        if(!used[it] && it != dad)
        {
            calc(it, node);
            sum += dp[it];
        }

    if(is_plant[node]) dp[node] = max(1, sum - 1);
        else dp[node] = sum;

    assert(dp[node] >= 0);
}

void case1(int node)
{
    int sum = 0;

    for(auto it : v[node])
        if(!used[it])
            sum += dp[it];

    ans = max(ans, sum - is_plant[node]);
}

void case2(int node)
{
    if(!is_plant[node]) return;

    for(auto it : v[node])
        if(!used[it])
            ans = max(ans, dp[it] + 1);
}

void solve(int node)
{
    dfs0(node);
    All = w[node];
    node = centroid(node);
    assert(node != -1);

    calc(node);

    case1(node);
    case2(node);


    used[node] = 1;
    for(auto it : v[node])
        if(!used[it])
            solve(it);
}

int main()
{
   // freopen("input", "r", stdin);
    cin.tie(0); cin.sync_with_stdio(false);

    cin >> n;

    int i;
    for(i=1; i<n; ++i)
    {
        int x, y;
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }

    cin >> is_plant; is_plant = "#" + is_plant;
    for(auto &it : is_plant) it -= '0';

    solve(1);

    cout << ans << '\n';

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 4 ms 4992 KB Output is correct
6 Correct 4 ms 4992 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 4 ms 4992 KB Output is correct
9 Correct 3 ms 4992 KB Output is correct
10 Correct 3 ms 4992 KB Output is correct
11 Correct 3 ms 4992 KB Output is correct
12 Correct 3 ms 4992 KB Output is correct
13 Correct 4 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 3 ms 4992 KB Output is correct
16 Correct 4 ms 4992 KB Output is correct
17 Correct 3 ms 4992 KB Output is correct
18 Correct 4 ms 4992 KB Output is correct
19 Correct 4 ms 4992 KB Output is correct
20 Correct 4 ms 4992 KB Output is correct
21 Correct 4 ms 4992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 4 ms 4992 KB Output is correct
6 Correct 4 ms 4992 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 4 ms 4992 KB Output is correct
9 Correct 3 ms 4992 KB Output is correct
10 Correct 3 ms 4992 KB Output is correct
11 Correct 3 ms 4992 KB Output is correct
12 Correct 3 ms 4992 KB Output is correct
13 Correct 4 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 3 ms 4992 KB Output is correct
16 Correct 4 ms 4992 KB Output is correct
17 Correct 3 ms 4992 KB Output is correct
18 Correct 4 ms 4992 KB Output is correct
19 Correct 4 ms 4992 KB Output is correct
20 Correct 4 ms 4992 KB Output is correct
21 Correct 4 ms 4992 KB Output is correct
22 Correct 6 ms 5208 KB Output is correct
23 Correct 5 ms 5120 KB Output is correct
24 Correct 5 ms 5120 KB Output is correct
25 Correct 5 ms 5120 KB Output is correct
26 Correct 5 ms 5120 KB Output is correct
27 Correct 5 ms 5248 KB Output is correct
28 Correct 5 ms 5120 KB Output is correct
29 Correct 6 ms 5248 KB Output is correct
30 Correct 6 ms 5248 KB Output is correct
31 Correct 5 ms 5120 KB Output is correct
32 Correct 5 ms 5120 KB Output is correct
33 Correct 5 ms 5120 KB Output is correct
34 Correct 5 ms 5120 KB Output is correct
35 Correct 5 ms 5120 KB Output is correct
36 Correct 5 ms 5120 KB Output is correct
37 Correct 5 ms 5120 KB Output is correct
38 Correct 5 ms 5120 KB Output is correct
39 Correct 6 ms 5248 KB Output is correct
40 Correct 6 ms 5248 KB Output is correct
41 Correct 6 ms 5248 KB Output is correct
42 Correct 6 ms 5120 KB Output is correct
43 Correct 5 ms 5120 KB Output is correct
44 Correct 5 ms 5248 KB Output is correct
45 Correct 5 ms 5120 KB Output is correct
46 Correct 5 ms 5120 KB Output is correct
47 Correct 5 ms 5120 KB Output is correct
48 Correct 5 ms 5248 KB Output is correct
49 Correct 5 ms 5248 KB Output is correct
50 Correct 5 ms 5120 KB Output is correct
51 Correct 5 ms 5120 KB Output is correct
52 Correct 5 ms 5120 KB Output is correct
53 Correct 6 ms 5120 KB Output is correct
54 Correct 5 ms 5120 KB Output is correct
55 Correct 5 ms 5120 KB Output is correct
56 Correct 4 ms 5120 KB Output is correct
57 Correct 5 ms 5248 KB Output is correct
58 Correct 4 ms 5120 KB Output is correct
59 Correct 4 ms 5120 KB Output is correct
60 Correct 6 ms 5248 KB Output is correct
61 Correct 4 ms 5120 KB Output is correct
62 Correct 5 ms 5248 KB Output is correct
63 Correct 5 ms 5120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 4 ms 4992 KB Output is correct
6 Correct 4 ms 4992 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 4 ms 4992 KB Output is correct
9 Correct 3 ms 4992 KB Output is correct
10 Correct 3 ms 4992 KB Output is correct
11 Correct 3 ms 4992 KB Output is correct
12 Correct 3 ms 4992 KB Output is correct
13 Correct 4 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 3 ms 4992 KB Output is correct
16 Correct 4 ms 4992 KB Output is correct
17 Correct 3 ms 4992 KB Output is correct
18 Correct 4 ms 4992 KB Output is correct
19 Correct 4 ms 4992 KB Output is correct
20 Correct 4 ms 4992 KB Output is correct
21 Correct 4 ms 4992 KB Output is correct
22 Correct 6 ms 5208 KB Output is correct
23 Correct 5 ms 5120 KB Output is correct
24 Correct 5 ms 5120 KB Output is correct
25 Correct 5 ms 5120 KB Output is correct
26 Correct 5 ms 5120 KB Output is correct
27 Correct 5 ms 5248 KB Output is correct
28 Correct 5 ms 5120 KB Output is correct
29 Correct 6 ms 5248 KB Output is correct
30 Correct 6 ms 5248 KB Output is correct
31 Correct 5 ms 5120 KB Output is correct
32 Correct 5 ms 5120 KB Output is correct
33 Correct 5 ms 5120 KB Output is correct
34 Correct 5 ms 5120 KB Output is correct
35 Correct 5 ms 5120 KB Output is correct
36 Correct 5 ms 5120 KB Output is correct
37 Correct 5 ms 5120 KB Output is correct
38 Correct 5 ms 5120 KB Output is correct
39 Correct 6 ms 5248 KB Output is correct
40 Correct 6 ms 5248 KB Output is correct
41 Correct 6 ms 5248 KB Output is correct
42 Correct 6 ms 5120 KB Output is correct
43 Correct 5 ms 5120 KB Output is correct
44 Correct 5 ms 5248 KB Output is correct
45 Correct 5 ms 5120 KB Output is correct
46 Correct 5 ms 5120 KB Output is correct
47 Correct 5 ms 5120 KB Output is correct
48 Correct 5 ms 5248 KB Output is correct
49 Correct 5 ms 5248 KB Output is correct
50 Correct 5 ms 5120 KB Output is correct
51 Correct 5 ms 5120 KB Output is correct
52 Correct 5 ms 5120 KB Output is correct
53 Correct 6 ms 5120 KB Output is correct
54 Correct 5 ms 5120 KB Output is correct
55 Correct 5 ms 5120 KB Output is correct
56 Correct 4 ms 5120 KB Output is correct
57 Correct 5 ms 5248 KB Output is correct
58 Correct 4 ms 5120 KB Output is correct
59 Correct 4 ms 5120 KB Output is correct
60 Correct 6 ms 5248 KB Output is correct
61 Correct 4 ms 5120 KB Output is correct
62 Correct 5 ms 5248 KB Output is correct
63 Correct 5 ms 5120 KB Output is correct
64 Correct 736 ms 16232 KB Output is correct
65 Correct 689 ms 16240 KB Output is correct
66 Correct 698 ms 16224 KB Output is correct
67 Correct 707 ms 16424 KB Output is correct
68 Correct 666 ms 16224 KB Output is correct
69 Correct 687 ms 16292 KB Output is correct
70 Correct 682 ms 16264 KB Output is correct
71 Correct 708 ms 16384 KB Output is correct
72 Correct 700 ms 16240 KB Output is correct
73 Correct 1129 ms 28792 KB Output is correct
74 Correct 1141 ms 28792 KB Output is correct
75 Correct 732 ms 16116 KB Output is correct
76 Correct 729 ms 16124 KB Output is correct
77 Correct 865 ms 16108 KB Output is correct
78 Correct 882 ms 16112 KB Output is correct
79 Correct 831 ms 16120 KB Output is correct
80 Correct 860 ms 16116 KB Output is correct
81 Correct 243 ms 16520 KB Output is correct
82 Correct 275 ms 16436 KB Output is correct
83 Correct 1012 ms 21376 KB Output is correct
84 Correct 1010 ms 23552 KB Output is correct
85 Correct 1026 ms 23168 KB Output is correct
86 Correct 827 ms 17524 KB Output is correct
87 Correct 849 ms 16992 KB Output is correct
88 Correct 576 ms 17456 KB Output is correct
89 Correct 580 ms 17412 KB Output is correct
90 Correct 582 ms 17244 KB Output is correct
91 Correct 621 ms 17492 KB Output is correct
92 Correct 569 ms 17748 KB Output is correct
93 Correct 641 ms 21768 KB Output is correct
94 Correct 606 ms 23152 KB Output is correct
95 Correct 604 ms 20652 KB Output is correct
96 Correct 637 ms 18784 KB Output is correct
97 Correct 536 ms 17360 KB Output is correct
98 Correct 537 ms 16804 KB Output is correct
99 Correct 533 ms 16496 KB Output is correct
100 Correct 585 ms 19804 KB Output is correct
101 Correct 610 ms 17776 KB Output is correct
102 Correct 593 ms 17148 KB Output is correct
103 Correct 614 ms 16788 KB Output is correct
104 Correct 143 ms 17128 KB Output is correct
105 Correct 1099 ms 25584 KB Output is correct
106 Correct 152 ms 17004 KB Output is correct
107 Correct 147 ms 17004 KB Output is correct
108 Correct 1137 ms 29556 KB Output is correct
109 Correct 151 ms 17128 KB Output is correct
110 Correct 1190 ms 29940 KB Output is correct
111 Correct 554 ms 16132 KB Output is correct