ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Thrall’s Dream HRBUST - 2048【BFS or 强连通分量】

2021-01-29 20:32:01  阅读:170  来源: 互联网

标签:HRBUST Grom int Thrall 2048 gg vis Kalimdor


立志用最少的代码做最高效的表达


We never paid any heed to the ancient prophecies, like fools we clung to the old hatreds, and fought as we had for generations. Until one day the sky rained fire, and a new enemy came upon us. We stand now upon the brink of destruction, for the Reign of Chaos has come at last.

Thrall, the warchief of the Orcish Horde, all along, he led his tribe live in the fringe of Lordaeron under the human control. In a downpour night, Thrall falls into sleep in a Orc hall at Arathi Highlands, at this moment he heard a voice:

“The sands of time have run out, son of Durotan. The cries of war echo upon the winds, the remnants of the past scar the land which is besieged once again by conflict. Heroes arise to challenge fate, and lead their brethren to battle. As mortal armies rush blindly towards their doom, The Burning Shadow comes to consume us all. You must rally the Horde, and lead your people to their destiny.

I will answer all of your questions in time, young warchief. For now, rally your warriors and prepare to leave this land, cross the sea to the distant land of Kalimdor. We will speak again. ”

Thrall believes the prophesy of Blood Raven Medivh. Three days later, He and Grom Hellscream’s Warsong Clan meet in the Lordaeron coast to the distant lands of Kalimdor. But the Goblin Zeppelins they take encountered storms in the middle. Thrall and Grom falling to the islands, they want to find each other and then to Kalimdor.

For the sake of simplicity, we assume that Thrall and Grom may fall into any islands x and y, only by Thrall to find Grom or by Grom to find Thrall. Give you the map of this island, please judge that Thrall and Gtom can meet?

Input
There are multiple test case in the input file, first line is a case number T. Each test case will begin with two integers N (0 <= N < 2001) and M (0 <= M < 10001), where N is the number of islands and M is number of portal. Next M lines each line contains two integers a and b, indicated there is a portal in island a that people can go from a to b by this portal. The island numbered from 1 to N.

Output
For each test case, your output should be in one line with “Kalimdor is just ahead” (without quotes, hereinafter the same) if Thrall and Grom can meet or “The Burning Shadow consume us all” otherwise as indicated in the sample output.

Sample Input
2
3 2
1 2
1 3
3 2
1 2
2 3
Sample Output
Case 1: The Burning Shadow consume us all
Case 2: Kalimdor is just ahead


分析

题意:n个点,m条路,单向路(只能从a到b)。 问任意两个点是否都连通。

分析

1、BFS
模板题,开大数组存储全部点,最后判断即可。

2、强联通分量
明天更新~


代码一:BFS解法

#include<iostream>
#include<queue>
#include<cstring>
#define maxn 2010
using namespace std;
typedef long long gg;

gg n, m;
vector<gg>g[maxn];
bool has[maxn][maxn];	//连通数组 
bool vis[maxn];			//每次bfs时判断是否可见 
bool ok;

void bfs(int s) {
	memset(vis, 0, sizeof(vis));		//vis置0 
	vis[s] = 1;
	queue<int>q;
	q.push(s);
	
	while(!q.empty()) {					//遍历数组 
		int first = q.front();
		q.pop();
		int len = g[first].size();
		for(int i = 0; i < len; i++) {
			int v = g[first][i];
			if(vis[v]) continue;		//如果已经遍历过,则跳过
			has[s][v] = 1;
			q.push(v);
			vis[v] = 1; 
		}
	}
}

int main() {
	gg t, c = 1;
	scanf("%lld", &t);
	while(t--) {
		ok = true;
		scanf("%lld%lld", &n, &m);
		
		memset(has, 0, sizeof(has));	//1、初始化 
		for(gg i = 0; i < maxn; i++) 
			g[i].clear();
			
		gg u, v;
		while(m--) {					//2、数值存储, 
			scanf("%lld%lld", &u, &v);
			g[u].push_back(v);
		}
		for(gg i = 1; i <= n; i++) 		//3、逐个bfs,生成has数组 
			bfs(i);
		
		for(int i = 1; i <= n; i++) {	//判断是否连通 
			for(int j = i+1; j <= n; j++) {
				if(!has[i][j] && !has[j][i]) {	//只要有一个不连通 
					ok = false; break;
				}
			}
			if(ok == false) break;
		} 
		if(ok)
			printf("Case %d: Kalimdor is just ahead\n", c++);
		else 
			printf("Case %d: The Burning Shadow consume us all\n",c++);
	}
	return 0;
} 

耗时

500ms

标签:HRBUST,Grom,int,Thrall,2048,gg,vis,Kalimdor
来源: https://blog.csdn.net/weixin_43899069/article/details/113405615

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有