HDU 5876:Sparse Graph(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=5876
Sparse Graph
Now you are given an undirected graph G of N nodes and M bidirectional edges of unit length. Consider the complement of G, i.e., H. For a given vertex S on H, you are required to compute the shortest distances from S to all N−1 other vertices.
题意:给出一个图,和一个起点,求在该图的补图中从起点到其他N-1个点的最短距离。如果不连通输出-1.
思路:比赛的时候觉得这题N那么大不会做。现在回过头发现貌似不难。用set将未走过的点放置进去,并在对点的邻边进行扩展的时候,把能走到的邻点删除掉(即补图中可以走到的邻点保留)。
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
using namespace std;
#define N 200010
#define INF 0x3f3f3f3f struct node
{
int v, nxt;
}edge[N];
int head[N];
int d[N], tot; void add(int u, int v)
{
edge[tot].v = v; edge[tot].nxt = head[u]; head[u] = tot++;
edge[tot].v = u; edge[tot].nxt = head[v]; head[v] = tot++;
} void bfs(int st, int n)
{
queue<int> que;
d[st] = ;
que.push(st);
set<int> s1, s2;
for(int i = ; i <= n; i++) {
if(i != st) s1.insert(i);
}
while(!que.empty()) {
int u = que.front(); que.pop();
for(int k = head[u]; ~k; k = edge[k].nxt) {
int v = edge[k].v;
if(!s1.count(v)) continue;
s1.erase(v); //补图中当前能走到的并且还未更新过的
s2.insert(v); //补图中走不到的还要扩展的
}
for(set<int>:: iterator it = s1.begin(); it != s1.end(); it++) {
d[*it] = d[u] + ;
que.push(*it);
}
s1.swap(s2); //还没更新过的
s2.clear();
}
} int main()
{
int t;
scanf("%d", &t);
while(t--) {
memset(head, -, sizeof(head));
memset(d, INF, sizeof(INF));
int n, m;
scanf("%d%d", &n, &m);
for(int i = ; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
}
int st;
scanf("%d", &st);
bfs(st, n);
bool f = ;
for(int i = ; i <= n; i++) {
if(i != st) {
if(f) printf(" ");
f = ;
if(d[i] == INF) printf("-1");
else printf("%d", d[i]);
}
}
puts("");
} return ;
}
HDU 5876:Sparse Graph(BFS)的更多相关文章
- HDU - 5876 :Sparse Graph (完全图的补图的最短路 -BFS&set)
In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinc ...
- HDU 1728:逃离迷宫(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有 ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
- HDU 4635:Strongly connected(强连通)
http://acm.hdu.edu.cn/showproblem.php?pid=4635 题意:给出n个点和m条边,问最多能添加几条边使得图不是一个强连通图.如果一开始强连通就-1.思路:把图分成 ...
- 九度OJ 1335:闯迷宫 (BFS)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1782 解决:483 题目描述: sun所在学校每年都要举行电脑节,今年电脑节有一个新的趣味比赛项目叫做闯迷宫. sun的室友在帮电脑节设计 ...
- 题解报告:hdu 2717 Catch That Cow(bfs)
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
- HDU 1890:Robotic Sort(Splay)
http://acm.hdu.edu.cn/showproblem.php?pid=1890 题意:有一个无序序列,经过不断地翻转,使得最后的序列是一个升序的序列,而且如果相同数字要使在原本序列靠前的 ...
- HDU 2767:Proving Equivalences(强连通)
http://acm.hdu.edu.cn/showproblem.php?pid=2767 题意:给出n个点m条边,问在m条边的基础上,最小再添加多少条边可以让图变成强连通.思路:强连通分量缩点后找 ...
随机推荐
- selenium 回放时遇到的问题
回放时,系统报”Window does not exist” 录制时,存在弹出页面 回放时,系统会报以下的错误: 问题的根本原因: window 窗口没有id和name属性,系统会自动生成name属性 ...
- ISymbol
public void Draw (IGeometry Geometry); public void QueryBoundary ( int hDC, ITransformation ...
- 最流行的PHP 代码规范
“PHP是最好的编程语言” ;-) 那么PHPer习惯使用什么样的代码规范呢?sideeffect.kr通过分析GitHub上托管的开源代码,得出了一些有趣的结果,让我们一起来看看吧. 缩进 空格(7 ...
- The REST Objection
HTTP 1.1 Standard http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
- MySQL复制-设置延迟复制
mysql> stop slave ; Query OK, 0 rows affected (0.00 sec) mysql> change master to master_delay= ...
- Java-NIO-Selector
扩展阅读: Java NIO类库Selector机制解析(上) Java NIO类库Selector机制解析(下) Java NIO的选择器 三个重要的类: 1,Selector 选择器,完成主要的选 ...
- python之django 资料
里边有不少比较好的文章. http://www.cnblogs.com/luxiaojun/p/5795070.html
- 转:Python获取随机数(中文)
下面介绍下random中常见的函数. 前提:需要导入random模块 >>>import random 1.random.random random.random() 用于生成一个0 ...
- SQL Server面试题
前几天在博客园上看到一道SQL面试题,sc是表名.老师拿来与同学分享,让大家试做,要求是:查出每科成绩都>=80分的名字,看能写出几种方法.没有主外键,没有关联,脑袋一下子就蒙了.经老师讲解指导 ...
- flot_js_$用法解释
$用法解释 $在JS中本身只是一个符号而异,在JS里什么也不是.但在JS应用库JQUERY的作者将之做为一个自定义函数名了,这个函数是获取指定网页元素的函数,使用非常之频繁,所以好多新手不知道,还以为 ...