codeforce 796C
来源:互联网 发布:刺客列传网络剧百度云 编辑:程序博客网 时间:2024/05/19 12:40
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 ai.
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 neighboringand 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.
The first line contains one integer n (1 ≤ n ≤ 3·105) — the total number of banks.
The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the strengths of the banks.
Each of the next n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — meaning that there is a wire directly connecting banks ui and vi.
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.
Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.
这其实是一棵无根树,答案只有Max, Max+1, Max+2;(Max表示这些权值中最大的)
然后接下来是来一遍预处理。求出各个点中与其相邻Max,Max-1的个数,包含当前节点。接下来设所有点中Max的个数为sum1, Max-1的个数为sum2;
(1)当答案为Max时,这时得有sum1==1,且当前点的子节点包含了全部的值为Max-1的节点。
(2)当答案为Max+1时,这时候要有一个节点的子节点包含了所有的值Max的节点。
(3)剩下的就是答案为Max+2;
AC代码:
#include<stdio.h>#include<vector>using namespace std;struct node{int M1, M2;};int n, a[300010], Max=-2000000000;;vector<int> g[300010];node s[300010];int sum1, sum2;int main(){int i, j, k, u, v;scanf("%d", &n);for(i=1; i<=n; i++){scanf("%d", &a[i]);if(a[i]>Max) Max=a[i];}for(i=1; i<=n-1; i++){scanf("%d%d", &u, &v);g[u].push_back(v);g[v].push_back(u);}for(i=1; i<=n; i++){if(a[i]==Max)sum1++;if(a[i]==Max-1)sum2++;}for(i=1; i<=n; i++){int Size=g[i].size();int c1=0, c2=0;if(a[i]==Max)c1++;if(a[i]==Max-1)c2++;for(j=0; j<Size; j++){if(a[g[i][j]]==Max) c1++;if(a[g[i][j]]==Max-1) c2++;}s[i].M1=c1;s[i].M2=c2;}int root;for(i=1; i<=n; i++){if(a[i]==Max){root=i;break;}} if(sum1==1){ if(s[root].M2==sum2){ printf("%d", Max); return 0;}}for(i=1; i<=n; i++){if(s[i].M1==sum1){printf("%d", Max+1);return 0;}}printf("%d", Max+2);return 0;}
- codeforce 796C
- codeforce 796C Bank Hacking 智力题orz
- Codeforce # C. Average Score
- CodeForce 279C
- codeforce 237 C D
- Codeforce C. Barcode
- Codeforce 57C Array
- Codeforce 240C Practice
- Codeforce 332C
- codeforce 370C
- codeforce 400 C
- codeforce 6C
- codeforce 12C map
- codeforce 14C
- codeforce 18C
- codeforce 447C
- codeforce 20140808 C. Boredom
- codeforce 492 C D
- AndroidStudio 使用本地Maven仓库
- 第一篇
- 不用工具解决VM下安装kali-linux到U盘出现not init found.Try passing init=option to kernel,然后后卡在initramfs:
- 大话数据结构二:线性表的链式存储结构(单链表)
- 自然语言处理的若干问题
- codeforce 796C
- python kafka producer consumer redis数据从kafka发送写人redis
- 搭建: canal部署与实例运行
- INSERT INTO .. ON DUPLICATE KEY用法
- 前端工程
- php通过Mysqli和PDO连接mysql数据详解
- testng的xml文件中包含中文参数
- 简单排列习题 紫书P35
- python input()