acdream群賽(4)Bad Horse(種類並查集)
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
2 1 Dead_Bowie Fake_Thomas_Jefferson 3 Dead_Bowie Fake_Thomas_Jefferson Fake_Thomas_Jefferson Fury_Leika Fury_Leika Dead_Bowie
Sample Output
Case #1: Yes Case #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;
}
相關文章
- 【轉】種類並查集並查集
- POJ2492(種類並查集)並查集
- 各種並查集並查集
- 並查集(一)並查集的幾種實現並查集
- POJ-1182-食物鏈(並查集種類)並查集
- The Suspects-並查集(4)並查集
- 【並查集】一種與時間賽跑的巧妙演算法並查集演算法
- 並查集到帶權並查集並查集
- 2-4 朋友圈【並查集】並查集
- 轉載:一個python並查集類Python並查集
- 【並查集】【帶偏移的並查集】食物鏈並查集
- C4top-排座位(並查集)並查集
- zoj 3811||牡丹江網賽 c題 並查集並查集
- 3.1並查集並查集
- 並查集(小白)並查集
- 並查集 - 紅色警報 - 天梯賽 L2-013並查集
- ACdream oj 1191 Dragon Maze(手速賽 A)Go
- 並查集(Union Find)並查集
- 並查集應用並查集
- The Door Problem 並查集並查集
- 並查集練習並查集
- 並查集的使用並查集
- 並查集—應用並查集
- 寫模板, 並查集。並查集
- 並查集跳躍並查集
- 食物鏈(並查集)並查集
- 並查集(二)並查集的演算法應用案例上並查集演算法
- 並查集是一種怎樣的資料結構?並查集資料結構
- [leetcode] 並查集(Ⅰ)LeetCode並查集
- [leetcode] 並查集(Ⅱ)LeetCode並查集
- [leetcode] 並查集(Ⅲ)LeetCode並查集
- 並查集演算法並查集演算法
- 並查集深度應用並查集
- 並查集java實現並查集Java
- 並查集-Java實現並查集Java
- 並查集題目合集並查集
- 並查集以及應用並查集
- 並查集的應用並查集