The 7th Zhejiang Provincial Collegiate Programming Contest - G(Wu Xing)

本文解析了一道涉及最少环数连接节点的图论问题,通过巧妙转换思路,找到了简洁的数学解法。
部署运行你感兴趣的模型镜像

Introduction

The Wu Xing, or the Five Movements, Five Phases or Five Steps/Stages, are chiefly an ancient mnemonic device, in many traditional Chinese fields.

The doctrine of five phases describes two cycles, a generating or creation cycle, also known as "mother-son", and an overcoming or destruction cycle, also known as "grandfather-nephew", of interactions between the phases.

Generating:

  • Wood feeds Fire;
  • Fire creates Earth (ash);
  • Earth bears Metal;
  • Metal carries Water (as in a bucket or tap, or water condenses on metal);
  • Water nourishes Wood.

Overcoming:

  • Wood parts Earth (such as roots) (or Trees can prevent soil erosion );
  • Earth absorbs (or muddies) Water (or Earth dam control the water);
  • Water quenches Fire;
  • Fire melts Metal;
  • Metal chops Wood.

Wu Xing

With the two types of interactions in the above graph, any two nodes are connected by an edge.

Problem

In a graph with N nodes, to ensure that any two nodes are connected by at least one edge, how many types of interactions are required at least? Here a type of interaction should have the following properties:

  • It can be represented as a set of directed edges in the graph.
  • For each type of interaction, there should be one and only one edge starting at each node.
  • For each type of interaction, there should be one and only one edge ending at each node.
  • The interactions are made up of cycles, i.e. starting from an arbitrary node and following the edges with the same type of interaction, you can always reach the starting node after several steps.

Input

For each test case, there's a line with an integer N (3 <= N < 1,000,000), the number of nodes in the graph.

N = 0 indicates the end of input.

Output

For each test case, output a line with the number of interactions that are required at least.

Sample Input

5
0 

Sample Output

2 
解题报告:
此题是一道变态题,说难他很难,他容易,他太容易了。如果你看到这道题就把他判为一道图论题,就开始想图论的算法,什么出度入度,什么邻接矩阵,那我可以告诉你,你估计要悲剧了。题目大意是说,用最少的环试给的几个点,任意两个点之间都有联系。这道题当中应该相当注意一点就是N (3 <= N < 1,000,000)。 1,000,000可是一个不小的数,一般的图论题中都不会见到的。。所以当看到这个的时候,你应该适当的想一下,这应该是一道推论题(说白了就是找规律,找到一个公式可以满足所有的点)。 叫我一个一个图画,我是没有那个本事,所以纯粹的找规律看来是不行的。 剩下的方法,只能好好分析题目了。
我们可以看到,他说要是任意两个点都有联系,看到这里,马上可以想到,要满足这个条件, 必须需要n*(n-1)/2条边,于是,换一种思考,该题目就可以理解为,在n*(n-1)/2条边中可以构成几个环。如果想到这里,你又开始去画点画边,那估计最后结果还是要悲剧。至少我是画不出,就算画出了也不知道到底是否是最小个数的环。 所以我们接着继续分析。。。抛开怎么连线不管, 往整体方向想,要构成一个环, n个点必须要n条边,可以理解,最终所有环所构成的边,都不会超过n*(n-1)/2条边。 所以每条边都在一个或几个环内,(其中不同的环可以共用一条边)。 所以至少有(n-1)/2个环, 如果可以整除,那么商就是答案。如果不能整除呢? 那么余下的边必定可以再组成一个环(必定的嘛)。所以如果(n-1)%2 != 0, 那么答案就等于(n-1)/2 +1;
结果就是和n/2的结果一样,如果有些同学,开玩笑地试了这个公式,恭喜你,你ac了。(踩到狗屎了,呵呵!)
代码就是那么几行,还没有题目的行数多:
#include <iostream>
using namespace std;
int main()
{
 int i,result;
 while(cin>>i && i != 0)
 {
  if((i-1) % 2 != 0)
   result = (i-1)/2+1;
  else
   result = (i-1)/2;
  cout<<result<<endl;
 }
 return 0;
}
 
感想:很遗憾,我没有在比赛中ac这道题,因为之前因为一些数字上的小问题,导致wa,以至于我放弃了这个想法。(无比懊悔中)。
希望,看了这篇文章的同学,做题时冷静分析, 坚持自己的想法,也许ac已经离你很近了,不要让他这么轻易从你手中溜走。。。。。

您可能感兴趣的与本文相关的镜像

AutoGPT

AutoGPT

AI应用

AutoGPT于2023年3月30日由游戏公司Significant Gravitas Ltd.的创始人Toran Bruce Richards发布,AutoGPT是一个AI agent(智能体),也是开源的应用程序,结合了GPT-4和GPT-3.5技术,给定自然语言的目标,它将尝试通过将其分解成子任务,并在自动循环中使用互联网和其他工具来实现这一目标

【电动汽车充电站有序充电调度的分散式优化】基于蒙特卡诺和拉格朗日的电动汽车优化调度(分时电价调度)(Matlab代码实现)内容概要:本文介绍了基于蒙特卡洛和拉格朗日方法的电动汽车充电站有序充电调度优化方案,重点在于采用分散式优化策略应对分时电价机制下的充电需求管理。通过构建数学模型,结合不确定性因素如用户充电行为和电网负荷波动,利用蒙特卡洛模拟生成大量场景,并运用拉格朗日松弛法对复杂问题进行分解求解,从而实现全局最优或近似最优的充电调度计划。该方法有效降低了电网峰值负荷压力,提升了充电站运营效率与经济效益,同时兼顾用户充电便利性。 适合人群:具备一定电力系统、优化算法和Matlab编程基础的高校研究生、科研人员及从事智能电网、电动汽车相关领域的工程技术人员。 使用场景及目标:①应用于电动汽车充电站的日常运营管理,优化充电负荷分布;②服务于城市智能交通系统规划,提升电网与交通系统的协同水平;③作为学术研究案例,用于验证分散式优化算法在复杂能源系统中的有效性。 阅读建议:建议读者结合Matlab代码实现部分,深入理解蒙特卡洛模拟与拉格朗日松弛法的具体实施步骤,重点关注场景生成、约束处理与迭代收敛过程,以便在实际项目中灵活应用与改进。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值