1659: Graph Center

Time Limit: 1 Sec  Memory Limit:
128 MB

Submit: 63  Solved: 25

[

id=1659">Submit][Status][Web
Board
]

Description

The center of a graph is the set of all vertices of minimum eccentricity, that is, the set of all vertices A where the greatest distance d(A,B) to other vertices B is minimal. Equivalently, it is the set of vertices with eccentricity equal to the graph's
radius. Thus vertices in the center (central points) minimize the maximal distance from other points in the graph.

                                                                                                             ------wikipedia

Now you are given a graph, tell me the vertices which are the graph center.

Input

There are multiple test cases.

The first line will contain a positive integer T (T ≤ 300) meaning the number of test cases.

For each test case, the first line contains the number of vertices N (3 ≤ N ≤ 100) and the number of edges M (N - 1 ≤ N * (N - 1) / 2). Each of the following N lines contains two vertices x (1 ≤ x ≤ N) and y (1 ≤ y ≤ N), meaning there is an edge between x and
y.

Output

The first line show contain the number of vertices which are the graph center. Then the next line should list them by increasing order, and every two adjacent number should be separated by a single space.

Sample Input

2
4 3
1 3
1 2
2 4
5 5
1 4
1 3
2 4
2 3
4 5

Sample Output

2
1 2
3
1 2 4

HINT

Source

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std; const int MAXN = 105;
const int MAXM = 100005;
const int INF = 1<<30;
struct EDG{
int to,next;
}edg[MAXM];
int eid,head[MAXN]; void init(){
eid=0;
memset(head,-1,sizeof(head));
}
void addEdg(int u,int v){
edg[eid].to=v; edg[eid].next=head[u]; head[u]=eid++;
edg[eid].to=u; edg[eid].next=head[v]; head[v]=eid++;
}
int spfa(int s,int n){
queue<int>q;
bool inq[MAXN]={0};
int d[MAXN];
for(int i=1; i<=n; i++)
d[i]=INF;
d[s]=0;
q.push(s);
while(!q.empty()){
int u=q.front(); q.pop();
inq[s]=0;
for(int i=head[u]; i!=-1; i=edg[i].next){
int v=edg[i].to;
if(d[v]>d[u]+1){
d[v]=d[u]+1;
if(!inq[v])
q.push(v),inq[v]=1;
}
}
}
int maxt=0;
for(int i=1; i<=n; i++)
if(maxt<d[i])
maxt=d[i];
return maxt;
}
int main(){
int T,n,m,u,v,d[MAXN],id[MAXN];
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
init();
while(m--){
scanf("%d%d",&u,&v);
addEdg(u,v);
}
int mint=INF;
for(int i=1;i<=n;i++){
d[i]=spfa(i,n);
if(d[i]<mint)
mint=d[i];
}
int k=0;
for(int i=1; i<=n; i++)
if(mint==d[i]){
id[k++]=i;
}
printf("%d\n",k);
for(int i=0; i<k; i++){
printf("%d",id[i]);
if(i!=k-1)
printf(" ");
else
printf("\n");
}
}
} /**************************************************************
Problem: 1659
User: aking2015
Language: C++
Result: Accepted
Time:256 ms
Memory:1848 kb
****************************************************************/

CSU 1659: Graph Center(SPFA)的更多相关文章

  1. csu - 1659 Graph Center(最短路)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1659 题意是找一个图的中心,图的中心定义是某一个点到其他点的最大距离最小,如果有多个排序输出. 注 ...

  2. 模板C++ 03图论算法 1最短路之单源最短路(SPFA)

    3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...

  3. 【转】使用Boost Graph library(二)

    原文转自:http://shanzhizi.blog.51cto.com/5066308/942972 让我们从一个新的图的开始,定义一些属性,然后加入一些带属性的顶点和边.我们将给出所有的代码,这样 ...

  4. 最短路(SPFA)

    SPFA是Bellman-Ford算法的一种队列实现,减少了不必要的冗余计算. 主要思想是: 初始时将起点加入队列.每次从队列中取出一个元素,并对所有与它相邻的点进行修改,若某个相邻的点修改成功,则将 ...

  5. Bellman-Ford算法及其队列优化(SPFA)

    一.算法概述 Bellman-Ford算法解决的是一般情况下的单源最短路径问题.所谓单源最短路径问题:给定一个图G=(V,E),我们希望找到从给定源结点s属于V到每个结点v属于V的最短路径.单源最短路 ...

  6. Funny Car Racing CSU - 1333 (spfa)

    There is a funny car racing in a city with n junctions and m directed roads. The funny part is: each ...

  7. sgu 240 Runaway (spfa)

    题意:N点M边的无向图,边上有线性不下降的温度,给固定入口S,有E个出口.逃出去,使最大承受温度最小.输出该温度,若该温度超过H,输出-1. 羞涩的题意 显然N*H的复杂度dp[n][h]表示到达n最 ...

  8. codevs 1021 玛丽卡(spfa)

    题目描述 Description 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们 ...

  9. 【POJ】1062 昂贵的聘礼(spfa)

    http://poj.org/problem?id=1062 此题一开始果断想到暴力.. 但是n<=100果断不行. 一看题解,噗!最短路... 构图很巧妙. 每一个物品对应的所需物品相当于一个 ...

随机推荐

  1. 在navigationItem中添加搜索栏

    给navigationItem中添加个搜索栏,效果和大部分程序一样.代码如下: UISearchBar *searchBar = [[UISearchBaralloc] initWithFrame:C ...

  2. MATLAB——scatter的简单应用

    scatter可用于描绘散点图. 1.scatter(X,Y) X和Y是数据向量,以X中数据为横坐标,以Y中数据位纵坐标描绘散点图,点的形状默认使用圈. 样例: X = [1:10];  Y = X ...

  3. poj1459(最大流)

    传送门:Power Network 题意:在一个网络图中有n个点,其中有np个发电站,nc个用户,m条电线;每个发电站,用户,和电线都对应有一个最大的电流;让求出该网络中最大的电流. 分析:最大流裸题 ...

  4. 读取USB HDD(USB移动硬盘信息)序列号的代码

    读取USB HDD(USB移动硬盘)序列号的代码,型号及分位. 使用Visual Studio 2010编译成功. 代码使用了CrystalDiskInfo中的代码smartata.c中相关代码: 例 ...

  5. mongodb在PHP下的应用学习笔记

    1.连接 mongodb默认端口是:27017,因此我们连接mongodb:$mongodb = new Mongo('localhost') 或者指定IP与端口 $mongodb = new Mon ...

  6. WPF案例 (六) 动态切换UI布局

    原文:WPF案例 (六) 动态切换UI布局 这个Wpf示例对同一个界面支持以ListView或者CardView的布局方式呈现界面,使用控件ItemsControl绑定数据源,使用DataTempla ...

  7. hdu3804(树链剖分)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3804 题意:给定一棵n个结点的树及边权,回答m个询问(x,y)满足以下条件的边权: 1)该边在结点1~ ...

  8. hdu2036 (计算多边形的面积)

    Input 输入数据包含多个测试实例,每个测试实例占一行,每行的开始是一个整数n(3<=n<=100),它表示多边形的边数(当然也是顶点数),然后是按照逆时针顺序给出的n个顶点的坐标(x1 ...

  9. openfire插件开发的几点说明

    1.关于插件的目录结构 这个网上的资料很多,但是我觉得要看懂也不太容易,我这里上一个包括了jsp和servlet的图,希望大家能马上看懂: ME的Navigator视图下的截图: build path ...

  10. 在Xshell中上传下载文件到本地(linux中从多次ssh登录的dbserver里面的文件夹)

    在Xshell中上传下载文件到本地(linux中从多次ssh登录的dbserver里面的文件夹) 1 列出所有需要copy的sh文件 -bash-4.1$ ll /mysqllog/osw/*.sh ...