a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

PAT甲級(jí)1021

2019-11-14 10:15:38
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

1021. Deepest Root (25)

時(shí)間限制1500 ms內(nèi)存限制65536 kB代碼長(zhǎng)度限制16000 B判題程序Standard作者CHEN, Yue

A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=10000) which is the number of nodes, and hence the nodes are numbered from 1 to N. Then N-1 lines follow, each describes an edge by given the two adjacent nodes' numbers.

Output Specification:

For each test case, PRint each of the deepest roots in a line. If such a root is not unique, print them in increasing order of their numbers. In case that the given graph is not a tree, print "Error: K components" where K is the number of connected components in the graph.

Sample Input 1:
51 21 31 42 5Sample Output 1:
345Sample Input 2:
51 31 42 53 4Sample Output 2:
Error: 2 components
#include<cstdio>#include<vector>#include<set>#include<algorithm>using namespace std;const int maxn = 10000;vector<int> G[maxn];bool  vis[maxn] = {false};int maxdepth = 0;int N;set<int> deepestroots;void DFS(int u, int depth){	if (maxdepth < depth)	{		maxdepth = depth;		deepestroots.clear();		deepestroots.insert(u + 1);	}	else if (maxdepth == depth)	{		deepestroots.insert(u + 1);	}	vis[u] = true;	for (int i = 0; i < G[u].size(); i++)	{		int t = G[u][i];		if (!vis[t])		{			DFS(t, depth + 1);		}	}}void DFSTrave(vector<int>*G){	int count = 0;	for (int i = 0; i < N; i++)	{		if (!vis[i])		{			DFS(i, 1);			count++;		}	}	if (count == 1)	{		set<int>::iterator it = deepestroots.begin();		int s = *(it)-1;		maxdepth = 0;		fill(vis, vis + maxn, false);		set<int> deepestrootstemp = deepestroots;		deepestroots.clear();		DFS(s, 1);		it = deepestrootstemp.begin();		for (it; it != deepestrootstemp.end(); it++)		{			deepestroots.insert(*(it));		}		it = deepestroots.begin();		for (it; it != deepestroots.end(); it++)		{			printf("%d/n", *it);		}	}	else	{		printf("Error: %d components/n", count);	}}//只需至少兩次DFS就行,若太多會(huì)超時(shí),第一次DFS找出深度最大的那些點(diǎn),然后從中任選一個(gè)進(jìn)行//第二次DFS,再次找深度最大的哪些點(diǎn),這兩次DFS所找到的點(diǎn)的并集就是,注意去重和排序int main(){	scanf("%d", &N);	int u, v;	if (N == 1)	{		printf("1/n");		return 0;	}	for (int i = 0; i < N - 1; i++)	{		scanf("%d %d", &u, &v);		G[u - 1].push_back(v - 1);		G[v - 1].push_back(u - 1);	}	DFSTrave(G);	return 0;}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 四虎永久免费在线 | 日本三级视频在线观看 | 亚洲成人第一区 | 日日干天天操 | 国产精品一区二区三 | 亚洲欧美中文日韩v在线观看 | 91久久综合 | 欧美一级黄色片网站 | 一级免费黄视频 | 91亚洲视频在线观看 | 日本一二三区在线 | 日韩精品无玛区免费专区又长又大 | 日本超碰| 国产精品久久久久久久久久东京 | 冷水浴在线观看 | 野狼在线社区2017入口 | 中文字幕在线第一页 | 自拍第一页 | 国产中文视频 | 国产视频一区二区在线观看 | 91久久久www播放日本观看 | 亚洲在线一区二区 | 日韩视频一区二区三区在线观看 | 成年人在线看片 | 妞干网福利视频 | h片免费| 亚洲视频在线免费观看 | 99re视频在线 | 国产成人精品免费 | 特级淫片裸体免费看 | 在线播放国产一区二区三区 | 免费色网址 | 三级视频网站在线观看 | 国产一区在线视频 | 日韩欧美一区二区三区免费观看 | 天天色天天看 | 欧美激情欧美激情在线五月 | 日本精品一区二区三区视频 | 日本在线免费观看 | 中文字幕在线观看网站 | 国产高清在线观看 |