HDU 5876 Sparse Graph
Sparse Graph
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1520 Accepted Submission(s): 537
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.
#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <queue>
using namespace std; const int INF = 0x3f3f3f3f;
const int MAXN = 2e5+5;
vector<int> e[MAXN];
int dis[MAXN];
int n, m, s; void bfs()
{
set<int> s1; //保存还未被访问过的结点
set<int> s2; //保存与节点u相连的结点
set<int>::iterator it;
for(int i = 1; i <= n; ++i){ //s1初始化为除结点s外的所有结点
if(i != s)
s1.insert(i);
}
queue<int> q;
q.push(s);
dis[s] = 0;
while(!q.empty()){
int u = q.front();
q.pop();
int len = e[u].size();
for(int i = 0; i < len; ++i){
int v = e[u][i];
if(s1.find(v) == s1.end())
continue;
s1.erase(v); //把与结点u之间有边的点从s1排除
s2.insert(v); //并加入到s2
}
for(it = s1.begin(); it != s1.end(); ++it){
q.push(*it);
dis[*it] = dis[u]+1;
}
s1.swap(s2); //s1中的结点已经全部访问,s2中的结点尚未访问,二者交换
s2.clear(); //清空
}
} void solve()
{
memset(dis, INF, sizeof(dis));
for(int i = 1; i <= n; ++i)
e[i].clear();
int u, v;
while(m--){
scanf("%d%d", &u, &v);
e[u].push_back(v);
e[v].push_back(u);
}
scanf("%d", &s);
bfs();
for(int i = 1; i <= n; ++i){
if(i == s)
continue;
if(dis[i] >= INF)
dis[i] = -1;
printf("%d", dis[i]);
if(i != n)
printf(" ");
}
printf("\n");
} int main()
{
int t;
scanf("%d", &t);
while(t--){
scanf("%d%d", &n, &m);
solve();
}
return 0;
}
HDU 5876 Sparse Graph的更多相关文章
- HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- HDU 5876 Sparse Graph BFS 最短路
Sparse Graph Problem Description In graph theory, the complement of a graph G is a graph H on the ...
- hdu 5876 Sparse Graph 无权图bfs求最短路
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) P ...
- HDU 5876 Sparse Graph(补图中求最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 在补图中求s到其余各个点的最短路. 思路:因为这道题目每条边的距离都是1,所以可以直接用bfs来做 ...
- HDU 5876 Sparse Graph(补图上BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 有一个 n 个点无向图,再给你 m 对顶点, 代表着这 m 对顶点之间没有边, 除此之外 ...
- HDU 5876 Sparse Graph BFS+set删点
Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices s ...
- hdu 5876 Sparse Graph icpc大连站网络赛 1009 补图最短路
BFS+链表 代码改自某博客 #include<stdio.h> #include<iostream> #include<algorithm> #include&l ...
- HDU 5867 Sparse Graph (2016年大连网络赛 I bfs+补图)
题意:给你n个点m条边形成一个无向图,问你求出给定点在此图的补图上到每个点距离的最小值,每条边距离为1 补图:完全图减去原图 完全图:每两个点都相连的图 其实就是一个有技巧的bfs,我们可以看到虽然点 ...
- HDU 5876 关于补图的bfs
1.HDU 5876 Sparse Graph 2.总结:好题,把STL都过了一遍 题意:n个点组成的完全图,删去m条边,求点s到其余n-1个点的最短距离. 思路:把点分为两个集合,A为所有没有到达 ...
随机推荐
- jQuery经典面试题及答案精选[转载]
问题:jQuery的美元符号$有什么作用? 回答:其实美元符号$只是”jQuery”的别名,它是jQuery的选择器,如下代码: $(document).ready(function(){ }); 当 ...
- Oracle日期函数
Oracle日期函数用于对Oracle数据库中的日期及时间进行处理. (1)ADD_MONTHS Oracle日期函数返回一个具有与所提供日期相差月份的日期,函数中给出了未来或以前的月份数.语法如下: ...
- Oracle的学习一:安装与卸载、sql *plus常用命令、Oracle用户管理
1.为什么学习oracle? 性能优越: 小型数据库 中型数据库 大型数据库 acess.foxbase mysql.sql server.informix sybase.oracle.db2 复杂量 ...
- linux 查看局域网内ip
$ sudo apt-get install nmap $ nmap -sP 192.168.1.1/24 windows 下直接arp -a就能看到.
- *[hackerrank]Consecutive Subsequences
https://www.hackerrank.com/contests/w6/challenges/consecutive-subsequences 求数组中被k整除的子段和有几个.这个要利用sum[ ...
- 【memcache缓存专题(3)】PHP-memcache扩展的安装以及使用
安装PHP-memcache扩展和安装其他PHP扩展的步骤是一样的. 安装 step 1:搜索下载扩展 http://pecl.php.net/package/memcache step 2: gzi ...
- jvm垃圾回收的时间问题
1.系统崩溃前的一些现象: 每次垃圾回收的时间越来越长,由之前的10ms延长到50ms左右,FullGC的时间也有之前的0.5s延长到4.5s FullGC的次数越来越多,最频繁时隔不到1分钟就进行一 ...
- REACTOS(193)与汇编编译器(69)的高人
REACTOS(193)与汇编编译器(69)的高人http://blog.csdn.net/caimouse ReactOS编译成VS工程1: 首先从https://www.reactos.org/w ...
- OpenCV源码阅读(2)---matx.h---函数的内联实现
外部矩阵计算函数 namespace internal { template<typename _Tp, int m> struct Matx_DetOp { double operato ...
- 创建高安全性PHP网站的几个实用要点
大家都知道PHP已经是当前最流行的Web应用编程语言了.但是也与其他脚本语言一样,PHP也有几个很危险的安全漏洞.所以在这篇教学文章中,我们将大致看看几个实用的技巧来让你避免一些常见的PHP安全问题. ...