[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, 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
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;}
- [Practice Round APAC test 2016]Problem A. Bad Horse(Google 2016笔试题)
- Practice Round APAC test Problem A. Bad Horse 2-sat
- Google APAC 2016 University Graduates Test Practice Round APAC test
- Problem A. gRanks Google APAC 2016 University Test Round C
- Problem A. Travel Google APAC 2016 University Test Round B
- Problem B. gCube Google APAC 2016 University Test Round A
- Problem C. gCampus Google APAC 2016 University Test Round A
- Problem A. Lazy Spelling Bee Google APAC 2017 University Test Practice Round
- Practice Round APAC test 2017--Problem A. Lazy Spelling Bee
- Problem A. Googol String Google APAC 2016 University Test Round A
- Problem D. gMatrix Google APAC 2016 University Test Round C
- Problem B. gFiles Google APAC 2016 University Test Round C
- Problem B. gBalloon Google APAC 2016 University Test Round D
- Problem B. gWheels Google APAC 2016 University Test Round B
- Problem C. gNumbers Google APAC 2016 University Test Round B
- Problem A. Dynamic Grid Google APAC 2016 University Test Round D
- Round A APAC Test 2016 Problem A. Googol String
- Round A APAC Test 2016 Problem A. Googol String
- Implement strStr()
- 自定义属性
- (4.3.2.10)android:descendantFocusability用法简析
- Rootkit技术的主要原理
- Servlet 3.0特性详解
- [Practice Round APAC test 2016]Problem A. Bad Horse(Google 2016笔试题)
- Win10下eclipse中打开图片乱码
- 错误:no acceptable C compiler found in $PATH
- Vim常用插件及配置
- POJ1804 Brainman (逆序对)
- Linux学习——数据流重定向与管道
- LINUX修改path环境变量
- 【数据结构】链表的原理及与其相关的常见面试题总结
- mysql设置批量更新