acdream群赛(4)Bad Horse(种类并查集)
来源:互联网 发布:黑蚂蚁网络电视 编辑:程序博客网 时间:2024/06/07 23:36
Problem Description
As the leader of the Evil League of Evil, Bad Horse has a lot of problems to deal with.
Most recently, there have been far too many arguments and far too much backstabbing in the League, so much so that Bad Horse has decided to split the league into two departments in order to separate troublesome members.
Being the Thoroughbred of Sin, Bad Horse isn't about to spend his valuable time figuring out how to split the League members by himself.
That what he's got you -- his loyal henchman -- for.
Input
The first line of the input gives the number of test cases, T (1 ≤ T ≤ 100).
T test cases follow.
Each test case starts with a positive integer M (1 ≤ M ≤ 100) on a line by itself -- the number of troublesome pairs of League members.
The next M lines each contain a pair of names, separated by a single space.
Each member name will consist of only letters and the underscore character "_".
Names are case-sensitive.
No pair will appear more than once in the same test case.
Each pair will contain two distinct League members.
Output
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is either "Yes" or "No", depending on whether the League members mentioned in the input can be split into two groups with neither of the groups containing a troublesome pair.
Sample Input
21Dead_Bowie Fake_Thomas_Jefferson3Dead_Bowie Fake_Thomas_JeffersonFake_Thomas_Jefferson Fury_LeikaFury_Leika Dead_Bowie
Sample Output
Case #1: YesCase #2: No
就是能不能把 所有的字符串分成两组,和食物链那题比较像
代码如下:
#include <iostream>#include <cstring>#include <cstdio>#include <set>#include <map>using namespace std;int par[500];void init(){ for(int i=0;i<500;i++) par[i]=i;}int find_par(int x){ if(par[x]!=x) return par[x]=find_par(par[x]); return x;}void Union(int x,int y){ int px=find_par(x); int py=find_par(y); if(px==py) return; par[px]=py;}int main(){ int t,m,ans=1; cin>>t; while(t--){ cin>>m; string a,b; map<string,int> p1; p1.clear(); init(); bool l=1; int cnt=1; for(int i=0;i<m;i++){ cin>>a>>b; if(!p1[a]) p1[a]=cnt++; if(!p1[b]) p1[b]=cnt++; if(find_par(p1[a])==find_par(p1[b])||find_par(p1[a]+101)==find_par(p1[b]+101)) l=0; else{ Union(p1[a],p1[b]+101); Union(p1[b],p1[a]+101); } } //for(it=p1.begin();it!=p1.end();it++) // cout<<it->first<<" "<<it->second<<endl; printf("Case #%d: ",ans++); if(l) puts("Yes"); else puts("No"); } return 0;}
- ACdream 1056 Bad Horse (种类并查集)
- acdream群赛(4)Bad Horse(种类并查集)
- ACdream 1056 Bad Horse (判断二分图)(并查集 or BFS)
- ACdream-1056 Bad Horse
- ACDREAM 01F(并查集)
- 分组并查集(种类并查集)
- 种类并查集(POJ 1703)
- poj1988(种类并查集)
- hdu1829(种类并查集)
- poj1703(种类并查集)
- ACdream1056(种类并查集)
- poj1182 食物链 (种类并查集)
- POJ1182:食物链(种类并查集)
- acdream 1685(并查集)
- 种类并查集
- 种类并查集
- 种类并查集
- 种类并查集
- Chrome 被和谐的解决办法
- VC中MessageBox的常见用法
- 河南省第六届acm省赛 探寻宝藏&& poj 3422 (最大费用最大流)
- PIC单片机红外遥控编码及识别.
- html添加的锚点
- acdream群赛(4)Bad Horse(种类并查集)
- LeetCode-Subsets
- C 语言计算精确时间
- C++第十九天 面向对象编程
- 如何让开饭板跑出图来
- LBaas 原理分析
- C 语言复杂声明的理解
- 042:hibernate多对一单向关联(xml)
- systemd的若干理解,相对于susVinit