답안 #309883

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
309883 2020-10-04T21:26:35 Z adrianbudau Power Plant (JOI20_power) C++17
100 / 100
188 ms 31744 KB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Tree {
  public:
    Tree(int size): m_edges(size), m_generators(size, 0) {}

    void add_edge(int from, int to) {
        m_edges[from].emplace_back(to);
        m_edges[to].emplace_back(from);
    }

    void set_generator(int node) {
        m_generators[node]++;
    }

    int best() const {
        return dfs(0).first;
    }

  private:
    pair<int, int> dfs(int node, int father = -1) const {
        int bestdown = m_generators[node];
        int bestfull = m_generators[node];

        int sum = 0;
        for (auto &x : m_edges[node]) {
            if (x == father)
                continue;
            auto [a, b] = dfs(x, node);
            bestdown = max({bestdown, a, b + m_generators[node]});
            sum += b;
        }
        bestfull = max(bestfull, sum - m_generators[node]);
        bestdown = max(bestdown, bestfull);
        return {bestdown, bestfull};
    }

    vector< vector<int> > m_edges;
    vector<int> m_generators;
};

int main() {
    ios::sync_with_stdio(false);

    int N; cin >> N;
    Tree T(N);
    for (int i = 1; i < N; ++i) {
        int x, y; cin >> x >> y;
        T.add_edge(x - 1, y - 1);
    }

    string S; cin >> S;
    for (int i = 0; i < N; ++i)
        if (S[i] == '1')
            T.set_generator(i);

    cout << T.best() << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 416 KB Output is correct
23 Correct 2 ms 512 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 512 KB Output is correct
26 Correct 1 ms 512 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
28 Correct 1 ms 512 KB Output is correct
29 Correct 1 ms 640 KB Output is correct
30 Correct 1 ms 512 KB Output is correct
31 Correct 1 ms 512 KB Output is correct
32 Correct 1 ms 512 KB Output is correct
33 Correct 1 ms 512 KB Output is correct
34 Correct 1 ms 512 KB Output is correct
35 Correct 1 ms 512 KB Output is correct
36 Correct 1 ms 512 KB Output is correct
37 Correct 1 ms 512 KB Output is correct
38 Correct 1 ms 512 KB Output is correct
39 Correct 1 ms 512 KB Output is correct
40 Correct 1 ms 512 KB Output is correct
41 Correct 1 ms 512 KB Output is correct
42 Correct 1 ms 512 KB Output is correct
43 Correct 1 ms 512 KB Output is correct
44 Correct 1 ms 512 KB Output is correct
45 Correct 1 ms 512 KB Output is correct
46 Correct 1 ms 512 KB Output is correct
47 Correct 1 ms 512 KB Output is correct
48 Correct 1 ms 512 KB Output is correct
49 Correct 1 ms 512 KB Output is correct
50 Correct 2 ms 512 KB Output is correct
51 Correct 2 ms 512 KB Output is correct
52 Correct 1 ms 512 KB Output is correct
53 Correct 1 ms 512 KB Output is correct
54 Correct 1 ms 512 KB Output is correct
55 Correct 1 ms 512 KB Output is correct
56 Correct 1 ms 512 KB Output is correct
57 Correct 1 ms 640 KB Output is correct
58 Correct 1 ms 512 KB Output is correct
59 Correct 1 ms 512 KB Output is correct
60 Correct 1 ms 512 KB Output is correct
61 Correct 1 ms 512 KB Output is correct
62 Correct 1 ms 640 KB Output is correct
63 Correct 2 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 416 KB Output is correct
23 Correct 2 ms 512 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 512 KB Output is correct
26 Correct 1 ms 512 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
28 Correct 1 ms 512 KB Output is correct
29 Correct 1 ms 640 KB Output is correct
30 Correct 1 ms 512 KB Output is correct
31 Correct 1 ms 512 KB Output is correct
32 Correct 1 ms 512 KB Output is correct
33 Correct 1 ms 512 KB Output is correct
34 Correct 1 ms 512 KB Output is correct
35 Correct 1 ms 512 KB Output is correct
36 Correct 1 ms 512 KB Output is correct
37 Correct 1 ms 512 KB Output is correct
38 Correct 1 ms 512 KB Output is correct
39 Correct 1 ms 512 KB Output is correct
40 Correct 1 ms 512 KB Output is correct
41 Correct 1 ms 512 KB Output is correct
42 Correct 1 ms 512 KB Output is correct
43 Correct 1 ms 512 KB Output is correct
44 Correct 1 ms 512 KB Output is correct
45 Correct 1 ms 512 KB Output is correct
46 Correct 1 ms 512 KB Output is correct
47 Correct 1 ms 512 KB Output is correct
48 Correct 1 ms 512 KB Output is correct
49 Correct 1 ms 512 KB Output is correct
50 Correct 2 ms 512 KB Output is correct
51 Correct 2 ms 512 KB Output is correct
52 Correct 1 ms 512 KB Output is correct
53 Correct 1 ms 512 KB Output is correct
54 Correct 1 ms 512 KB Output is correct
55 Correct 1 ms 512 KB Output is correct
56 Correct 1 ms 512 KB Output is correct
57 Correct 1 ms 640 KB Output is correct
58 Correct 1 ms 512 KB Output is correct
59 Correct 1 ms 512 KB Output is correct
60 Correct 1 ms 512 KB Output is correct
61 Correct 1 ms 512 KB Output is correct
62 Correct 1 ms 640 KB Output is correct
63 Correct 2 ms 512 KB Output is correct
64 Correct 162 ms 15328 KB Output is correct
65 Correct 161 ms 15456 KB Output is correct
66 Correct 156 ms 15356 KB Output is correct
67 Correct 160 ms 15324 KB Output is correct
68 Correct 158 ms 15312 KB Output is correct
69 Correct 160 ms 15320 KB Output is correct
70 Correct 159 ms 15456 KB Output is correct
71 Correct 154 ms 15328 KB Output is correct
72 Correct 154 ms 15320 KB Output is correct
73 Correct 177 ms 30520 KB Output is correct
74 Correct 180 ms 30268 KB Output is correct
75 Correct 158 ms 15324 KB Output is correct
76 Correct 159 ms 15320 KB Output is correct
77 Correct 170 ms 15192 KB Output is correct
78 Correct 169 ms 15452 KB Output is correct
79 Correct 165 ms 15204 KB Output is correct
80 Correct 162 ms 15196 KB Output is correct
81 Correct 134 ms 15604 KB Output is correct
82 Correct 151 ms 15388 KB Output is correct
83 Correct 176 ms 21436 KB Output is correct
84 Correct 178 ms 23880 KB Output is correct
85 Correct 172 ms 23492 KB Output is correct
86 Correct 163 ms 16824 KB Output is correct
87 Correct 163 ms 16180 KB Output is correct
88 Correct 145 ms 16624 KB Output is correct
89 Correct 149 ms 16584 KB Output is correct
90 Correct 152 ms 16284 KB Output is correct
91 Correct 147 ms 16668 KB Output is correct
92 Correct 154 ms 17060 KB Output is correct
93 Correct 142 ms 20100 KB Output is correct
94 Correct 146 ms 22000 KB Output is correct
95 Correct 141 ms 20468 KB Output is correct
96 Correct 149 ms 18212 KB Output is correct
97 Correct 139 ms 16404 KB Output is correct
98 Correct 146 ms 15880 KB Output is correct
99 Correct 147 ms 15672 KB Output is correct
100 Correct 155 ms 19384 KB Output is correct
101 Correct 148 ms 16948 KB Output is correct
102 Correct 150 ms 16076 KB Output is correct
103 Correct 154 ms 16088 KB Output is correct
104 Correct 94 ms 16236 KB Output is correct
105 Correct 185 ms 26552 KB Output is correct
106 Correct 96 ms 15980 KB Output is correct
107 Correct 94 ms 16108 KB Output is correct
108 Correct 183 ms 31288 KB Output is correct
109 Correct 99 ms 16108 KB Output is correct
110 Correct 188 ms 31744 KB Output is correct
111 Correct 149 ms 15204 KB Output is correct