Google Practice Round_A: Bad Horse

本文深入探讨了信息技术领域的多个关键子领域,包括前端开发、后端开发、移动开发等,通过具体实例展示了各子领域的核心概念和技术实践。文章旨在为读者提供全面的技术视野,帮助理解信息技术如何在不同场景中发挥重要作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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, T. T 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 
 
2
1
Dead_Bowie Fake_Thomas_Jefferson
3
Dead_Bowie Fake_Thomas_Jefferson
Fake_Thomas_Jefferson Fury_Leika

Fury_Leika Dead_Bowie


Output


Case #1: Yes

Case #2: No


备注:先构建图,然后判断图是否二分。构建图时用map建立名字的映射。注意图不连通时的判断(代码已修正,感谢tommyhu111朋友的指正

#include<stdio.h>
#include<string>
#include<map>
#include<vector>
#include<queue>
using namespace std;

const int MAXSIZE = 1000;
typedef struct Node NODE;

struct Node
{
	int color;
	vector<int> neigh_list;
};

bool checkAllNodesVisited(NODE *graph, int numNodes, int &index)
{
	for (int i=0; i<numNodes; i++)
	{
		if (graph[i].color == -1)
		{
			index = i;
			return false;
		}
	}
	return true;
}

bool JudgeBiGraph(NODE *graph, int numNodes)
{
	int start = 0;
	do 
	{
		queue<int> Myqueue;
		Myqueue.push(start);
		graph[start].color = 0;

		while(!Myqueue.empty())
		{
			int gid = Myqueue.front();
			for(int i=0; i<graph[gid].neigh_list.size(); i++)
			{
				int neighid = graph[gid].neigh_list[i];
				if(graph[neighid].color == -1)
				{
					graph[neighid].color = (graph[gid].color+1)%2; // assign to another group
					Myqueue.push(neighid);
				}
				else
				{
					if(graph[neighid].color == graph[gid].color) // touble pair in the same group
					return false;
				}
			}
			Myqueue.pop();
		}
	}while (!checkAllNodesVisited(graph, numNodes, start));

	return true;
}

int main()
{
	int T;
	int M;
	int caseid = 0;
	scanf("%d",&T);
	
	while(caseid<T)
	{
		scanf("%d",&M);
		map<string,int> Mymap;
		NODE graph[MAXSIZE];

		int id = 0;
		for(int i=0;i<M;i++)
		{
			char s1[MAXSIZE],s2[MAXSIZE];
			scanf("%s %s",s1,s2);
			string str_s1(s1),str_s2(s2);
			NODE node1,node2;
			int id1,id2;
			if(Mymap.count(str_s1)==0)
			{
				Mymap[str_s1] = id;
				id1 = id;
				id++;
			}
			else
				id1 = Mymap[str_s1];
			if(Mymap.count(str_s2)==0)
			{
				Mymap[str_s2] = id;
				id2 = id;
				id++;
			}
			else
				id2 = Mymap[str_s2];
			graph[id1].neigh_list.push_back(id2);
			graph[id2].neigh_list.push_back(id1);
		}

		//judge whether it is a bipartite graph
		for(int i=0;i<id;i++)
		{
			graph[i].color = -1;
		}
		if(JudgeBiGraph(graph,id))
			printf("Case #%d: Yes\n",caseid+1);
		else
			printf("Case #%d: No\n",caseid+1);
		caseid++;
	}
	return 0;
}

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值