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

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. [转]无废话SharePoint入门教程二[SharePoint发展、工具及术语]

    本文转自:http://www.cnblogs.com/iamlilinfeng/p/3186919.html 一.前言 1.由于上一篇文章的标题命名失误,此篇标题写给百度搜索”什么是SharePoi ...

  2. Aspose.Word 的常见使用(2018-12-26 更新版)

    Aspose.Word 的常见使用 起因 因项目需要,而且使用html转Word的时候,样式不兼容问题,于是只能使用Aspose.Word通过代码生成.下面是通过DocumentBuilder来设计W ...

  3. rem手机端页面自适应布局(待修正下一篇完美布局)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. [Windows Server 2012] MySQL移机方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:MySQL ...

  5. mysql-oracle

    1.oracle数据崩导入时部分字段数据太长导入失败:原因是两边oracle字符集不一致 https://www.cnblogs.com/baoposhou/p/5669778.html 2.orac ...

  6. 使用JDBC创建出版社和书籍管理系统

    1.需求 已知如下两个表: publisher id name(唯一) address book id isbn name publisher_id 欢迎进入书籍管理系统 1.出版社管理:增.删(na ...

  7. 类方法__setattr__,__delattr__,__getattr__

    __getattr__,_delattr_,_getattr_ class Foo: x = 1 def __init__(self, y): self.y = y def __getattr__(s ...

  8. 02Hibernate基本配置

    Hibernate基本配置 1.引入jar 2.建立项目 3.创建实体类 package com.sqlserver.domain; public class Customer { long cust ...

  9. css滚动条样式修改

    .activeMoreBankList{ height: 188px; overflow-y: auto;} /*滚动条样式*/.activeMoreBankList::-webkit-scrollb ...

  10. IP、CIDR、广播地址、子网掩码、MAC地址--这些是什么鬼

    继续学习趣谈网络协议中的内容,认识几个专有名词,IP.CIDR.广播地址.子网掩码.MAC地址,这些都是什么鬼? 一.IP IP地址是一个网卡在网络世界的通讯地址,相当于我们现实世界的门牌号码 (1) ...