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条边的基础上,最小再添加多少条边可以让图变成强连通.思路:强连通分量缩点后找 ...
随机推荐
- 3D Modeling using GDI+
https://code.msdn.microsoft.com/3D-Modeling-using-GDI-b93937b9 Introduction Most of us use OpenGL/ D ...
- windows安装pip 和easy_install
先安装windows版的easy_install 下载 然后下载pip ,python setup.py install 安装好的 pip和easy_install通常在 python目录的 Scr ...
- LIS 最长递增子序列
一.最长公共子序列 经典的动态规划问题,大概的陈述如下: 给定两个序列a1,a2,a3,a4,a5,a6......和b1,b2,b3,b4,b5,b6.......,要求这样的序列使得c同时是这两个 ...
- iOS 提交代码出现提示弹出框显示 “A commit message is required to perform this operation.Enter a commit message and try again.“
需要你写一下你确认提交的信息,就是你这次提交上去修改了什么功能,简单描述一下
- win32 listbox
real-time refresh: the scrollbar will jump when the listbox refresh change color: how to change the ...
- Fzu oj2194星系碰撞(排序+并查集+路径压缩)
Problem 2194 星系碰撞 Accept: 14 Submit: 48Time Limit: 30000 mSec Memory Limit : 327680 KB Proble ...
- [转]有哪些值得关注的技术博客(Java篇)
有哪些值得关注的技术博客(Java篇) 大部分程序员在自学的道路上不知道走了多少坑,这个视频那个网站搞得自己晕头转向.对我个人来说我平常在学习的过程中喜欢看一些教程式的博客.这些博客的特点: 1. ...
- 移动端下拉刷新,iScroll.js用法(转载)
本文转载自: iScroll.js 用法参考 (share)
- Floyd算法核心代码证明
Flody 大家都知道这个最终模版: for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dis[i ...
- SQL的主键和外键约束(转)
SQL的主键和外键的作用: 外键取值规则:空值或参照的主键值. (1)插入非空值时,如果主键表中没有这个值,则不能插入. (2)更新时,不能改为主键表中没有的值. (3)删除主键表记录时,你可以在建外 ...