Friends and Enemies(思维)

Friends and Enemies

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1379    Accepted Submission(s): 655


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
 

题目大意:

就是有M个人N种颜色的石头 M个人中每两个人不是朋友就是敌人

现在他们每个人要用石头要串一条项链要求是

1.朋友之间的项链至少有一个相同颜色的石头

2.敌人之间没有颜色相同的石头

3.项链可以使空的就是不串石头

问N种颜色的石头能不能满足这M个人

思路:因为每两个人之间的关系只有朋友或者敌人,所以我们可以根据这个把这些人分成两部分,所以我们可以把这个关系模拟成一个二分图;那么究竟是同侧是朋友,还是异侧是朋友呢,假如是同侧的话,只需要两种颜色就好了,那么是异侧的时候,就有(m/2)*(m-m/2)种情况,因为是要考虑最坏的情况,所以要选异侧是朋友。

//2016.9.10
#include <iostream>
#include <cstdio>

using namespace std;

long long fun(int m)
{
    long long ans;
    ans = (m/2)*(m/2);
    if(m%2)ans += (m/2);
    return ans;
}

int main()
{
    long long n, m, res;
    while(scanf("%lld%lld", &m, &n)!=EOF)
    {
        res = fun(m);
        if(n>=res)
          printf("T
");
        else printf("F
");
    }

    return 0;
}
原文地址:https://www.cnblogs.com/xiechenxi/p/8695180.html