1. 程式人生 > >TZOJ--5449: King of the Waves(拓撲排序)

TZOJ--5449: King of the Waves(拓撲排序)

5449: King of the Waves 

時間限制(普通/Java):5000MS/15000MS     記憶體限制:65536KByte
總提交: 43            測試通過:9

描述

You are organising a king of the hill tournament, the Buenos Aires Paddleboarding Competition (BAPC), with n participants. In a king of the hill tournament, one person starts as a “king” and is then challenged by another person, the winning person becomes the new king. This is repeated until all participants have challenged exactly once (except for the starting person). In a paddleboarding match, there are no draws. The person which ends up as king, wins the tournament. Since you are the organiser, you get to choose the starting person and the order in which they challenge the king. 

Someone is offering you a substantial amount of money in case one of the participants, Henk, ends up winning the tournament. You happen to know, for any two participants x and y, which of the two would win if they were to match during the tournament. Consequently, you choose to do the unethical: you will try to rig the game. Can you find a schedule that makes Henk win the tournament?

輸入

The first line contains an integer 1 ≤ n ≤ 1000, the number of participants. The participants are numbered 0, . . . , n − 1, where Henk is 0. 

• Then n lines follow, where each line has exactly n characters (not counting the newline character). These lines represent the matrix with the information of who beats who, as follows. On line i the jth character is (note that 0 ≤ i, j < n): 

– ’1’ if person i will win against person j. 

– ’0’ if person i will lose against person j. 

– ’X’ if i = j.

輸出

If there exists a way to rig the game such that Henk wins, print "Yes", else "No".

樣例輸入

3
X10
0X1
10X

樣例輸出

 Yes

題目來源

BAPC 2017

題目上傳者

crq

 

題目連結:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=5449

 

題目大意:有N個人,給你一幅N*N的圖,第i行第J列表示i和j的比賽結果,1表示贏,2表示輸,問你能否尋找一種方案使得0號選手獲勝。那麼即尋找一種方案使得拓撲排序後0大於所有即可。(這題時間好長,應該其他方法瞎搞也可以過)

 

 

#include <bits/stdc++.h>
using namespace std;  
#define LL long long  
void closeio(){
	cin.tie(0);
   	ios::sync_with_stdio(0);
}
vector<int>g[1010];
char ma[1010][1010];
int vis[1001],n,ans;
int flag = 0;
void dfs(int v){
	vis[v] = 1;
	if(ans == n-1){
		flag = 1;
		return;
	}
	for(int i = 0 ; i < g[v].size() ; i ++){
		if(!vis[g[v][i]]){
			ans++;
			dfs(g[v][i]);
		}
	}
}
int main()
{
	scanf("%d",&n);
	flag = ans =0;
	for(int i = 0 ; i < n ; i++) g[i].clear();
	for(int i = 0 ; i < n ; i++){
		scanf("%s",ma[i]);
		for(int j = i + 1 ; j < n ; j++){
			if(ma[i][j] == '1'){
				g[i].push_back(j);
			}
			else{
				g[j].push_back(i);
			}
		}
	}
	if(n <= 1){
		puts("Yes");return 0;
	}
	memset(vis,0,sizeof(vis));
	dfs(0);
	if(flag)puts("Yes");
	else puts("No");
	return 0;
}