HDU - 5874 Friends and Enemies (思维or构造)

On an isolated island, lived some dwarves. A king (not a dwarf) ruled the island and the seas nearby, there are abundant cobblestones of varying colors on the island. Every two dwarves on the island are either friends or enemies. One day, the king demanded that each dwarf on the island (not including the king himself, of course) wear a stone necklace according to the following rules:
  
  For any two dwarves, if they are friends, at least one of the stones from each of their necklaces are of the same color; and if they are enemies, any two stones from each of their necklaces should be of different colors. Note that a necklace can be empty.
  
  Now, given the population and the number of colors of stones on the island, you are going to judge if it's possible for each dwarf to prepare himself a necklace.

Input

Multiple test cases, process till end of the input.
  
  For each test case, the one and only line contains 2 positive integers M,N (M,N<2^31)

representing the total number of dwarves (not including the king) and the number of colors of stones on the island.

Output

For each test case, The one and only line of output should contain a character indicating if it is possible to finish the king's assignment. Output ``T" (without quotes) if possible, ``F" (without quotes) otherwise.

Sample Input

20 100

Sample Output

T

题意:

 M个人,N种颜色的石头(M,N<2^31),任意两个人之间不是朋友就是敌人,每个人都有一个石头项链,项链上可以串多种颜色的石头(也可以是空项链),朋友间至少有一种相同颜色,敌人间不能有相同颜色。现在只告诉你M和N的值,不告诉你人们之间的关系,求能否满足最坏情况(题意不清晰,但确实是问能否满足最坏情况)。

借鉴大神的题解:

首先,M个人,是朋友的连一条边代表有一种相同颜色。那么就有了一张图。

  现在考虑,X和Y,Z是朋友,Y和Z也是朋友,那么图上就形成了一个三角形XYZ,有三条边代表需要三种颜色的石头,但其实XYZ都互为朋友只需要一种颜色的石头就可以满足。

  而如果X与Y,Z是朋友,Y和Z是敌人,那么就需要两种颜色,(Y Z 不同色)。所以有三角形的情况不是最坏情况。而我们要考虑的是最坏情况。

  所以最终最坏情况的图是没有三角形的,即为一张完全二分图。左边集合X里的人互为敌人,右边集合Y里的人互为敌人(无三角形),X里每个点到Y的每个点都有连边

  总边数为X*Y,表示需要的石头颜色种数,又有X+Y=M,所以X=M/2,Y=M-M/2时,X*Y最大,为最坏情况,只需比较X*Y和N的大小即可。

代码:

#include <cstdio>

using namespace std;

int main(){
	
	long long N,M;
	while(scanf("%lld %lld",&M,&N) == 2){
		long long ans = M * M / 4;
		if(N >= ans)printf("T\n");
		else printf("F\n");
	}
	
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值