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
Sample Output
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;}菜鸟成长记
- HDU 5874 Friends and Enemies(二分图思想)——2016 ACM/ICPC Asia Regional Dalian Online
- 2016 ACM/ICPC Asia Regional Dalian Online Friends and Enemies
- HDU 5874 2016 ACM/ICPC Asia Regional Dalian Online 1007Friends and Enemies
- HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)
- HDU 5874 2016 ACM/ICPC Asia Regional Dalian Online 1007Friends and Enemies
- HDU 5875 Function(二分区间+RMQ)——2016 ACM/ICPC Asia Regional Dalian Online
- 2016 ACM/ICPC Dalian Online-1007 Friends and Enemies
- HDU 5873 Football Games(签到题)——2016 ACM/ICPC Asia Regional Dalian Online
- hdu 5875 2016 ACM/ICPC Asia Regional Dalian Online 1008
- HDU 5875 Function 2016 ACM/ICPC Asia Regional Dalian Online
- HDU 5875 Function 2016 ACM/ICPC Asia Regional Dalian Online
- hdu 5875 2016 ACM/ICPC Asia Regional Dalian Online 1008
- 2016 ACM/ICPC Asia Regional Dalian Online
- 2016 ACM/ICPC Asia Regional Dalian Online
- 补图+BFS( 邻接链表 )——HDU 5876 ( 2016 ACM/ICPC Asia Regional Dalian Online 1009 )
- HDU 5876 Sparse Graph(bfs求解补图中的单源最短路)——2016 ACM/ICPC Asia Regional Dalian Online
- HDU 5876 Sparse Graph(bfs求解补图中的单源最短路)——2016 ACM/ICPC Asia Regional Dalian Online
- 2016 ACM/ICPC Asia Regional Dalian Online 1006 football game 1007 hdu 5873 hdu 5874
- HDU 5876 Sparse Graph (补图找最短路/BFS)
- 欢迎使用CSDN-markdown编辑器
- 使用xinput_calibrator解决gtk程序无法校准的问题
- 【汇编学习】汇编语言读书笔记(第三章)
- tjut 3577
- HDU 5874 Friends and Enemies(二分图思想)——2016 ACM/ICPC Asia Regional Dalian Online
- opencv实现多图像读取并显示,sprintf_s函数,static_cast
- Bubble Cup 9 - Finals E. 搜索
- Android横竖屏切换
- 网络编程url,httpurlconnection,post,get
- java4_第一章_对象导论
- spark学习笔记3 spark应用程序之间的调度
- lintcode 给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符。
- 夜间模式切换的实现库,只需三行代码实现流畅切换