HDU 5874 Friends and Enemies(二分图思想)——2016 ACM/ICPC Asia Regional Dalian Online

来源:互联网 发布:cdn域名加速 编辑:程序博客网 时间:2024/04/28 14:51

此文章可以使用目录功能哟↑(点击上方[+])

 HDU 5874 Friends and Enemies

Accept: 0    Submit: 0
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

 Problem Idea

解题思路:

【题意】
有M个人,N种颜色的石头若干

M个人中每两个人,不是朋友就是敌人
现在他们每个人都要用石头串一条项链,要求如下: 
1.互为朋友的俩人的项链至少有一颗相同颜色的石头
2.互为敌人的俩人没有相同颜色的石头
3.项链可以是空的,即不串石头,这种情况下,空项链的人与其他任何人都是敌人 
问N种颜色的石头能不能满足这随机关系的M个人

【类型】
二分图思想
【分析】
比赛的时候,比较蠢,看AC的人用时和内存都比较小,就随意画画,找到了规律

但实际上,此题蕴含着二分图的思想

问能不能满足随机关系,那肯定是要和最坏的情况比较

也就是求最坏的情况下最少需要多少种颜色的石子
然后根据两个人的关系只有朋友和敌人这点

就可以想象成一个二分图,分别在两侧的人是朋友,同侧的是敌人

这样的话,就是求一个完全二分图的边数有多少就行了   
因为总共M个人,想要边数尽可能多,所以尽量均分,左右两边人数最接近就行了
这样的话,边数就是(m-m/2)*(
m/2)
之后再跟N比较一下就可以了

【时间复杂度&&优化】
O(1)

题目链接→HDU 5874 Friends and Enemies

 Source Code

/*Sherlock and Watson and Adler*/#pragma comment(linker, "/STACK:1024000000,1024000000")#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<bitset>#include<cmath>#include<complex>#include<string>#include<algorithm>#include<iostream>#define eps 1e-9#define LL long long#define PI acos(-1.0)#define bitnum(a) __builtin_popcount(a)using namespace std;const int N = 100005;const int M = 20005;const int inf = 1000000007;const int mod = 1000000007;int main(){    __int64 m,n;    while(~scanf("%I64d%I64d",&m,&n))        if(n>=(m-m/2)*(m/2))            puts("T");        else            puts("F");    return 0;}
菜鸟成长记

1 0
原创粉丝点击