Google Practice Round_A: Bad Horse

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

### 命令作用 `git fetch origin fep_switch_practice:fep_switch_practice` 命令的作用是从远程仓库 `origin` 中获取 `fep_switch_practice` 分支的最新提交信息,并将这些信息更新到本地的 `fep_switch_practice` 分支上。不过,这里要注意的是,这个命令并不会自动合并远程分支到本地分支,它只是单纯地获取远程分支的最新状态。 ### 使用场景 - **同步远程分支状态**:当多人协作开发时,其他团队成员可能已经向远程的 `fep_switch_practice` 分支推送了新的提交。此时,通过该命令可以快速获取这些新提交的信息,以便了解远程分支的最新状态。 - **避免自动合并冲突**:如果不确定远程分支和本地分支的差异情况,或者不想立即进行合并操作,可以先使用 `git fetch` 命令获取远程分支的信息,之后再根据实际情况决定是否进行合并操作。 ### 命令解析 - `git fetch`:这是 Git 用于从远程仓库获取更新的基本命令。 - `origin`:表示远程仓库的名称,通常默认的远程仓库名称就是 `origin`。 - `fep_switch_practice:fep_switch_practice`:冒号前的 `fep_switch_practice` 是远程分支的名称,冒号后的 `fep_switch_practice` 是本地分支的名称。该命令会将远程 `origin` 仓库的 `fep_switch_practice` 分支的更新内容拉取到本地的 `fep_switch_practice` 分支上。 ### 可能出现的问题 - **本地分支不存在**:如果本地不存在 `fep_switch_practice` 分支,执行该命令时,Git 会尝试创建一个新的本地分支并拉取远程分支的内容。 - **本地分支有未提交的更改**:如果本地的 `fep_switch_practice` 分支有未提交的更改,可能会导致拉取操作失败。需要先提交或暂存这些更改,再执行 `git fetch` 命令。 - **远程分支不存在**:若远程仓库 `origin` 中不存在 `fep_switch_practice` 分支,执行该命令会报错。 ### 示例代码 ```bash # 执行获取操作 git fetch origin fep_switch_practice:fep_switch_practice # 查看获取后的状态 git log fep_switch_practice ```
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值