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. hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)

    http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...

  2. sha256

    SHA-512 (这些有时候也被称做 SHA-2). 简介 SHA 家族 SHA (Secure Hash Algorithm,译作安全散列算法) 是美国国家安全局 (NSA) 设计,美国国家标准与技 ...

  3. A Game of Thrones(18) - Catelyn

    “We will make King’s Landing within the hour.” Catelyn turned away from the rail and forced herself ...

  4. Hermes和开源Solr、ElasticSearch 不同

    Hermes和开源Solr.ElasticSearch不同          谈到Hermes的索引技术.相信非常多同学都会想到Solr.ElasticSearch.Solr.ElasticSearc ...

  5. Webserver管理系列:1、安装Windows Server 2008

    简单了解下server: 1U: 2U: 3U: 在安装Windows Server 2008之前我们先了解下Windows Server 2008: Windows Server 2008是微软一个 ...

  6. LB 负载均衡的层次结构(转)

    作为后端应用的开发者,我们经常开发.调试.测试完我们的应用并发布到生产环境,用户就可以直接访问到我们的应用了.但对于互联网应用,在你的应用和用户之间还隔着一层低调的或厚或薄的负载均衡层软件,它们不显山 ...

  7. 参数化测试--sheet表的应用

    自动化测试对录制和编辑好的测试步骤进行回放,这种是线性的自动化测试方式,其缺点是明显的,就是其测试覆盖面比较低.测试回放的只是录制时做出的界面操作,以及输入的测试数据,或者是脚本编辑时指定的界面操作和 ...

  8. IOS开发-通知与消息机制

    在多数移动应用中不论什么时候都仅仅能有一个应用程序处于活跃状态.假设其它应用此刻发生了一些用户感兴趣的那么通过通知机制就能够告诉用户此时发生的事情. iOS中通知机制又叫消息机制,其包含两类:一类是本 ...

  9. poj 3399 Product(数学)

    主题链接:http://poj.org/problem?id=3399 Product Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  10. WPF Media 简单的播放器

    <Window x:Class="PlayTest.MediaControl" xmlns="http://schemas.microsoft.com/winfx/ ...