Codeforces Round #408 (Div. 2) C. Bank Hacking
来源:互联网 发布:windows播放器下载 编辑:程序博客网 时间:2024/06/06 12:23
题意:有n个银行,n-1条边,也就是一棵树,每个银行都有一个权值,只有当能力值大于等于这个数值,你才能抢劫这个银行。每当抢劫完这个银行,与它相邻的和半相邻的银行权值都+1,并且下一次抢劫只能抢与它相邻的银行。(半相邻的定义是相邻的点再相邻)求最少需要的能力值能把全部银行抢劫完毕。
思路:我们很容易能发现,任意选则一个点的话,那么该点所需的能力值为a[i],与它相邻所需的能力值为a[i]+1,其余都是a[i]+2,所以答案只有三种,必定是max,max+1,max+2。
max:只存在一个点的权值是max,相邻的点包含了全部max-1的点。
max+1:存在一个点,它与它相邻的点包含了全部权值为max的点。
其余情况均为max+2。下面给代码:
#include<iostream> #include<cmath> #include<queue> #include<cstdio> #include<queue> #include<algorithm> #include<cstring> #include<string> #include<utility>#include<map>#include<vector>#define maxn 300005#define inf 0x3f3f3f3f using namespace std;typedef long long LL;const double eps = 1e-8;int a[maxn], head[maxn], len, maxnum, sum, sum1, index, cal, vis[maxn];struct node{int v, next;}p[maxn << 1];void addedge(int u, int v){p[len].v = v;p[len].next = head[u];head[u] = len++;}int main(){int n;scanf("%d", &n);memset(head, -1, sizeof(head));maxnum = -inf;for (int i = 1; i <= n; i++){scanf("%d", &a[i]);maxnum = max(maxnum, a[i]);}for (int i = 1; i <= n; i++){if (a[i] == maxnum){sum++;index = i;}else if (a[i] == maxnum - 1)sum1++;}for (int i = 0; i < n - 1; i++){int u, v;scanf("%d%d", &u, &v);addedge(u, v);addedge(v, u);}int judge = 2;if (sum == 1){for (int i = head[index]; ~i; i = p[i].next){if (a[p[i].v] == maxnum - 1)cal++;}if (cal == sum1)judge = 0;elsejudge = 1;}else{for (int i = 1; i <= n; i++){if (a[i] == maxnum){vis[i]++;if (vis[i] == sum){judge = 1;break;}for (int j = head[i]; ~j; j = p[j].next){vis[p[j].v]++;if (vis[p[j].v] == sum){judge = 1;break;}}if (judge == 1)break;}}}printf("%d\n", maxnum + judge);}
0 0
- codeforces round#408(div.2) C. Bank Hacking
- Codeforces Round #408 (Div. 2) C. Bank Hacking(二分)
- Codeforces Round #408 (Div. 2) C. Bank Hacking
- Codeforces Round #408 (Div. 2) C. Bank Hacking(分类讨论)
- Codeforces Round #408 (Div. 2) C. Bank Hacking
- Codeforces Round #408 (Div. 2) C.Bank Hacking【思维+分类讨论】
- Codeforces Round #408 (Div. 2) -- C. Bank Hacking(分类讨论)
- Codeforces Round #408 (Div. 2) C. Bank Hacking 无根树、贪心、枚举
- Codeforces Round #408 (Div. 2)-C. Bank Hacking-(三种方法)分类讨论,二分,集合
- Codeforces Round #408 C. Bank Hacking
- codeforces round #408 C. Bank Hacking
- Codeforces Round #408 /Mcvi Round #8 C Bank Hacking
- Codeforces Round #279 (Div. 2)C. Hacking Cypher
- Codeforces Round #279 (Div. 2) C. Hacking Cypher
- Codeforces Round #279 (Div. 2) C. Hacking Cypher
- Codeforces Round #279 (Div. 2)C. Hacking Cypher
- CodeForces 796C Bank Hacking
- Codeforces 796C Bank Hacking 贪心+规律
- 微商加粉方法,微商加粉,究竟难在哪?
- AliSQL开源Sequence Engine
- 事件对象
- c++实验四-星号输出图
- assert()用法
- Codeforces Round #408 (Div. 2) C. Bank Hacking
- MATLAB 视频与图像转换
- 资源整理
- nyoj 115 城市平乱(spfa)
- 拦截导弹 dp java
- Linux信号1
- socket网络编程
- 在编译android4.4的时候libwebviewchromium.so时停止很长时间发生错误
- LinuxC编程之IO-通过lseek对文件进行读写