[Practice Round APAC test 2016]Problem A. Bad Horse(Google 2016笔试题)

来源:互联网 发布:linux 关闭oracle进程 编辑:程序博客网 时间:2024/05/22 17:25

Problem

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, TT test cases follow. Each test case starts with a positive integer M 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.

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.

Limits

1 ≤ T ≤ 100.
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.

Small dataset

1 ≤ M ≤ 10.

Large dataset

1 ≤ M ≤ 100.

Sample


Input 
 
Output 
 2
1
Dead_Bowie Fake_Thomas_Jefferson
3
Dead_Bowie Fake_Thomas_Jefferson
Fake_Thomas_Jefferson Fury_Leika
Fury_Leika Dead_Bowie
Case #1: Yes
Case #2: No


题解:二分图判定问题。或是染色问题。这里染两种颜色,1,2。DFS判定。也可以用并查集做。


#include <iostream>#include<map>#include<cstdio>#include<string>#include<vector>using namespace std;char s1[1001],s2[1001];bool res;void dfs(int cur,vector<int>& col,map<int,vector<int>>& edge) {    if(!res) return;    for(int i=0;i<edge[cur].size();i++) {        int nex=edge[cur][i];        if(col[nex]==col[cur]) {            res=false;return;        }    }    int nex_col;    if(col[cur]==1) nex_col=2;    else nex_col=1;    for(int i=0;i<edge[cur].size();i++) {        int nex=edge[cur][i];        if(col[nex]==0) {            col[nex]=nex_col;            dfs(nex,col,edge);        }    }}int main(){    freopen("A-small-practice-2.in", "r", stdin);    freopen("a2.out", "w", stdout);    int t,m;    scanf("%d",&t);    for(int cnt=1;cnt<=t;cnt++) {        scanf("%d",&m);        map<string,int> trans;        map<int,vector<int>> edge;        int num=1;        for(int i=0;i<m;i++) {            scanf("%s%s",s1,s2);            if(trans[s1]==0) trans[s1]=num++;            if(trans[s2]==0) trans[s2]=num++;            edge[trans[s1]].push_back(trans[s2]);            edge[trans[s2]].push_back(trans[s1]);        }        vector<int> col(num+1,0);        res=true;        for(int i=1;i<num;i++) {            if(col[i]==0) {                col[i]=1;                dfs(i,col,edge);                if(!res) break;            }        }        printf("Case #%d: ",cnt);        if(res) printf("Yes\n");        else printf("No\n");    }    return 0;}


0 0
原创粉丝点击