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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值