CodeForces - 796C Bank Hacking

时间:2021-07-26 21:03:52

思路:共有n-1条边连接n个点,即形成一棵树。一开始需要选择一个点hack--将这个点视为根结点,与它相邻的点防御值加1,与它相隔一个在线点的点的防御也加1。当根节点被hack,即这个点被删除,又变成多棵树。此时,Inzane只有一种选择,因为题目规定“Bank
x is neighboring to some
offline bank.”,即选完第一个点,接下来他只能选择剩下的数的根结点,每个节点的防御值都变得有规律可循--假设把根结点看做第一层,那么第二层的防御值都会加一,其它层都会加二。

因此,答案只与根结点的选取有关,那么可以枚举n个点,取最小值。注意每棵树的答案,只与最大值和最大值减一有关。

AC代码

#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 3e5 + 5;
int a[maxn];
vector<int>G[maxn];

int main() {
	int n;
	while(scanf("%d", &n) == 1) {
		for(int i = 0; i <= n; ++i) G[i].clear();
		int maxt = -inf;
		for(int i = 1; i <= n; ++i) {
			scanf("%d", &a[i]);
			maxt = max(maxt, a[i]);
		}
		//统计最大值和第二大的值的数量
		int fir = 0, sec = 0;
		for(int i = 1; i <= n; ++i) {
			if(a[i] == maxt) ++fir;
			if(a[i] == maxt-1) ++sec;
		}
		int u, v;
		for(int i = 0; i < n-1; ++i) {
			scanf("%d%d", &u, &v);
			G[u].push_back(v);
			G[v].push_back(u);
		}
		int ans = inf;
		for(int i = 1; i <= n; ++i) {
			int f = 0, s = 0;
			for(int j = 0; j < G[i].size(); ++j) {
				int x = G[i][j];
				if(a[x] == maxt) ++f;
				if(a[x] == maxt-1) ++s;
			}
			int hack = a[i];
			int l1 = fir - f, l2 = sec - s;
			if(a[i] == maxt) l1--;
			if(a[i] == maxt-1) l2--;
			if(f > 0) hack = max(hack, maxt+1);
			if(s > 0) hack = max(hack, maxt);
			if(l1 > 0) hack = max(hack, maxt+2);
			if(l2 > 0) hack = max(hack, maxt+1);
			ans = min(ans, hack);
		}
		printf("%d\n", ans);
	}
	return 0;
}

如有不当之处欢迎指出!