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 calledthe deepest root.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤104) 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:
5
1 2
1 3
1 4
2 5
Sample Output 1:
3
4
5
Sample Input 2:
5
1 3
1 4
2 5
3 4
Sample Output 2:
Error: 2 components
我的思路:
1. 先判断连同分量,若 !=1 则输出Error,若 == 1, 此时再求最深root
2. dfs递归是我的短板 ,逻辑不太好写,自己写了一半就被自己绕晕... 后面补!
先上不完整代码:
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
using namespace std;
const int MAXN = 10010;
bool visit[MAXN];
int G[MAXN][MAXN];
int n; // 结点个数
int k; //连同分量个数
vector<int> maxRoot;
int maxDepth = 0;
void dfsConnected(int no)
{
visit[no] = true;
for (int v = 1; v <= n; v++)
{
if (visit[v] == false && G[no][v] == 1)
{
dfsConnected(v);
}
}
}
void dfsRoot(int root,int depth)
{
if (depth < maxDepth)
{
maxRoot.clear();
maxRoot.push_back(root);
maxDepth = depth;
}else if(depth == maxDepth)
{
maxRoot.push_back(root);
}
visit[root] = true;
for (int i = 1; i <= n; i++)
{
if (G[root][i] == 1 && visit[i] == false)
{
dfsRoot(i,depth+1);
}
}
}
int main()
{
fill(G[0], G[0]+MAXN*MAXN, 0);
scanf("%d", &n);
for (int i = 0; i < n-1; i++)
{
int u, v;
scanf("%d%d", &u, &v);
G[u][v] = G[v][u] = 1;
}
fill(visit, visit+MAXN, false);
for (int i = 1; i <= n; i++)
{
if(visit[i] == false)
{
dfsConnected(i);
k++;
}
}
if (k == 1)
{
maxRoot.clear();
fill(visit, visit+MAXN, false);
dfsRoot(1, 0);
for (int i = 0; i < maxRoot.size(); i++)
{
printf("%d\n", maxRoot[i]);
}
}else
{
printf("Error: %d components", k);
}
return 0;
}
补代码,23分:
1. 增加了在 k == 1 处 增加了循环
2. 一个测试点显示段错误,考虑是不是数组问题,改成vector变成了内存超限
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <set>
using namespace std;
const int MAXN = 10010;
bool visit[MAXN];
int G[MAXN][MAXN];
int n; // 结点个数
int k; //连同分量个数
vector<int> maxRoot;
int maxDepth = 0;
void dfsConnected(int no)
{
visit[no] = true;
for (int v = 1; v <= n; v++)
{
if (visit[v] == false && G[no][v] == 1)
{
dfsConnected(v);
}
}
}
void dfsRoot(int root,int depth)
{
if (depth > maxDepth)
{
maxRoot.clear();
maxRoot.push_back(root);
maxDepth = depth;
}else if(depth == maxDepth)
{
maxRoot.push_back(root);
}
visit[root] = true;
for (int i = 1; i <= n; i++)
{
if (G[root][i] == 1 && visit[i] == false)
{
dfsRoot(i,depth+1);
}
}
}
int main()
{
fill(G[0], G[0]+MAXN*MAXN, 0);
scanf("%d", &n);
for (int i = 0; i < n-1; i++)
{
int u, v;
scanf("%d%d", &u, &v);
G[u][v] = G[v][u] = 1;
}
fill(visit, visit+MAXN, false);
for (int i = 1; i <= n; i++)
{
if(visit[i] == false)
{
dfsConnected(i);
k++;
}
}
if (k == 1)
{
for(int i = 1; i <= n; i++)
{
fill(visit, visit+MAXN, false);
dfsRoot(i, 1);
}
set<int> s;
for (int i = 0; i < maxRoot.size(); i++)
{
s.insert(maxRoot[i]);
}
for (auto it = s.begin(); it != s.end(); it++)
{
printf("%d\n", *it);
}
}else if(k>=2)
{
printf("Error: %d components", k);
}
return 0;
}
//改用vector
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <set>
using namespace std;
const int MAXN = 10001;
bool visit[MAXN];
//开太大内存超限,改用vector
//int G[MAXN][MAXN];
vector<vector<int>> G;
int n; // 结点个数
int k; //连同分量个数
vector<int> maxRoot;
int maxDepth = 0;
void dfsConnected(int no)
{
visit[no] = true;
for (int v = 0; v < G[no].size(); v++)
{
if (visit[G[no][v]] == false)
{
dfsConnected(G[no][v]); //注意参数的变化
}
}
}
void dfsRoot(int root,int depth)
{
if (depth > maxDepth)
{
maxRoot.clear();
maxRoot.push_back(root);
maxDepth = depth;
}else if(depth == maxDepth)
{
maxRoot.push_back(root);
}
visit[root] = true;
for (int i = 0; i < G[root].size(); i++)
{
if (visit[G[root][i]] == false)
{
dfsRoot(G[root][i],depth+1);
}
}
}
int main()
{
//fill(G[0], G[0]+MAXN*MAXN, 0);
scanf("%d", &n);
G.resize(n+1);
for (int i = 0; i < n-1; i++)
{
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
fill(visit, visit+MAXN, false);
for (int i = 1; i <= n; i++)
{
if(visit[i] == false)
{
dfsConnected(i);
k++;
}
}
if (k == 1)
{
for(int i = 1; i <= n; i++)
{
fill(visit, visit+MAXN, false);
dfsRoot(i, 1);
}
set<int> s;
for (int i = 0; i < maxRoot.size(); i++)
{
s.insert(maxRoot[i]);
}
for (auto it = s.begin(); it != s.end(); it++)
{
printf("%d\n", *it);
}
}else if(k >= 2)
{
printf("Error: %d components", k);
}
return 0;
}
贴柳神的代码学习:
看了一下,思路应该是:
1. 在判断连通性的时候,找到最深的点,加入集合s
2. 从最深的点出发,看最远到达的点,加入s。
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
int n, maxheight = 0;
vector<vector<int>> v;
bool visit[10010];
set<int> s;
vector<int> temp;
void dfs(int node, int height) {
if(height > maxheight) {
temp.clear();
temp.push_back(node);
maxheight = height;
} else if(height == maxheight){
temp.push_back(node);
}
visit[node] = true;
for(int i = 0; i < v[node].size(); i++) {
if(visit[v[node][i]] == false)
dfs(v[node][i], height + 1);
}
}
int main() {
scanf("%d", &n);
v.resize(n + 1);
int a, b, cnt = 0, s1 = 0;
for(int i = 0; i < n - 1; i++) {
scanf("%d%d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
for(int i = 1; i <= n; i++) {
if(visit[i] == false) {
dfs(i, 1);
if(i == 1) {
if (temp.size() != 0) s1 = temp[0];
for(int j = 0; j < temp.size(); j++)
s.insert(temp[j]);
}
cnt++;
}
}
if(cnt >= 2) {
printf("Error: %d components", cnt);
} else {
temp.clear();
maxheight = 0;
fill(visit, visit + 10010, false);
dfs(s1, 1);
for(int i = 0; i < temp.size(); i++)
s.insert(temp[i]);
for(auto it = s.begin(); it != s.end(); it++)
printf("%d\n", *it);
}
return 0;
}