正文
增加 11 行删除 11 行
49 行未改动50515253545556575822 行未改动81828384858687888952 行未改动14214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717814 行未改动49 行未改动int n, tot;int head[N], c[N], Size[N], f[N];li void add(re int x, re int y) { //链式前向星不多说e[++tot].to = y; e[tot].next = head[x]; head[x] = tot;e[++tot].to = x; e[tot].next = head[y]; head[y] = tot;}22 行未改动for (re int i = 1; i <= n; i++) { c[i] = read(); }for (re int i = 1; i < n; i++) {re int x = read(), y = read(); add(x, y);} update(1, 1); dfs(1, 1);write(std::max(f[1] - 1, c[1]) + (n - 1) * 2); //这里的-1是减去从1的子节点走回1的这步return 0;}```52 行未改动int n, tot;int head[N], c[N], Size[N], f[N];li void add(re int x, re int y) { //链式前向星不多说e[++tot].to = y; e[tot].next = head[x]; head[x] = tot;e[++tot].to = x; e[tot].next = head[y]; head[y] = tot;}li void update(re int x, re int fa) { //求Size[]for (re int i = head[x]; i; i = e[i].next) {re int y = e[i].to;if (y == fa) { continue; }update(y, x);Size[x] += Size[y] + 1;}}li bool cmp(std::pair <int, int> x, std::pair <int, int> y) { return x.first > y.first; } //按f(y)从大到小排序li void dfs(re int x, re int fa) {std::priority_queue < std::pair <int, int> > q; //定义优先队列re int tmp, s = 0;for (re int i = head[x]; i; i = e[i].next) {re int y = e[i].to;if (y == fa) { continue; }dfs(y, x); q.push(std::make_pair(f[y], y)); //插入} tmp = q.size();for (re int i = 1; i <= tmp; i++) {s += Size[q.top().second]; // 累加前子节点之和f[x] = std::max(f[x], q.top().first - ((Size[x] - s - i) * 2 + 1)); //dp转换q.pop(); //弹出队首} f[x] = std::max(f[x], c[x] - 2 * Size[x]); //实际上不特判x = 1也可以(因为在最后输出的时候还有一次max}signed main() {n = read();14 行未改动49 行未改动50515253545556575822 行未改动81828384858687888952 行未改动14214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717814 行未改动49 行未改动int n, tot;int head[N], c[N], Size[N], f[N];li void add(re int x, re int y) { //链式前向星不多说e[++tot].to = y; e[tot].next = head[x]; head[x] = tot;e[++tot].to = x; e[tot].next = head[y]; head[y] = tot;}22 行未改动for (re int i = 1; i <= n; i++) { c[i] = read(); }for (re int i = 1; i < n; i++) {re int x = read(), y = read(); add(x, y);} update(1, 1); dfs(1, 1);write(std::max(f[1] - 1, c[1]) + (n - 1) * 2); //这里的-1是减去从1的子节点走回1的这步return 0;}```52 行未改动int n, tot;int head[N], c[N], Size[N], f[N];li void add(re int x, re int y) { //链式前向星不多说e[++tot].to = y; e[tot].next = head[x]; head[x] = tot;e[++tot].to = x; e[tot].next = head[y]; head[y] = tot;}li void update(re int x, re int fa) { //求Size[]for (re int i = head[x]; i; i = e[i].next) {re int y = e[i].to;if (y == fa) { continue; }update(y, x);Size[x] += Size[y] + 1;}}li bool cmp(std::pair <int, int> x, std::pair <int, int> y) { return x.first > y.first; } //按f(y)从大到小排序li void dfs(re int x, re int fa) {std::priority_queue < std::pair <int, int> > q; //定义优先队列re int tmp, s = 0;for (re int i = head[x]; i; i = e[i].next) {re int y = e[i].to;if (y == fa) { continue; }dfs(y, x); q.push(std::make_pair(f[y], y)); //插入} tmp = q.size();for (re int i = 1; i <= tmp; i++) {s += Size[q.top().second]; // 累加前子节点之和f[x] = std::max(f[x], q.top().first - ((Size[x] - s - i) * 2 + 1)); //dp转换q.pop(); //弹出队首} f[x] = std::max(f[x], c[x] - 2 * Size[x]); //实际上不特判x = 1也可以(因为在最后输出的时候还有一次max}signed main() {n = read();14 行未改动