SGU 134 Centroid(树的重心)
2016-08-01 15:33
274 查看
思路:找树的重心,就是删除那个节点之后使得剩下子树的节点数最大的最小的点
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 16000+5;
#define inf 1e9
vector<int>e[maxn],ans;
int siz[maxn];
int dp[maxn];
int n,minx,res;
void dfs(int u,int fa)
{
siz[u]=1;
for(int i = 0;i<e[u].size();i++)
{
int v = e[u][i];
if(v==fa)
continue;
dfs(v,u);
siz[u]+=siz[v];
res = max(res,siz[v]);
}
dp[u]=max(res,n-siz[u]);
minx = min(dp[u],minx);
}
int main()
{
scanf("%d",&n);
{
for(int i = 0;i<=n;i++)
e[i].clear();
minx = inf;
res = 0;
for(int i = 1;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
}
dfs(1,-1);
//for(int i = 1;i<=n;i++)
// printf("%d\n",dp[i]);
for(int i = 1;i<=n;i++)
if(dp[i]==minx)
ans.push_back(i);
printf("%d %d\n",minx,ans.size());
for(int i = 0;i<ans.size();i++)
printf("%d ",ans[i]);
//printf("%d\n",ans[ans.size()-1]);
}
}
Description
You are given an undirected connected graph, with N vertices and N-1 edges (a tree). You must find the centroid(s) of the tree.
In order to define the centroid, some integer value will be assosciated to every vertex. Let's consider the vertex k. If we remove the vertex k from the tree (along with its adjacent edges), the remaining graph will have only N-1 vertices
and may be composed of more than one connected components. Each of these components is (obviously) a tree. The value associated to vertex k is the largest number of vertices contained by some connected component in the remaining graph, after
the removal of vertex k. All the vertices for which the associated value is minimum are considered centroids.
Input
The first line of the input contains the integer number N (1<=N<=16 000). The next N-1 lines will contain two integers, a and b, separated by blanks, meaning
that there exists an edge between vertex a and vertex b.
Output
You should print two lines. The first line should contain the minimum value associated to the centroid(s) and the number of centroids. The second line should contain the list of vertices which are centroids, sorted in ascending order.
Sample Input
Sample Output
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 16000+5;
#define inf 1e9
vector<int>e[maxn],ans;
int siz[maxn];
int dp[maxn];
int n,minx,res;
void dfs(int u,int fa)
{
siz[u]=1;
for(int i = 0;i<e[u].size();i++)
{
int v = e[u][i];
if(v==fa)
continue;
dfs(v,u);
siz[u]+=siz[v];
res = max(res,siz[v]);
}
dp[u]=max(res,n-siz[u]);
minx = min(dp[u],minx);
}
int main()
{
scanf("%d",&n);
{
for(int i = 0;i<=n;i++)
e[i].clear();
minx = inf;
res = 0;
for(int i = 1;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
}
dfs(1,-1);
//for(int i = 1;i<=n;i++)
// printf("%d\n",dp[i]);
for(int i = 1;i<=n;i++)
if(dp[i]==minx)
ans.push_back(i);
printf("%d %d\n",minx,ans.size());
for(int i = 0;i<ans.size();i++)
printf("%d ",ans[i]);
//printf("%d\n",ans[ans.size()-1]);
}
}
Description
You are given an undirected connected graph, with N vertices and N-1 edges (a tree). You must find the centroid(s) of the tree.
In order to define the centroid, some integer value will be assosciated to every vertex. Let's consider the vertex k. If we remove the vertex k from the tree (along with its adjacent edges), the remaining graph will have only N-1 vertices
and may be composed of more than one connected components. Each of these components is (obviously) a tree. The value associated to vertex k is the largest number of vertices contained by some connected component in the remaining graph, after
the removal of vertex k. All the vertices for which the associated value is minimum are considered centroids.
Input
The first line of the input contains the integer number N (1<=N<=16 000). The next N-1 lines will contain two integers, a and b, separated by blanks, meaning
that there exists an edge between vertex a and vertex b.
Output
You should print two lines. The first line should contain the minimum value associated to the centroid(s) and the number of centroids. The second line should contain the list of vertices which are centroids, sorted in ascending order.
Sample Input
7 1 2 2 3 2 4 1 5 5 6 6 7
Sample Output
3 1 1
相关文章推荐
- SGU 134 Centroid【树的重心】
- SGU 134. Centroid(树的重心)
- SGU 134 Centroid (树的重心)
- sgu 134 Centroid 树的重心
- SGU 134 Centroid 找树的重心 水题
- SGU - 134 Centroid 无根树转有根树 + 树形DP
- SGU134 Centroid 树形DP,dfs建树
- sgu 134 Centroid
- SGU134 Centroid
- SGU 134 Centroid (树形dp 求树的中心)
- SGU 134.Centroid(图心)
- SGU 134 Centroid
- 树形DP求树的重心 --SGU 134
- 树形DP求树的重心 --SGU 134
- Centroid - SGU 134(树的搜索)
- SGU 134 Centroid(树形dp)
- SGU 134(树的重心)
- SGU134 Centroid
- SGU 134 Centroid(树形DP)
- SGU 134 树的重心