zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    学号 20175212童皓桢 《Java程序设计》第8周学习总结
    20175212童皓桢 在IDEA中以TDD的方式对String类和Arrays类进行学习
    20175212童皓桢 Java实验二-面向对象程序设计实验报告
    学号20175212 《Java程序设计》第7周学习总结
    20175212童皓桢 结对编程项目-四则运算 第二周
    20175212童皓桢 类定义
    20175212童皓桢 《Java程序设计》第六周学习总结
    20175212童皓桢 结对编程项目-四则运算 第一周
    学号 20175212童皓桢 第五周迭代选做题学习
    学号 2018-2019-20175212 童皓桢《Java程序设计》第5周学习总结
  • 原文地址:https://www.cnblogs.com/xiechenxi/p/8695180.html
Copyright © 2011-2022 走看看