hdu 2094 产生冠军

文章目录

产生冠军

题目描述:

有一群人,打乒乓球比赛,两两捉对撕杀,每两个人之间最多打一场比赛。
球赛的规则如下:
如果A打败了B,B又打败了C,而A与C之间没有进行过比赛,那么就认定,A一定能打败C。
如果A打败了B,B又打败了C,而且,C又打败了A,那么A、B、C三者都不可能成为冠军。
根据这个规则,无需循环较量,或许就能确定冠军。你的任务就是面对一群比赛选手,在经过了若干场撕杀之后,确定是否已经实际上产生了冠军。

输入:
输入含有一些选手群,每群选手都以一个整数n(n<1000)开头,后跟n对选手的比赛结果,比赛结果以一对选手名字(中间隔一空格)表示,前者战胜后者。如果n为0,则表示输入结束。

输出:
对于每个选手群,若你判断出产生了冠军,则在一行中输出“Yes”,否则在一行中输出“No”。

样例输入:

3
Alice Bob
Smith John
Alice Smith
5
a c
c d
d e
b e
a d
0

样例输出:

Yes
No
题解

只判断产生不产生:set

解题思路

定义集合A B ,把所有人都放入A中,将失败者放入B中,他们会自动管理去重。那么如果A-B=1,则可以推断存在冠军,否则不能。

代码

#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<set>
#include<map>

// #include<bits/stdc++.h>

using namespace std;

int main()
{
	set<string> A,B;
	string s1,s2;
	int n;
	while(cin>>n&&n)
	{
		for(int i=0;i<n;i++)
		{
			cin>>s1>>s2;
			A.insert(s1);
			A.insert(s2);	// 将所有人放入A 
			B.insert(s2);	// 将失败的人放入B 
		}
		
		if(A.size()-B.size()==1)
			cout<<"Yes"<<endl;
		else
			cout<<"No"<<endl;
		A.clear();
		B.clear();	 
	} 
	
	return 0;
}

判断是否产生并且输出名字:map

解题思路

分析:本题使用STL中的map,根据题意可知,前者一定是打败后者,用map<string,int>string作关键字,int是value也就是第二数据。例如输入数据为a b,那么a一定打败b。操作步骤如下:
1、当前map中不存在a,那么将a添加进去,并设置map[a]=1;表示获胜。如果map里已经存在a了,那么要判断一下map[a]的值,如果map[a]的值为0,表示a曾经已经输了,也就是说a不可能成为冠军了,就不用修改其值。如果为1,保持不变。
2、对于b来说,b表示失败,只需将map[b]设置为0即可。
3、最后统计一下,map里面值为1的元素的个数,如果只有一个,表示冠军存在;其余情况表示不存在。

代码

#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<set>
#include<map>

// #include<bits/stdc++.h>

using namespace std;

//string到int的映射map
//string是关键字
map<string, int> Mp;
int in[2002];

int main()
{
    int n;
    while (scanf("%d", &n) != EOF && n != 0)
    {
        for (int i = 0; i < 2 * n; i++)
        {
            in[i] = 0;
        }
        Mp.clear();
        int idx = 0;
        for (int i = 0; i < n; i++)
        {
            char str1[50], str2[50];
            scanf("%s%s", str1, str2);
            string a = str1;
            string b = str2;
            int idxa, idxb;
            if (Mp.find(a) == Mp.end())
            {
                Mp[a] = 1;
            }
            else
            {
                if (Mp[a]==0)
                    Mp[a] = 0;
            }

            if (Mp.find(b) == Mp.end())
            {
                Mp[b] = 0;
            }
            else
            {
                Mp[b] = 0;
            }
        }
        
        map<string, int>::iterator it;
        int count = 0;
        for (it = Mp.begin(); it != Mp.end(); it++)
        {
            if (it->second == 1)
            {
                count++;
                cout << it->first << ' ' << it->second << endl;
            }

        }

        if (count == 1)
            cout << "Yes" << endl;
        else
            cout << "No" << endl;

    }

    return 0;
}





上一篇:HDU 2896


下一篇:hdu 4841 圆桌问题