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. HDU1688(Sightseeing)

    题目链接:传送门 题目大意:给你一幅图(单向边),找出从起点到终点有多少条不同路径(最短路或者比最短路长度大1) 题目思路:二维dijkstra,真的是要对dijkstra理解非常透彻才行,距离数组d ...

  2. 原文来自 url get

    w http://www.tuicool.com/articles/BvYbEvR http://36kr.com/p/5069371.html?utm_source=tuicool&utm_ ...

  3. 我的Android进阶之旅------>Android项目运行报java.lang.NoClassDefFoundError错误的解决办法

    今天在运行一个Android项目的时候,报了以下错误: D/AndroidRuntime( 3859): Shutting down VM E/AndroidRuntime( 3859): FATAL ...

  4. jQuery 查找标签

    1 基本选择器 2 基本筛选器 3 属性选择器 4 间接选择 1 基本选择器 //id选择器: $("#id") //标签选择器: $("tagName") / ...

  5. nodejs get请求

    const http = require('http'); http.get('http://192.168.1.6:8080/getDemo?msg=12', (res) => { const ...

  6. centos7+cobbler安装

    cobbler工作流程 1.安装软件包: yum -y install httpd dhcp tftp python-ctypes cobbler xinetd cobbler-web pykicks ...

  7. rails 下载 send_file

    def download send_file File.join(Rails.root, "public", @doc.link), :filename => @title+ ...

  8. pyhton3 re模块

    本文转自 AstralWind 的博客:Python正则表达式指南 特来收藏 1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有 ...

  9. smarty模板及其应用

    Smarty是一个使用PHP写出来的模板引擎,是目前业界最著名的PHP模板引擎之一.它分离了逻辑代码和外在的内容,提供了一种易于管理和使用的方法,用来将原本与HTML代码混杂在一起PHP代码逻辑分离. ...

  10. Adjust Linux Mint Mouse Scroll (Normal/Reverse)

    Set Scroll Normal 1 echo "pointer = 1 2 3 4 5 6 7 8 9 10 11 12" > ~/.Xmodmap && ...