이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |