1. 程式人生 > >Bad Horse -google-判斷是否是二分圖

Bad Horse -google-判斷是否是二分圖

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

推薦指數:※※※

來源:google

可以轉換成典型的判斷是否是二分圖問題。

BFS採用圖著色。如果兩個顏色可以,這是二分圖,否則不是。

注意多個不連通子圖的情況

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<map>
#include<queue>
#include<string>
using namespace std;
const int T=201;
int    color[T],visited[T];
int bfs(int index,vector<vector<int> > *adj,int n){
	int i,j;
	memset(color,-1,sizeof(color));
	memset(visited,0,sizeof(visited));
	queue<int >q;
	q.push(index);
	visited[index]=true;
	color[index]=0;
	while(!q.empty()){
		int tmp_now=q.front();
		for(i=0;i<(*adj)[tmp_now].size();i++){
			int tmp_index=(*adj)[tmp_now][i];
			if(color[tmp_index]==-1){
				color[tmp_index]=(color[tmp_now]+1)%2;
				q.push(tmp_index);
				visited[tmp_index]=true;
			}
			else if(color[tmp_index]==color[tmp_now]&&tmp_index!=tmp_now)//color is same and is a new node
				return false;
		}
		q.pop();
	}
	for(i=0;i<n;i++){
		if(visited[i]==false){
			return bfs(i,adj,n);
		}
	}
	return true;
}
int main()
{
	freopen("a.in", "r", stdin);
   freopen("a.out", "w", stdout);
	int loops;
	scanf("%d",&loops);
	int casenum;
	for(casenum=1;casenum<=loops;casenum++){
		int pairs,i;
		scanf("%d",&pairs);
		vector<vector<int> >adj;
		adj.resize(T);
		map<string,int> mp;
		int m_id=0;
		for(i=0;i<pairs;i++){
			string a,b;
			int tmp_a,tmp_b;
			cin>>a>>b;
			if(mp.count(a)>0){//hash
				tmp_a=mp[a];
			}
			else{
				mp[a]=m_id;
				tmp_a=m_id;
				m_id++;
			}
			if(mp.count(b)>0){//hash 
				tmp_b=mp[b];
			}
			else{
				mp[b]=m_id;
				tmp_b=m_id;
				m_id++;
			}
			adj[tmp_a].push_back(tmp_b);
			adj[tmp_b].push_back(tmp_a);
		}
		if(bfs(0,&adj,m_id)==true)
			printf("Case #%d: Yes\n",casenum);
		else
			printf("Case #%d: No\n",casenum);
	}
	return 0;
}

相關推薦

Bad Horse -google-判斷是否是二分

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

google要線上筆試----bad horse 二分

一開始想得不夠全面,很傻很天真: 輸入如果兩個都存在,並且相等那麼則出錯 輸入如果一個存在那麼另一個則與其相反 輸入如果都不存在那麼一個為1一個為0,就是這個地方,01選擇的問題最終決定還是二分圖吧 想要不等他輸完就出結果,顯然不對,當然測試用例通過了百分之90 多,但是有

二分匹配入門專題1】M - Cyclic Tour hdu1853【km算法--判斷自重邊】

初始化 case test case 思路 contain first rst ant eve There are N cities in our country, and M one-way roads connecting them. Now Little Tom wa

[論][二分判斷]The Accomodation of Students

member ali itl 回路 input The turn 匹配 previous Problem Description There are a group of students. Some of them may know each other, while

二分判斷-筆試題

不同 解析 string code max c++ include href ons 第四範式秋招筆試題 題目 - 給定一個具有n個頂點的圖。要給圖上每個頂點染色,並且要使相鄰的頂點顏色不同。判斷是否能最多用兩種顏色進行染色。題目保證沒有重邊和自環。 概念:把相鄰

HDU 2444 (判斷二分+二分最大匹配)

題目連結 題意:首先判斷這個圖是否是二分圖,如果不是二分圖就輸出no,如果是二分圖就輸出最大匹配數 判斷二分圖可以用染色法。。bfs一次就可以 求二分圖最大匹配我用的匈牙利 #include<stdio.h> #include<iostream> #inclu

[leetcode]785. Is Graph Bipartite? [bai'pɑrtait] 判斷二分

Given an undirected graph, return true if and only if it is bipartite. Example 1: Input: [[1,3], [0,2], [1,3], [0,2]] Output: true Explanat

HDU2444(二分的最大匹配+染色法判斷二分

HDU2444   n=200,邊要開到1e5才能過。。。。。。   #include<iostream> #include<cstdio> #include<cstring> #include<queue> #

785. 判斷二分

題目 給定一個無向圖graph,當這個圖為二分圖時返回true。 如果我們能將一個圖的節點集合分割成兩個獨立的子集A和B,並使圖中的每一條邊的兩個節點一個來自A集合,一個來自B集合,我們就將這個圖稱為二分圖。 graph將會以鄰接表方式給出,graph[i]表示圖中與節點i相連的

二分判斷(dfs)

二分圖著色法判斷 #include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> using namespace std; const int maxn =

Bicoloring UVA - 10004 二分判斷

\(\color{#0066ff}{題目描述}\) 多組資料,n=0結束,每次一個n,m,之後是邊,問你是不是二分圖 \(\color{#0066ff}{輸入樣例}\) 3 3 0 1 1 2 2 0 3 2 0 1 1 2 9 8 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0

洛谷P1330 封鎖陽光大學 二分判斷+dfs

傳送門 如果這個圖不是二分圖就輸出impossible 否則對圖進行染色(圖可能不是聯通的),對每一個聯通塊染成兩種顏色(比如-1和1),然後統計-1和1的個數,用一個數組儲存他們中的較小值,最後對這個陣列求和就行了 #include<iostream> #include<vec

UVA1627-Team them up!(二分判斷+動態規劃)

Problem UVA1627-Team them up! Total Submissions:1228  Solved:139 Time Limit: 3000 mSec Problem Description Your task is to divide a numbe

二分 板題 HDU2444 判斷是否為二分&求二分最大匹配

The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss

二分判斷

二分圖又稱為二部圖,其定義是:設G=(V,E)是一個無向圖。如頂點集V可分割為兩個互不相交的子集,並且圖中每條邊依附的兩個頂點都分屬兩個不同的子集。則稱圖G為二分圖。也就是說在二分圖中,頂點可以分為兩個集合X和Y,每一條邊的兩個頂點都分別位於X和Y集合中。它滿足這樣一個特性,

hdu 2444 The Accomodation of Students (判斷是否是二分,最大匹配)

小記:這題主要是判斷給定的圖是否是二分圖匹配,如果是的,求出最大匹配。這題資料比較水。 思路:求圖是否可以二分,可以使用dfs染色,或者bfs染色,或者並查集, 這裡我使用的是dfs染色,比較簡單的一種。 程式碼: #include <iostream> #

soj4522 完全二分判斷

題意: 有三個字母a,b,c,只有相鄰字母或者相同字母才連邊。現在給你一個無向圖,問你是否是這個規則建立出來的。 分析: - wa了很多次,還是思路沒有分析好 - 觀察發現只有a,c這對之間不能有邊相連線,圖2著色問題,就是完全二分圖。 - 注意除了孤

判斷一個無向是否為二分

要求:先設計演算法,然後用程式實現。程式可允許輸入一個無向圖,然後自動判斷是否為二分圖 注:一個圖G=(V,E)是二分圖如果存在V的一個劃分V= XY,其中XY=空集。 話不多說,直接上程式碼 package test; import java.util.Scanner

NYOJ-1015(判斷是否為二分)

二部圖 時間限制:1000 ms | 記憶體限制:65535 KB 難度:1 描述 二部圖又叫二分圖,我們不是求它的二分圖最大匹配,也不是完美匹配,也不是多重匹配,而是證明一個圖是不是二部圖

BFS/DFS 判斷是否是二分

package Graph; import java.util.ArrayDeque; import java.util.LinkedList; import java.util.Queue; impo