1021 Deepest Root (25分)
题目链接:PAT A 1021
题目大意:给出一个数n代表节点的个数(节点编号1-n),给出各个边的连接方式,问哪个节点为根节点时,这棵树最高,如果有多个这样的结点,则按升序的方式输出。如果给出的图不是一棵树,那么输出它有几个部分组成。
思路分析:注意题中输入第一行是节点数目,接下来的n-1行是边。由于连通,节点数为n,边为n-1的图一定是一棵树,因此首先要判断给定的数据能不能使图连通。在我们熟悉的bfs遍历中,如果从一个点出发,只进行了一次bfs遍历,那么就意味着这个图一定是连通的。所以定义一个变量num,如果num大于1,说明进行了多次bfs遍历,说明图一定不是连通的,于是输出error,否则就是一棵树。接下来对这棵树的每个节点进行一次bfs遍历,是用结构体定义的节点,遍历时候要进行层数统计,同时定义一个答案数组。如果层数比最大层数大,就要清空答案数组,向里添加新的元素,如果层数与最大层数相等,就直接向里添加新的元素。最后用sort排序后输出即可。
下面先看一份错误代码,大概只能过两个测试点:
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
struct node{
int data, layer;
};
vector<node> g[10005];
vector<int> ans;
bool vis[10005] = {false};
int n, maxlayer = 1;
void bfs(int s) {
node start;
start.data = s;
start.layer = 1;
queue<node> q;
q.push(start);
vis[s] = true;
int templayer = 1;
while(q.empty() != true) {
node top = q.front();
q.pop();
int u = top.data;
for(int i = 0; i < g[u].size(); i++) {
node now = g[u][i];
if(vis[now.data] == false) {
now.layer = top.layer + 1;
q.push(now);
vis[now.data] = true;
}
templayer = max(templayer, now.layer);
}
}
if(templayer > maxlayer) {
maxlayer = templayer;
ans.clear();
ans.push_back(s);
}
else if(templayer == maxlayer)
ans.push_back(s);
}
void bfstravel() {
int num = 0;
for(int i = 1; i <= n; i++) {
if(vis[i] == false) {
bfs(i);
num++;
}
}
if(num > 1)
printf("Error: %d components", num);
else {
for(int i = 2; i <= n; i++) {
fill(vis, vis + 10005, false);
bfs(i);
}
sort(ans.begin(), ans.end());
for(int i = 0; i < ans.size(); i++)
printf("%d\n", ans[i]);
}
}
int main() {
int a, b;
scanf("%d", &n);
for(int i = 0; i < n - 1; i++) {
scanf("%d %d", &a, &b);
node temp;
temp.data = b;
g[a].push_back(temp);
temp.data = a;
g[b].push_back(temp);
}
bfstravel();
return 0;
}
这个代码与正确代码之间只有微小的差别,差别在于32行templayer = max(templayer, now.layer);这个语句的位置,如果放在if里则正确,放在if外则错误。这是因为如果放在if外面,如果vis[now.data]为真,那么会直接执行if外的语句,而我们输入时并没有指定vector数组g的层数,导致这个数是一个不确定的数,可能非常大,进而影响templayer的值。所以要将这句话放在if语句里面,就可以AC了。
AC代码:
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
struct node{
int data, layer; //数据域,层数
};
vector<node> g[10005]; //图
vector<int> ans; //答案数组
bool vis[10005] = {false};
int n, maxlayer = 1; //最大层数初始定义为1
void bfs(int s) {
node start;
start.data = s;
start.layer = 1; //根节点层数为1
queue<node> q;
q.push(start);
vis[s] = true;
int templayer = 1; //统计临时最大层数
while(q.empty() != true) {
node top = q.front();
q.pop();
int u = top.data;
for(int i = 0; i < g[u].size(); i++) { //bfs核心代码
node now = g[u][i];
if(vis[now.data] == false) {
now.layer = top.layer + 1; //层数+1
q.push(now);
vis[now.data] = true;
templayer = max(templayer, now.layer); //更新临时最大层数
}
}
}
if(templayer > maxlayer) { //比较
maxlayer = templayer;
ans.clear(); //清空临时数组
ans.push_back(s);
}
else if(templayer == maxlayer)
ans.push_back(s);
}
void bfstravel() {
int num = 0; //统计有几个部分
for(int i = 1; i <= n; i++) {
if(vis[i] == false) {
bfs(i);
num++;
}
}
if(num > 1)
printf("Error: %d components", num);
else {
for(int i = 2; i <= n; i++) { //把每一个节点都当成根节点进行统计
fill(vis, vis + 10005, false);
bfs(i);
}
sort(ans.begin(), ans.end());
for(int i = 0; i < ans.size(); i++)
printf("%d\n", ans[i]);
}
}
int main() {
int a, b;
scanf("%d", &n);
for(int i = 0; i < n - 1; i++) {
scanf("%d %d", &a, &b);
node temp;
temp.data = b;
g[a].push_back(temp);
temp.data = a;
g[b].push_back(temp);
}
bfstravel();
return 0;
}