Sparse Graph

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Problem Description
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 other vertices.

 
Input
There 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.
 
Output
For 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
 
Source
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
const int N=1e5+,M=4e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
vector<int>v[N<<];
queue<int>q;
set<int>s;
int flag[N<<];
int ans[N<<];
void init(int n)
{
s.clear();
for(int i=;i<=n;i++)
v[i].clear(),flag[i]=,ans[i]=;
while(!q.empty())q.pop();
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
init(n);
for(int i=;i<m;i++)
{
int u,w;
scanf("%d%d",&u,&w);
v[u].push_back(w);
v[w].push_back(u);
}
int st;
scanf("%d",&st);
q.push(st);
for(int i=;i<=n;i++)if(i!=st)s.insert(i);
flag[st]=,ans[st]=;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=;i<v[u].size();i++)
flag[v[u][i]]=;
for(set<int>::iterator itt,it=s.begin();it!=s.end();)
{
if(flag[*it])
{
q.push(*it);
ans[*it]=ans[u]+;
itt=it;
it++;
s.erase(itt);
}
else
it++;
}
for(set<int>::iterator it=s.begin();it!=s.end();it++)flag[*it]=;
}
int flag=;
for(int i=;i<=n;i++)
{
if(i==st)
continue;
printf("%d%c",ans[i],(flag++>=(n-)?'\n':' '));
}
}
return ;
}

hdu 5876 Sparse Graph 无权图bfs求最短路的更多相关文章

  1. HDU 5876 Sparse Graph(补图中求最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 在补图中求s到其余各个点的最短路. 思路:因为这道题目每条边的距离都是1,所以可以直接用bfs来做 ...

  2. HDU 5876 Sparse Graph(补图上BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 有一个 n 个点无向图,再给你 m 对顶点, 代表着这 m 对顶点之间没有边, 除此之外 ...

  3. 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 ...

  4. HDU 5876 Sparse Graph BFS 最短路

    Sparse Graph Problem Description   In graph theory, the complement of a graph G is a graph H on the ...

  5. HDU 5876 Sparse Graph

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  6. 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 ...

  7. hdu 5876 Sparse Graph icpc大连站网络赛 1009 补图最短路

    BFS+链表 代码改自某博客 #include<stdio.h> #include<iostream> #include<algorithm> #include&l ...

  8. 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

    这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...

  9. UVA 816 -- Abbott's Revenge(BFS求最短路)

     UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...

随机推荐

  1. 防sql注入之参数绑定 SQL Injection Attacks and Defense

    http://php.net/manual/zh/pdo.prepared-statements.php 预处理语句与存储过程 很多更成熟的数据库都支持预处理语句的概念.什么是预处理语句?可以把它看作 ...

  2. Avoiding Full Table Scans

    w MySQL :: MySQL 5.7 Reference Manual :: 9.2.1.19 Avoiding Full Table Scanshttps://dev.mysql.com/doc ...

  3. 用户画像 销量预测 微观 宏观 bi

    w 目前我们没有自己的平台 第三方平台又不会给任何我们想要的数据   没有用户的注册信息 全天候的行为信息   用户画像没法做    针对我们业务的bi做的思路是什么呢   数据中心怎么做销量预测呢 ...

  4. Nginx服务监听端口修改启动bug

    监听的端口从80 修改到其他端口出现启动不起来问题. 解决方案如下: yum install policycoreutils-python sudo cat /var/log/audit/audit. ...

  5. h5-localStorage实现缓存ajax请求数据

    使用背景:要实现每次鼠标hover“能力雷达”,则显示能力雷达图(通过ajax请求数据实现雷达图数据显示),所以每次hover都去请求ajax会影响性能,因此这里要用到本地缓存. 实现: 此处是通过传 ...

  6. java NIO 模型(一)

    1. 阻塞I/O通信模型 1.性能:一连接一线程模型导致服务端的并发接入数和系统吞吐量受到极大限制 2.可靠性:由于IO操作采用同步阻塞模式,当网络拥塞或者逻辑处理缓慢会导致IO线程被挂住,阻塞时间无 ...

  7. Red Gate系列 - SQL各种工具

    Red Gate系列 - SQL各种工具 Red Gate系列文章: Red Gate系列之一 SQL Compare 10.4.8.87 Edition 数据库比较工具 完全破解+使用教程 Red ...

  8. Latex技巧:插入参考文献

    LaTeX插入参考文献,可以使用BibTex,也可以不使用BibTex. 方法一:不使用BibTeX 先在文章文章末尾写好需要插入的参考文献,逐一写出,例如: \begin{thebibliograp ...

  9. 002 MIRO发票校验采购订单项目科目分配类别检查增强-20150819

    BADI SE19:ZINVOICE_UPDATE   MIRO发票检验过账好模拟时,检查采购订单line 是否有固定资产的行项目,如果有固定资产项目,则弹出提示框,提示消息:存在规定资产采购项目! ...

  10. Android sdk manager加载缓慢或加载不出来

    1.打开android sdk manager 2.打开tool->options,如图所示 3.将Proxy Settings 里的HTTP Proxy Server和HTTP Proxy P ...