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 distinct vertices of H are adjacent if and only if they are not adjacent in G.
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
InputThere are multiple test cases. The first line of input is an integer T(1≤T<35) denoting the number of test cases. For each test case, the first line contains two integers N(2≤N≤200000) and M(0≤M≤20000). The following M lines each contains two distinct integers u,v(1≤u,v≤N) denoting an edge. And S (1≤S≤N) is given on the last line.OutputFor each of T test cases, print a single line consisting of N−1 space separated integers, denoting shortest distances of the remaining N−1 vertices from S (if a vertex cannot be reached from S, output ``-1" (without quotes) instead) in ascending order of vertex number.Sample Input
1
2 0
1
Sample Output
1
题意:给定一个无向图,求它的补图中S到每一点的最短路。
思路:我们BFS,长度从0,1,2...慢慢试探,由于是补图,显然试探的次数不会太多就可以弄完,所以我们可以暴力一点,一次BFS,我们取出队首u,其最短距离是dis[u],那么它没有被访问的点中(满足dis==-1),不与u相邻的点的最短距离是dis[u]+1,将其加入队首; 我们可以用set来表示未被访问的点。
由于S1.swap(S2)是两个set的指针交换,所以复杂度是O(1),比较快的。主要复杂度再S.clear那里,clear的复杂度是元素个数,由于是补图,所以可以假设放进去之后几个回合内就删完了,复杂度不会太高。
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn],To[maxn],dis[maxn],num[maxn];
int q[maxn],tot,cnt,S,N;
void add(int u,int v){
Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v;
}
void BFS()
{
dis[S]=;
set<int>S1,S2;
set<int>::iterator it;
queue<int>q;
q.push(S);
rep(i,,N) if(i!=S) S1.insert(i);
while(!q.empty()){
int u=q.front(); q.pop();
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i]; if(S1.find(v)==S1.end()) continue;
S1.erase(v); S2.insert(v);
}
for(it=S1.begin();it!=S1.end();it++) dis[*it]=dis[u]+,q.push(*it);
S1.swap(S2); S2.clear();
}
}
int main()
{
int T,M,u,v;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M);
cnt=; rep(i,,N) Laxt[i]=,dis[i]=-;
rep(i,,M){
scanf("%d%d",&u,&v);
add(u,v); add(v,u);
}
scanf("%d",&S);
BFS();
rep(i,,N){
if(i!=S){
if(i!=N) printf("%d ",dis[i]);
else printf("%d\n",dis[i]);
}
}
}
return ;
}
HDU - 5876 :Sparse Graph (完全图的补图的最短路 -BFS&set)的更多相关文章
- 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
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- 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 icpc大连站网络赛 1009 补图最短路
BFS+链表 代码改自某博客 #include<stdio.h> #include<iostream> #include<algorithm> #include&l ...
- 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 5867 Sparse Graph (2016年大连网络赛 I bfs+补图)
题意:给你n个点m条边形成一个无向图,问你求出给定点在此图的补图上到每个点距离的最小值,每条边距离为1 补图:完全图减去原图 完全图:每两个点都相连的图 其实就是一个有技巧的bfs,我们可以看到虽然点 ...
随机推荐
- 【网络结构】VGG-Net论文解析
目录 0. 论文链接 1. 概述 2. 网络结构 2.1 卷积核 2.2 池化核 2.3 全连接层 3. 训练 4. 测试 5. 其他 6.参考链接 @ 0. 论文链接 论文链接 1. 概述 VG ...
- Lucene 更新、删除、分页操作以及IndexWriter优化
更新操作如下: 注意:通过lukeall-1.0.0.jar 查看软件,我们可以看到,更新其实是先删除在插入, 前面我们知道索引库中有两部分的内容组成,一个是索引文件,另一个是目录文件, 目前我们更新 ...
- lessc的安装
win+R,cmd打开命令面板,输入cnpm install -g less,如图,然后就可以通过lessc -v查询lessc的版本,出现版本号证明安装成功
- 初识HTML和CSS2
上节作业问题: 1.css重用 <style> 如果整个页面的宽度 > 900px时: { .c{ 共有 } .c1{ 独有 } } .c2{ 独有 } </style> ...
- HUST 1328 String (字符串前缀子串个数 --- KMP)
题意 给定一个字符串S,定义子串subS[i] = S[0..i],定义C[i]为S中subS[i]的数量,求sigma(C[i])(0<=i<N). 思路 我们以子串结尾的位置来划分阶段 ...
- Django 中设置分页页码,只显示当前页以及左右两页
设置后的效果如下: Django 给我们提供了分页的功能:`Paginator`和`Page`类都是用来做分页的.他们在Django中的路径为:`from django.core.paginator ...
- pycharm git 提交使用情况
在IDEA中进行分支切换时,出现如此错误,导致无法正常切换: error: The following untracked working tree files would be overwritte ...
- 012PHP基础知识——运算符(五)
<?php /** * 运算符的短路: * && 逻辑与 || 逻辑或 存在短路: */ /* $a = 1; $a==1 ||$c=100; //逻辑或:第一个表达式返回tru ...
- laravel5.5种的Eloquent ORM的使用:
控制器方法: //Eloquent ORM的使用: public function orm1() { //all() /*$students=Student::all(); dd($students) ...
- HDU 5793 A Boring Question (找规律 : 快速幂+乘法逆元)
A Boring Question Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...