CF796C Bank Hacking 题解
题目
Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search for Zane, he would need a lot of money, of which he sadly has none. To deal with the problem, he has decided to hack the banks.
There are n banks, numbered from 1 to n. There are also n - 1 wires connecting the banks. All banks are initially online. Each bank also has its initial strength: bank i has initial strength a i.
Let us define some keywords before we proceed. Bank i and bank j are neighboring if and only if there exists a wire directly connecting them. Bank i and bank j are semi-neighboring if and only if there exists an online bank k such that bank i and bank k are neighboring and bank k and bank j are neighboring.
When a bank is hacked, it becomes offline (and no longer online), and other banks that are neighboring or semi-neighboring to it have their strengths increased by 1.
To start his plan, Inzane will choose a bank to hack first. Indeed, the strength of such bank must not exceed the strength of his computer. After this, he will repeatedly choose some bank to hack next until all the banks are hacked, but he can continue to hack bank x if and only if all these conditions are met:
- Bank x is online. That is, bank x is not hacked yet.
- Bank x is neighboring to some offline bank.
- The strength of bank x is less than or equal to the strength of Inzane's computer.
Determine the minimum strength of the computer Inzane needs to hack all the banks.
给定一棵带点权树,选出一个最佳的根节点,使得根节点的点权不变,它的儿子点权加1,其余点点权加2,并使最大点权最小,输出这个最小的最大点权
输入格式
The first line contains one integer \(n (1 ≤ n ≤ 3·10^5)\) — the total number of banks.
The second line contains \(n\) integers \(a_1, a_2, ..., a_n ( - 10^9 ≤ a i ≤ 10^9)\) — the strengths of the banks.
Each of the next n - 1 lines contains two integers \(u_i\) and \(v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i)\) — meaning that there is a wire directly connecting banks \(u_i\) and \(v_i\).
输出格式
Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.
It is guaranteed that the wires connect the banks in such a way that Inzane can somehow hack all the banks using a computer with appropriate strength.
题解
给定一棵带点权树,选出一个最佳的根节点,使得根节点的点权不变,它的儿子点权加1,其余点点权加2,并使最大点权最小,输出这个最小的最大点权
这是洛谷上的翻译, 很简洁明了.
这道题直接遍历一遍树, 按照题意统计一下就好了.
输入的时候先求出点权的最大值和每个权值的个数, 然后遍历每一个节点, 每个节点遍历每一个子节点, 根据题目性质计算即可.
代码
#include <bits/stdc++.h>
using namespace std;
struct Edge { int to, next; } edges[600010];
int n, tot, ans, maxv = -0x3f3f3f3f, flag, x, y, a[300010], head[300010];
map<int, int> m;
void add(int x,int y) { edges[++tot] = (Edge){y, head[x]},head[x] = tot; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]), m[a[i]]++, maxv = max(maxv, a[i]);
ans = maxv + 2;
for (int i = 1; i < n; i++) scanf("%d%d", &x, &y), add(x,y), add(y,x);
for (int i = 1; i <= n; i++) {
flag = 0;
for (int j = head[i]; j; j = edges[j].next) {
m[a[edges[j].to]]--;
if (a[edges[j].to] == maxv) flag = 1;
}
if (!m[maxv]) ans = maxv + 1;
if (maxv == a[i] && m[maxv] == 1) {
if (m[maxv - 1] || flag) // 其它节点有ans-1的权值,-1+2=+1 或者 子节点中也有最大值,子节点+1后是答案
ans = maxv + 1;
else {
ans = maxv; // 根节点是最大值, 并且子树中没有更大的, 直接break
break;
}
}
for (int j = head[i]; j; j = edges[j].next) m[a[edges[j].to]]++;
}
printf("%d\n", ans);
return 0;
}
CF796C Bank Hacking 题解的更多相关文章
- CF796C Bank Hacking 思维
Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search f ...
- CF796C Bank Hacking 细节
思路十分简单,答案只有 3 种可能,但是有一些细节需要额外注意一下. code: #include <bits/stdc++.h> #define N 300002 #define set ...
- Bank Hacking题解
题目: 题意: 有一颗树,你可以断开点(第一个随便断,以后只能是和已经断开的点相临的点),每个点有权值,断开之后,经一条边和两条边可以到达的节点权值加一,问到最后出现过的最大的权值. 分析: 为啥断开 ...
- C. Bank Hacking 解析(思維)
Codeforce 796 C. Bank Hacking 解析(思維) 今天我們來看看CF796C 題目連結 題目 略,請直接看原題. 前言 @copyright petjelinux 版權所有 觀 ...
- Code Forces 796C Bank Hacking(贪心)
Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...
- codeforce 796C - Bank Hacking(无根树+思维)
题目 Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To searc ...
- CodeForces - 796C Bank Hacking
思路:共有n-1条边连接n个点,即形成一棵树.一开始需要选择一个点hack--将这个点视为根结点,与它相邻的点防御值加1,与它相隔一个在线点的点的防御也加1.当根节点被hack,即这个点被删除,又变成 ...
- Codeforces Round #408 (Div. 2)C. Bank Hacking(STL)
题目链接:http://codeforces.com/problemset/problem/796/C 题目大意:有n家银行,第一次可以攻击任意一家银行(能量低于自身),跟被攻击银行相邻或者间接相邻( ...
- Codeforces Round #408 (Div. 2) C. Bank Hacking
http://codeforces.com/contest/796/problem/C Although Inzane successfully found his beloved bone, Zan ...
随机推荐
- PAT 旧键盘打字
旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及坏掉的那些键,打出的结果文字会是怎样? 输入格式: 输入在 2 行中分别给出坏掉的那些键.以及应该输入 ...
- 循序渐进VUE+Element 前端应用开发(8)--- 树列表组件的使用
在我前面随笔<循序渐进VUE+Element 前端应用开发(6)--- 常规Element 界面组件的使用>里面曾经介绍过一些常规的界面组件的处理,主要介绍到单文本输入框.多文本框.下拉列 ...
- 第12章 Java内存模型与线程
参考<深入理解Java虚拟机> 一.Java内存模型 1.Java内存模型 2.内存间交互操作 流程图: 3.volatile关键字 两个特性: 3.1.保证变脸对所有线程的可见性: 由 ...
- 白嫖永久免费云服务器教程,永久免费虚拟主机、永久免费云数据库、搭建FTP服务器、服务器安装Linux / windows操作系统、服务器部署网站、宝塔一键部署多网站、独立ip、永久国内高速云服务器
一.准备工作 1. 注册账号 声明:切记不可用服务器做违法的事情 申请地址:https://www.sanfengyun.com/ 图文教程地址:https://www.cnblogs.com/zwn ...
- @codeforces - 607E@ Cross Sum
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定 n 条直线,构造可重点集合 I 为 n 条直线两两的交点集 ...
- RESTORE FILELISTONLY FROM DISK ='D:\DataSQL\BakFile\pubs.bak' _ 数据库安装工具_连载_4
在查询分析器中执行: RESTORE FILELISTONLY FROM DISK = 'D:\DataSQL\BakFile\pubs.bak'根据查询类型判断Type(D.L),再取得对应的Lo ...
- 并发系列(一)——线程池源码(ThreadPoolExecutor类)简析
前言 本文主要是结合源码去线程池执行任务的过程,基于JDK 11,整个过程基本与JDK 8相同. 个人水平有限,文中若有表达有误的,欢迎大伙留言指出,谢谢了! 一.线程池简介 1.1 使用线程池的优点 ...
- Beta 冲刺
这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 Beta 冲刺 这个作业的目标 Beta 冲刺 汇总博客 作业正文 如下 其他参考文献 ... 说明:此博客 ...
- js中each函数的用法
官方说明: jQuery.each(object, [callback]) 概述 通用例遍方法,可用于例遍对象和数组. 不同于例遍 jQuery 对象的 $().each() 方法,此方法可用于例遍任 ...
- 爬取B站弹幕并且制作词云
目录 爬取弹幕 1. 从手机端口进入网页爬取找到接口 2.代码 制作词云 1.文件读取 2.代码 爬取弹幕 1. 从手机端口进入网页爬取找到接口 2.代码 import requests from l ...