ACdream 1056 Bad Horse (種類並查集),acdream1056

來源:互聯網
上載者:User

ACdream 1056 Bad Horse (種類並查集),acdream1056

Bad Horse Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)SubmitStatusProblem 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



大致題意:據說這題是出自google挑戰賽吧,但是偶然在ACdream上見到。題目是講一個教派裡面有好多幫派,現在要將這些幫派劃分成兩個大幫派。其中題目中給出了好多組相互對立的幫派,讓你判斷這些幫派是否可以劃分為兩個大幫派。


解題思路:這裡用到了很常見的並查集,但是這裡有點小小的變化,就是要開兩個空間,就是每個並查集都開了一個額外的空間,這裡具體處理的時候是在200以後的空間裡再存有一下。

                   先用map把string映射到int(這裡用map將我們要進行編號操作的string(字串)映射到int,是一個很方便的處理string問題的手段。),然後合并a和b+200,a+200和b,當a與b或a+200與b+200在同一集合時,就產生了矛盾。


                   網上也有大神講可以用二分圖匹配做,暫時還不懂,就附上網址,感興趣的自己看吧       二分圖匹配詳解


AC代碼:

#include <cstdio>#include <algorithm>#include <map>#include <string>#include <iostream>using namespace std;const int maxn = 10001;int f[maxn];map<string, int> m;int find(int x){return x == f[x] ? x : f[x] = find(f[x]);}void unin(int x, int y){x = find(x);y = find(y);if(x != y){f[x] = y;}}int main(){#ifndef ONLINE_JUDGEfreopen("in.txt","r",stdin);#endifint T, n;string a, b;scanf("%d", &T);int cnt = 0;for(int t=1; t<=T; t++){scanf("%d", &n);int flag = 1;for(int i=0; i<maxn; i++)         //初始化f[]f[i] = i;for(int i=0; i<n; i++){cin >> a >> b;if(!m[a])   m[a] = cnt++;if(!m[b])   m[b] = cnt++;if(find( m[a] ) == find( m[b] ) || find( m[a]+200 ) == find( m[b]+200 ) )       //產生矛盾的判斷flag = 0;else{unin(m[a], m[b]+200);unin(m[a]+200, m[b]);}}printf("Case #%d: %s\n", t, flag ? "Yes" : "No"); }return 0;}





相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.