HDU - 5874 Friends and Enemies [无三元环最大边数]

2016大连网络赛 G题 传送门:HDU - 5874


Friends and Enemies

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)

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<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

Source

2016 ACM/ICPC Asia Regional Dalian Online


题目大意:

  • M个人,每有一个彩色石头做的项链,每两两人互为朋友或敌人,要求两个朋友的项链至少有一颗石头颜色相同,两个敌人的项链不能有任何石头相同。
  • 给出M和石头颜色种类N,问不管这M个人是什么关系,这k种颜色的石头都能满足要求。
  • 石头数量无限,只限制颜色种类;项链上的石头个数可以为0。

基本思路:

  • 很明显要找到这M个人中 需要石头颜色最多 的关系情况。最先想到的是全是敌人,但是项链可以为空,空与空虽然相等,但是也满足敌人的要求。
  • 对于每一对朋友a和b,他们最少有一个共同颜色,连一条边表示这个颜色;如果存在c与a、b都是朋友,a、b、c这个三元环虽然有三条边,但是只贡献了一种颜色,显然是不利于颜色增加的;如果c与a、b都是敌人,则不影响;如果c与a是敌人、c与b是朋友,那么a、b的共同颜色就不会是b、c的共同颜色,a、b、c新增两种颜色,符合 边数=颜色数
  • 所以,要找M个点的图,没有三元环的条件下最多有多少个边。

具体实现:

  • 无三元环且边数最大,大胆猜测如果没有环,就是个满的二分图(学名:完全二分图),左边n个点右边m个点一共nm个边,n+m=M,最大的时候n为M/2,答案\(M^2/4\)。(正好就是样例的极限情况,于是就这么先A了=v=)
  • 那么回过头来考虑一下有没有比完全二分图更优的情况——没有,因为完全二分图上再加任意一条边都会产生三元环,并且没有办法在完全二分图上删一条边同时合法增两条边。

注意事项:

  • 有一个容易忽略的细节:实际答案不是\(M^2/4\),而应该是\(M/2*(M-M/2)\)。如果数据过大,前者会出错。
  • 在第一次WA之后,可能会考虑到\(M^2/4\)爆long long并改成\(M/4*M\),但还是会错。甚至是\(M/2*(M-M/2)4\)写成\(M*(M-M/2)/4\)也会出错,所以其实是有爆long long问题的

Codes:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    long long n,m;
    while(~scanf("%lld %lld",&m,&n))
    {
        m = m /2 * (m - m/2);
    	if(m <= n)
    		printf("T\n");
    	else
    		printf("F\n");
    }

    return 0;
}
posted @ 2018-08-30 15:24  摸鱼鱼  阅读(294)  评论(0编辑  收藏  举报