Codeforce 796 c Bank Hacking

本文详细解析了Codeforce796cBankHacking问题,阐述了银行间关系的树状结构及如何通过分析节点关系确定最大力量值。探讨了三种可能的最优解情况,并提供了一段有待优化的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Codeforce 796 c Bank Hacking

题目描述:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

题目分析:

  1. 除了第一次hack之外,以后所有将要hack的bank都要和已经被hack的bank即已经offline的bank有关联;
  2. 整个bank之间的关系可以抽象成一课树;
    在这里插入图片描述
    如果当前hack的是父节点,那么只有儿子节点和孙子节点会有影响,即满足题目中邻居和半邻居的关系,他们相应的力量随之加一,对于孙子节点以下的节点则完全没有影响;
  3. 由此可以分析得到:
    (1)ans=max,当且仅当max的个数为1,所有的max-1的节点都与max相邻;
    (2)ans=max+1,当且仅当存在一个点满足包括他自己和她所有相邻的节点中值为max的个数为所有max的个数;
    (3)ans=max+2,others;

Code:

注:以下代码存在问题,在cf上提交的时候在第八组数据会超时,可以参考AC代码:https://blog.youkuaiyun.com/h1021456873/article/details/70183187
希望求解如何改进我的代码使之不会TLE;

#include <iostream>
#include <climits>
#include <algorithm>
using namespace std;
int a[300010],n,u[300010],v[300010],maxx=INT_MIN,count1=0,count2=0,ans=INT_MAX;
int main() {
	cin>>n;
	for(int i=1; i<=n; i++) {
		cin>>a[i];
	}
	for(int i=1; i<=n-1; i++) {
		cin>>u[i]>>v[i];
	}
	for(int i=1; i<=n; i++) {
		maxx=max(maxx,a[i]);
	}
	for(int i=1; i<=n; i++) {
		if(a[i]==maxx) {
			count1++;
		}
		if(a[i]==maxx-1) {
			count2++;
		}
	}

	if(count1==1) {
		int cnt=0;
		for(int i=1; i<=n-1; i++) {
			if(a[v[i]]==maxx&&a[u[i]]==maxx-1) {
				cnt++;
			} else if(a[u[i]]==maxx&&a[v[i]]==maxx-1) {
				cnt++;
			}
		}
		if(cnt==count2) {
			ans=maxx;
		}
	}
	if(ans==INT_MAX) {
		for(int i=1; i<=n; i++) {
			int cnt=0;
			if(a[i]==maxx) {
				cnt++;
			}
			for(int j=1; j<=n-1; j++) {
				if(v[j]==i&&a[u[j]]==maxx) {
					cnt++;
				} else if(u[j]==i&&a[v[j]]==maxx) {
					cnt++;
				}
			}
			if(cnt==count1) {
				ans=maxx+1;
				break;
			}
		}
	}
	if(ans==INT_MAX) {
		ans=maxx+2;
	}
	cout<<ans<<endl;
	return 0;
}
### Codeforces Problem or Contest 998 Information For the specific details on Codeforces problem or contest numbered 998, direct references were not provided within the available citations. However, based on similar structures observed in other contests such as those described where configurations often include constraints like `n` representing numbers of elements with defined ranges[^1], it can be inferred that contest or problem 998 would follow a comparable format. Typically, each Codeforces contest includes several problems labeled from A to F or beyond depending on the round size. Each problem comes with its own set of rules, input/output formats, and constraint descriptions. For instance, some problems specify conditions involving integer inputs for calculations or logical deductions, while others might involve more complex algorithms or data processing tasks[^3]. To find detailed information regarding contest or problem 998 specifically: - Visit the official Codeforces website. - Navigate through past contests until reaching contest 998. - Review individual problem statements under this contest for precise requirements and examples. Additionally, competitive programming platforms usually provide comprehensive documentation alongside community discussions which serve valuable resources when exploring particular challenges or learning algorithmic solutions[^2]. ```cpp // Example C++ code snippet demonstrating how contestants interact with input/output during competitions #include <iostream> using namespace std; int main() { int n; cin >> n; // Process according to problem statement specifics } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值