CSU1659: Graph Center(最短路)
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 <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(最短路)的更多相关文章
- csu - 1659 Graph Center(最短路)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1659 题意是找一个图的中心,图的中心定义是某一个点到其他点的最大距离最小,如果有多个排序输出. 注 ...
- CSU 1659: Graph Center(SPFA)
1659: Graph Center Time Limit: 1 Sec Memory Limit: 128 MB Submit: 63 Solved: 25 [id=1659"> ...
- HDU 5876 Sparse Graph BFS 最短路
Sparse Graph Problem Description In graph theory, the complement of a graph G is a graph H on the ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- HDU 4725 The Shortest Path in Nya Graph(最短路拆点)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:n个点,某个点属于某一层.共有n层.第i层的点到第i+1层的点和到第i-1层的点的代价均是 ...
- hdu4725 The Shortest Path in Nya Graph【最短路+建图】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297574.html ---by 墨染之樱花 题目链接:http://acm.hdu ...
随机推荐
- img图片元素下多余空白解决方案
在进行页面的DIV+CSS排版时,遇到IE6(当然有时Firefox下也会偶遇)浏览器中的图片元素img下出现多余空白的问题绝对是常见的对於 该问题的解决方法也是「见机行事」,根据原因的不同要用不同的 ...
- 简单水池&&迷宫问题
#include <iostream> #include <stdio.h> #include <cstring> using namespace std; int ...
- 数据库连接超时和go away、如何检测数据库的最大连接数
搜索连接bi库超时 数据库连接超时 go away go away和连接超时之间的关系是什么? 写一个例子测试一下. 如何检测数据库的最大连接数
- activiti总结2
根据流程号查询失败原因. activiti重试机制.齿轮节点.邮件节点.任务节点.ACT_HI_ACTINST历史表.ACT_RU_EXECUTION运行表.看图. 在Eclipse里面自己写个测试方 ...
- 一条insert语句批量插入多条记录
一条insert语句批量插入多条记录 常见的insert语句,向数据库中,一条语句只能插入一条数据: insert into persons (id_p, lastname , firstName, ...
- 数据库的事务处理必须满足ACID原则,ACID分别是指什么
http://blog.csdn.net/dingxingmei/article/details/39270375
- MFC 堆栈溢出 test dword ptr [eax],eax ; probe page.
今天调试程序的时候,发现一个奇怪的问题,之前调试都没问题的,今早加了一点东西,就出现错误,跳到调试位置,如下4行红色部分 ; Find next lower page and probe cs20: ...
- PHP自定义弹出消息类,用于弹出提示信息并返回
一个用PHP自写的弹出消息类,用于在程序出错时弹出提示,,弹出警告框,或在程序运行到某阶段的快捷提示,需用时只需传入参数即可,函数并不复杂,但觉得挺实用.具体代码: function Alert($a ...
- mysql时间int日期转换
select from_unixtime(1350437720);select unix_timestamp(now());插入用 unix_timestamp(date)查询用from_unixti ...
- EDIT编辑框
编辑框 编辑框的主要作用是让用户输入文本,例如要求用户在编辑框中输入密码的文本. .基础知识 编辑框里的文本可以是单行,也可以是多行,后者的风格取值为 ES_MULTILINE.一般对于多行文本编辑框 ...