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


题意:
给出n个点。m条边,求每一个点到其它点的距离,取最大的,然后在这全部最大的距离中选一个最小的值,最后输出这个值下有哪些点符合条件


思路:
n次最短路找出全部答案

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <list>
#include <algorithm>
#include <climits>
using namespace std; #define lson 2*i
#define rson 2*i+1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 200005
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)
const int mod = 1e9+7;
const int L = 10005;
struct Edges
{
int x,y,w,next;
} e[L<<2]; int head[L],n,m;
int dis[L];
int vis[L];
int cnt[L],hash[L],ss[L];
int s[L];
void init()
{
memset(e,-1,sizeof(e));
memset(head,-1,sizeof(head));
}
void AddEdge(int x,int y,int w,int k)
{
e[k].x = x,e[k].y = y,e[k].w = w,e[k].next = head[x],head[x] = k;
}
int relax(int u,int v,int c)
{
if(dis[v]>dis[u]+c)
{
dis[v] = dis[u]+c;
return 1;
}
return 0;
} int SPFA(int src)
{
int i;
memset(vis,0,sizeof(vis));
for(int i = 0; i<=n; i++)
dis[i] = INF;
dis[src] = 0;
queue<int> Q;
Q.push(src);
vis[src] = 1;
while(!Q.empty())
{
int u,v;
u = Q.front();
Q.pop();
vis[u] = 0;
for(i = head[u]; i!=-1; i=e[i].next)
{
v = e[i].y;
if(relax(u,v,e[i].w)==1 && !vis[v])
{
Q.push(v);
vis[v] = 1;
}
}
}
int maxn = -1;
for(i = 1; i<=n; i++)
maxn = max(maxn,dis[i]);
return maxn;
} int ans[L],tot,p[N];
int main()
{
int t,u,v,i,j,k;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
for(i = 0; i<2*m; i+=2)
{
scanf("%d%d",&u,&v);
AddEdge(u,v,1,i);
AddEdge(v,u,1,i+1);
}
int minn = INF;
for(i = 1; i<=n; i++)
{
p[i] = SPFA(i);
minn = min(p[i],minn);
}
tot = 0;
for(i = 1; i<=n; i++)
{
if(p[i]==minn)
ans[tot++] = i;
}
printf("%d\n",tot);
for(i = 0; i<tot; i++)
{
if(i)
printf(" ");
printf("%d",ans[i]);
}
printf("\n");
} return 0;
}

CSU1659: Graph Center(最短路)的更多相关文章

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

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

  2. CSU 1659: Graph Center(SPFA)

    1659: Graph Center Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 63  Solved: 25 [id=1659"> ...

  3. HDU 5876 Sparse Graph BFS 最短路

    Sparse Graph Problem Description   In graph theory, the complement of a graph G is a graph H on the ...

  4. Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))

    B. Complete The Graph time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  5. HDU 4725 The Shortest Path in Nya Graph (最短路)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  6. HDU4725:The Shortest Path in Nya Graph(最短路)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. hdu 4725 The Shortest Path in Nya Graph (最短路+建图)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  8. HDU 4725 The Shortest Path in Nya Graph(最短路拆点)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:n个点,某个点属于某一层.共有n层.第i层的点到第i+1层的点和到第i-1层的点的代价均是 ...

  9. hdu4725 The Shortest Path in Nya Graph【最短路+建图】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297574.html      ---by 墨染之樱花 题目链接:http://acm.hdu ...

随机推荐

  1. smarty半小时快速上手教程(转)

    来源于:http://www.chinaz.com/program/2010/0224/107006.shtml 一:smarty的程序设计部分: 在smarty的模板设计部分我简单的把smarty在 ...

  2. 【译】addEventListener 第二个参数

    这是原文链接:Our Backwards DOM Event Libraries 浏览器APIs 先简单回顾一下各个浏览器提供了哪些绑定事件的接口. IE浏览器提供了element.attachEve ...

  3. POJ 3254 状压DP

    题目大意: 一个农民有一片n行m列 的农场   n和m 范围[1,12]  对于每一块土地 ,1代表可以种地,0代表不能种. 因为农夫要种草喂牛,牛吃草不能挨着,所以农夫种菜的每一块都不能有公共边. ...

  4. HTML5 canvas中的转换方法

    转换方法 scale(scalewidth,scaleheight)                缩放当前绘图至更大或更小 scalewidth         缩放当前绘图的宽度 (1=100%, ...

  5. java虚拟机内存分析

    1.大致来说java虚拟机分为:堆  栈 栈在数据结构就是那个先进后出的栈.堆...这名字我一听就觉得大..毕竟我们形容东西多又没什么大多的组织的时候就是一堆一堆的....(原谅我发散性的思维,我是妹 ...

  6. 【PHP】新浪、淘宝的地区 API调用

    /推荐使用新浪的: $ch = curl_init(); $url = "http://ip.dpool.sina.com.cn/iplookup/iplookup.php?format=j ...

  7. phpcms v9二次开发之数据模型类

    系统模型类:model.class.php数据模型类的位置:/phpcms/libs/classes phpcms v9二次开发中,我们要经常需要对模块的数据表进行查询.添加.修改和删除数据等操作,所 ...

  8. 启动任务StartTask() 发送完消息队列 自己删除,接收方一直显示数据 用OSQFlush(Str_Q); //清空消息队列 下面纠结接收不到了 哈哈

    在建立工程的时候,启动任务StartTask()  启动了任务MyTask(),也建立了消息队列,然后发送消息队列,发送完自己删除了自己,在接收方一直能接受到数据???为何??? 因为我们的消息队列未 ...

  9. 无线通信技术协议-Zigbee 3.0

    物联网的无线通信技术有:短距离的无线局域网通信技术和长距离的无线广域网通信技术. 短距离局域网通信技术有Zigbee.Wi-Fi.Bluetooth.Z-wave.6LoWPAN等. 长距离广域网通信 ...

  10. 分布式文件系统 FastDFS Ceph

    分布式文件系统 FastDFS Cephhttp://www.oschina.net/p/fastdfshttp://www.oschina.net/p/ceph FastDFS 的 Go 客户端 f ...