ACdream区域赛指导赛之手速赛系列(4) A Bad Horse
2014-08-08 21:40
309 查看
题目:
SubmitStatisticNext
Problem
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.
T test cases follow.
Each test case starts with a positive integer M (1 ≤ M ≤ 100) 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.
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.
containing a troublesome pair.
传送门:点击打开链接
解题思路:
种类并查集,这里的名字用map映射即可。
代码:
Bad Horse
Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others)SubmitStatisticNext
Problem
Problem Description
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 (1 ≤ T ≤ 100).T test cases follow.
Each test case starts with a positive integer M (1 ≤ M ≤ 100) 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.
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.
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 groupscontaining a troublesome pair.
Sample Input
2 1 Dead_Bowie Fake_Thomas_Jefferson 3 Dead_Bowie Fake_Thomas_Jefferson Fake_Thomas_Jefferson Fury_Leika Fury_Leika Dead_Bowie
Sample Output
Case #1: Yes Case #2: No
传送门:点击打开链接
解题思路:
种类并查集,这里的名字用map映射即可。
代码:
#include <cstdio> #include <map> #include <iostream> #include <string> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 205; int set[MAXN<<1]; map<string, int> mp; int t, n; int find(int p) { if(set[p] < 0) return p; return set[p] = find(set[p]); } void join(int p, int q) { p = find(p), q = find(q); if(p != q) set[p] = q; } int main() { scanf("%d", &t); int w = 1; while(t--) { scanf("%d", &n); memset(set, -1, sizeof(set)); mp.clear(); bool flag = true; int cnt = 1; while(n--) { string sa, sb; cin >> sa >> sb; if(!mp[sa]) mp[sa] = cnt++; if(!mp[sb]) mp[sb] = cnt++; if(find(mp[sa])==find(mp[sb]) || find(mp[sa]+200)==find(mp[sb]+200)) { flag = false; } else { join(mp[sa], mp[sb]+200); join(mp[sa]+200, mp[sb]); } } printf("Case #%d: %s\n", w++, flag ? "Yes" : "No"); } return 0; }
相关文章推荐
- HUST_ACdream区域赛指导赛之手速赛系列(1)(2)G——BFS——Cutting Figure
- ACdream区域赛指导赛之手速赛系列(6)
- ACdream区域赛指导赛之手速赛系列(4) E Tic-Tac-Toe-Tomek
- ACdream区域赛指导赛之手速赛系列(6) A.
- ACdream区域赛指导赛之手速赛系列(6) 解题报告
- HUST_ACdream区域赛指导赛之手速赛系列(1)(2)B——usigned long long ——郭式树(未完成)
- ACdream区域赛指导赛之手速赛系列(5) 题解
- ACdream区域赛指导赛之手速赛系列(5) 解题报告 A C D
- HUST_ACdream区域赛指导赛之手速赛系列(1)(2)D——数学——Triangles
- ACdream区域赛指导赛之手速赛系列(7)
- HUST_ACdream区域赛指导赛之手速赛系列(1)(2)F——GCD+1ll——LCM Challenge
- ACdream区域赛指导赛之手速赛系列(2)
- ACdream区域赛指导赛之手速赛系列(4)题解
- ACdream区域赛指导赛之手速赛系列(7)
- ACdream区域赛指导赛之手速赛系列(4) C Captain Hammer
- ACdream区域赛指导赛之手速赛系列(4)
- ACdream 区域赛指导赛之专题赛系列(1)の数学专场 A - EOF女神的相反数 (反转2进制)
- ACdream区域赛指导赛之专题赛系列(1)の数学专场
- ACdream区域赛指导赛之专题赛系列(1)の数学专场
- 算法系列之十二:多边形区域填充算法--几种边标志填充算法