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为所有没有到达 ...
随机推荐
- 暑假集训单切赛第一场 CF 191A Dynasty Puzzles
题意不说了,看原题吧,思路见代码: #include <iostream> #include <stdio.h> #include <string.h> #incl ...
- REST_FRAMEWORK加深记忆-三种CLASS VIEW的进化史
一层一层的封装,又能到底层,就会有全局感啦... from rest_framework import status from rest_framework.response import Respo ...
- DIV+CSS列表式布局(同意图片的应用)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- cojs 安科赛斯特 题解报告
QAQ 从IOI搬了一道题目过来 官方题解貌似理论上没有我的做法优,我交到BZOJ上也跑的飞快 结果自己造了个数据把自己卡成了4s多,真是忧桑的故事 不过貌似原题是交互题,并不能离线 说说我的做法吧 ...
- Protege汉字不能正常显示问题
在Protege5.0中有下面的问题: 点击uses,汉字不能正常显示. 在qq群里面问到,可以通过设置label的方式,在对类,子类命名成英语的,点击annotations,在label中设置汉字名 ...
- Sina App Engine(SAE)入门教程(1)
此教程只针对刚接触SAE的小白用户,资深码农.高手请绕道.首先还是一个经典的实例,hello sae. 创建应用 在注册完账号之后,需要到 http://sae.sina.com.cn/?m=myap ...
- Nginx安装部署
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev ...
- js中鼠标滚轮事件详解
js中鼠标滚轮事件详解 (以下内容部分内容参考了http://adomas.org/javascript-mouse-wheel/ ) 之前js 仿Photoshop鼠标滚轮控制输入框取值中已使用 ...
- QTP数据驱动之读取Excel数据
这个代码的原理是把Excel的数据当做数据库里的数据一样处理,可以对Excel用select来检索需要的数据,然后把数据以键值对的形式保存到oDict里,方便在用例层来调用 Class oDataDi ...
- java web 下实现文件下载
来自:http://blog.csdn.net/longshengguoji/article/details/39433307 需求:实现一个具有文件下载功能的网页,主要下载压缩包和图片 两种实现方法 ...