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. ASP.NET通过代码给TextBox添加事件(点击显示日历)

    private void BindDate() { tbApplyStartDate.Attributes.Add("onclick", "new Calendar(). ...

  2. distinct与NULL在count的注意事项

    select * from errorinfo ) from errorinfo select distinct info from errorinfo select count(distinct i ...

  3. ORACLE中的MERGE语法使用记录

    项目中使用到了Oracle的MERGE INTO语句,在这里简单记录下使用方法 使用场景如下: 存在对一张数据量很大的表,你需要对里面的大量数据进行更新,如果数据不存在,就进行插入的操作. 常规想到的 ...

  4. 如何转换指定 波长 到 RGB 颜色?

    //指定波长转换成RGBA颜色 std::vector<int> lambdaToColor(double lambda,double gamma = 0.8,double intensi ...

  5. Python3.6全栈开发实例[004]

    4.计算传入函数的字符串中, 数字.字母.空格以及其他内容的个数,并返回结果. s1 = 'wan%$#(gwdwq\nwdhuaiww3 w02041718' def func1(s1): dic ...

  6. Python多进程multiprocessing

    import multiprocessing import time # 具体的处理函数,负责处理单个任务 def func(msg): # for i in range(3): print (msg ...

  7. Could not create ServerSocket on address 0.0.0.0/0.0.0.0:9083

    遇到这种情况大家都找不到头绪,是因为你开始运行了hive的metastore,可以输入jps 然后出现如下: 红线所示就是hive metastore的进程 为了重新启动,需要把这个进杀掉: kill ...

  8. 解决You are using pip version 9.0.1, however version 9.0.3 is available. You should consider upgra

    直接运行命令:python -m pip install --upgrade pip

  9. chrome浏览器自动填充失效问题

    现在浏览器带有自动填充,一般在input标签中增加autocomplete="off" 可以进行控制,off代表不填充,on代表填充,这个属性也可以放在form标签中,对所有的in ...

  10. Android 结束进程的方法forceStopPackage

    ActivityManager sd = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); Method method = Clas ...