Tournament CodeForces - 27B(dfs)

The tournament «Sleepyhead-2010» in the rapid falling asleep has just finished in Berland. n best participants from the country have participated in it. The tournament consists of games, each of them is a match between two participants. n·(n - 1) / 2 games were played during the tournament, and each participant had a match with each other participant.

The rules of the game are quite simple — the participant who falls asleep first wins. The secretary made a record of each game in the form «xi yi», where xi and yi are the numbers of participants. The first number in each pair is a winner (i.e. xi is a winner and yi is a loser). There is no draws.

Recently researches form the «Institute Of Sleep» have found that every person is characterized by a value pj — the speed of falling asleep. The person who has lower speed wins. Every person has its own value pj, constant during the life.

It is known that all participants of the tournament have distinct speeds of falling asleep. Also it was found that the secretary made records about all the games except one. You are to find the result of the missing game.

Input
The first line contains one integer n (3 ≤ n ≤ 50) — the number of participants. The following n·(n - 1) / 2 - 1 lines contain the results of the games. Each game is described in a single line by two integers xi, yi (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi и yi are the numbers of the opponents in this game. It is known that during the tournament each of the n participants played n - 1 games, one game with each other participant.

Output
Output two integers x and y — the missing record. If there are several solutions, output any of them.

Examples
Input
4
4 2
4 1
2 3
2 1
3 1
Output
4 3
题意:n个人,每两个人都要比一场,给出的场次比原有场次少一场,输出那一场的正确顺序(赢得在前,输的在后)
思路:我们可以找出来那两场是谁和谁打,然后再图中判断联通就可以了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=5e3+100;
struct node{
	int x;
	int y;
	bool operator<(const node &a)const{
		if(a.x!=x) return x<a.x;
		else return y<a.y;
	}
}p[maxx];
vector<int> s[maxx];
int vis[maxx];
int n;

inline void dfs(int u,int v,int &flag)
{
	vis[u]=1;
	if(flag) return ;
	for(int i=0;i<s[u].size();i++)
	{
		int to=s[u][i];
		if(to==v)
		{
			flag=1;
			return ;
		}
		if(!vis[to]) dfs(to,v,flag);
	}
}
int main()
{
	scanf("%d",&n);
	int x,y;
	int m=n;
	n=(n-1)*n/2-1;
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&x,&y);
		s[x].push_back(y);
		if(x>y) swap(x,y);
		p[i].x=x;p[i].y=y;
	}
	sort(p+1,p+1+n);
	int l=1,r=2;
	for(int i=1;i<=n;i++)
	{
		if(p[i].x!=l||p[i].y!=r) break;
		else
		{
			if(r==m) 
			{
				l++;
				r=l+1;
			}
			else r++;
		}
	}
	int flag=0;
	dfs(l,r,flag);
	if(flag) cout<<l<<" "<<r<<endl;
	else cout<<r<<" "<<l<<endl;
}

努力加油a啊,(o)/~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值