---恢复内容开始---

Sparse Graph

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2590    Accepted Submission(s): 902

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
 
题意  求源点 s 在补图中到其它点的最短路长
解析 因为 给的边比较少 点比较多  补图就是一个非常稠密的图 我们不能建补图 迪杰斯特拉跑一边 会超时的 我们注意到 边权都为1   我们可以直接BFS一层一层求最短路  
每个点都入队一次 然后取队顶在未被访问过的点中找在原图中不与其相邻的点  加入队列 。因为在原图中不与点s相连的点  在补图中最短路都为1   待访问的不超过m个
所以bfs是可行的 但是我们在找 与 当前队顶其不相邻的点 用数组标记 花费时间很大(每次遍历n会超时)我们用set存一下 就好了 只留下待访问的点 时间复杂度就降下来了
 
set    AC代码
 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;
const int maxn=2e5+;
int n,m,s;
vector<int> g[maxn];
int ans[maxn],vis[maxn];
set<int> a,b;
void bfs(int x)
{
for(int i=;i<=n;i++)
if(s!=i)
a.insert(i);
queue<int> q;
q.push(x);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=;i<g[u].size();i++)
{
int v=g[u][i];
if(a.count(v))
{
b.insert(v);
a.erase(v);
}
}
set<int>::iterator it;
for(it=a.begin();it!=a.end();it++)
{
q.push(*it);
ans[*it]=ans[u]+;
}
a.swap(b);
b.clear();
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
a.clear(),b.clear();
for(int i=;i<=n;i++)
{
g[i].clear();
}
memset(ans,-,sizeof(ans));
for(int i=;i<m;i++)
{
int u,v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
cin>>s;
ans[s]=;
// set<int>::iterator j;
// for(j=a.begin();j!=a.end();j++)
// cout<<*j<<endl;
bfs(s);
if(s!=n)
for(int i=;i<=n;i++)
{
if(i!=s)
{
if(i!=n)
cout<<ans[i]<<" ";
else
cout<<ans[n]<<endl;
}
}
else
for(int i=;i<=n-;i++)
{
if(i==n-)
cout<<ans[n-]<<endl;
else
cout<<ans[i]<<" ";
}
}
return ;
}

数组超时代码

 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn=2e5+;
int n,m,s;
vector<int> g[maxn];
int ans[maxn],vis[maxn],a[maxn];
void bfs(int x)
{
vis[x]=;
queue<int> q;
q.push(x);
int cnt=;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=;i<g[u].size();i++)
{
int v=g[u][i];
if(cnt%==)
a[v]=;
else
a[v]=;
}
for(int i=;i<=n;i++)
{
if(cnt%==&&a[i]==&&!vis[i])
{
ans[i]=ans[u]+;
q.push(i);
vis[i]=;
}
if(cnt%==&&a[i]==&&!vis[i])
{
ans[i]=ans[u]+;
q.push(i);
vis[i]=;
}
}
cnt++;
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=;i<=n;i++)
g[i].clear(),a[i]=;
memset(ans,-,sizeof(ans));
memset(vis,,sizeof(vis));
for(int i=;i<m;i++)
{
int u,v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
cin>>s;ans[s]=;
bfs(s);
for(int i=;i<=n;i++)
{
if(i!=s)
cout<<ans[i]<<" ";
}
cout<<endl;
}
return ;
}

HDU 5876 补图 单源 最短路的更多相关文章

  1. HDU 5637 Transform 单源最短路

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5637 题意: http://bestcoder.hdu.edu.cn/contests/contes ...

  2. 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)

    关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...

  3. [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)

    Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...

  4. 用scheme语言实现SPFA算法(单源最短路)

    最近自己陷入了很长时间的学习和思考之中,突然发现好久没有更新博文了,于是便想更新一篇. 这篇文章是我之前程序设计语言课作业中一段代码,用scheme语言实现单源最段路算法.当时的我,花了一整天时间,学 ...

  5. 单源最短路_SPFA_C++

    当我们需要求一个点到其它所有点的最短路时,我们可以采用SPFA算法 代码特别好写,而且可以有环,但是不能有负权环,时间复杂度是O(α(n)n),n为边数,α(n)为n的反阿克曼函数,一般小于等于4 模 ...

  6. 【UVA1416】(LA4080) Warfare And Logistics (单源最短路)

    题目: Sample Input4 6 10001 3 21 4 42 1 32 3 33 4 14 2 2Sample Output28 38 题意: 给出n个节点m条无向边的图,每条边权都为正.令 ...

  7. 【算法系列学习】Dijkstra单源最短路 [kuangbin带你飞]专题四 最短路练习 A - Til the Cows Come Home

    https://vjudge.net/contest/66569#problem/A http://blog.csdn.net/wangjian8006/article/details/7871889 ...

  8. 模板C++ 03图论算法 1最短路之单源最短路(SPFA)

    3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...

  9. 2018/1/28 每日一学 单源最短路的SPFA算法以及其他三大最短路算法比较总结

    刚刚AC的pj普及组第四题就是一种单源最短路. 我们知道当一个图存在负权边时像Dijkstra等算法便无法实现: 而Bellman-Ford算法的复杂度又过高O(V*E),SPFA算法便派上用场了. ...

随机推荐

  1. ABP Zero最新版源码

    获取专业版源码  官网 学习版源码

  2. logging模块基础3

    1.logging模块的日志级别 CRITICAL = 50 #FATAL = CRITICAL ERROR = 40 WARNING = 30 #WARN = WARNING INFO = 20 D ...

  3. mysql 的 case when then 用法 和null 的判断

    表:一个表 aa 有两个字段 id 和 sex ,第1条记录的sex 为空串  ('')  第二条记录的sex 为空  (null) 1. 用法: 第一种: select (case 字段名  whe ...

  4. mac 下使用gcc 编译c函数

    mac 终端其实和window 的cmd类似,由于mac 的os x 采用了unix 系统,所以,各种类似UNIX下的命令都有用.最近在看computer science ,用到了命令行. 下面是一个 ...

  5. R in action读书笔记(13)第十章 功效分析

    功效分析 功效分析可以帮助在给定置信度的情况下,判断检测到给定效应值时所需的样本量.反过来,它也可以帮助你在给定置信度水平情况下,计算在某样本量内能检测到给定效应值的概率.如果概率低得难以接受,修改或 ...

  6. MSSQL 重新生成索引,重新组织索引

    > 5% 且 < = 30% ALTER INDEX REORGANIZE > 30% ALTER INDEX REBUILD WITH (ONLINE = ON)* * 重新生成索 ...

  7. MIUI类ROM如何正确修改dpi

    (以下以MIUI为例) 在miui上,如果通过简单的修改build.prop会导致图标重绘错误,App图标分裂.此时配合一条简单的命令即可实现完美无bug的dpi修改. 1.使用终端模拟器执行su 2 ...

  8. java图片放大或缩小

    package org.jimmy.autotranslate20181022.utils; import java.awt.Graphics; import java.awt.image.Buffe ...

  9. ArrayList中removeAll和clear的区别(无区别)

    removeAll会直接调用此方法,传入list和false,因中间的逻辑都不会走(如果由retainAll方法调用,则会走这些逻辑判断),所以只需要看finaly中的最后一个if条件,w=0,通过循 ...

  10. 第2节 hive基本操作:12、hive当中的hql语法

    3.2. hive查询语法 3.2.1.SELECT https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Select 基本 ...