hdu 5874 Friends and Enemies -ICPC网络赛大连-二分图

来源:互联网 发布:python sys.exit 1 编辑:程序博客网 时间:2024/05/16 11:15
Friends and Enemies

Problem Description
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<231) 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
 

Source
2016 ACM/ICPC Asia Regional Dalian Online
 

题意

有M个人N种颜色的石头 M个人中每两个人 不是朋友就是敌人 ,现在他们每个人要用石头要串一条项链 要求是
1.朋友之间的项链至少有一个相同颜色的石头
2.敌人之间没有颜色相同的石头
3.项链可以是空的 
问N种颜色的石头能不能满足这M个人

解题思路
二分图,把所有人分为两部分A,B,每一部分之间都是敌人,与对面都是朋友
假设A组里有x个人,那么B组有m-x个人,每一组里的人不能一样所以石头必须有x种颜色才能满足A组
然后 对与B组的每一个人,都要与A组的人有一块相同色的石头,这个人至少有x个颜色,B组有m-x个人,所以,一共颜色是x*(m-x)
所以最坏的情况是使x*(m-x)最大,根据高中数学知识求个导,发现当x=m/2时候是最大的,所以石头颜色数量应该满足n>=m*m/4

代码
#include<cstdio>#include<iostream>#include<deque>using namespace std;int main(){    int n,m;    while(scanf("%d%d",&m,&n)!=EOF)    {        if(n>=m*m/4)            printf("T\n");        else printf("F\n");    }    return 0;}


1 0
原创粉丝点击