Sparse Graph

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

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 notadjacent 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
 
 
 
解析:补图上的最短路。维护一个set,里面保存尚未访问的结点。BFS的扩展结点u的时候,把与u直接相连的结点排除,得到的set在补图中都与u有一条权为1的边,访问完之后把这些点从set中删除,开始下一次扩展。即在扩展结点u的时候,只扩展与u没有边相连的结点,有边相邻的结点等下一次再扩展。
 
 
 
#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <queue>
using namespace std; const int INF = 0x3f3f3f3f;
const int MAXN = 2e5+5;
vector<int> e[MAXN];
int dis[MAXN];
int n, m, s; void bfs()
{
set<int> s1; //保存还未被访问过的结点
set<int> s2; //保存与节点u相连的结点
set<int>::iterator it;
for(int i = 1; i <= n; ++i){ //s1初始化为除结点s外的所有结点
if(i != s)
s1.insert(i);
}
queue<int> q;
q.push(s);
dis[s] = 0;
while(!q.empty()){
int u = q.front();
q.pop();
int len = e[u].size();
for(int i = 0; i < len; ++i){
int v = e[u][i];
if(s1.find(v) == s1.end())
continue;
s1.erase(v); //把与结点u之间有边的点从s1排除
s2.insert(v); //并加入到s2
}
for(it = s1.begin(); it != s1.end(); ++it){
q.push(*it);
dis[*it] = dis[u]+1;
}
s1.swap(s2); //s1中的结点已经全部访问,s2中的结点尚未访问,二者交换
s2.clear(); //清空
}
} void solve()
{
memset(dis, INF, sizeof(dis));
for(int i = 1; i <= n; ++i)
e[i].clear();
int u, v;
while(m--){
scanf("%d%d", &u, &v);
e[u].push_back(v);
e[v].push_back(u);
}
scanf("%d", &s);
bfs();
for(int i = 1; i <= n; ++i){
if(i == s)
continue;
if(dis[i] >= INF)
dis[i] = -1;
printf("%d", dis[i]);
if(i != n)
printf(" ");
}
printf("\n");
} int main()
{
int t;
scanf("%d", &t);
while(t--){
scanf("%d%d", &n, &m);
solve();
}
return 0;
}

  

  

HDU 5876 Sparse Graph的更多相关文章

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

  2. HDU 5876 Sparse Graph BFS 最短路

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

  3. hdu 5876 Sparse Graph 无权图bfs求最短路

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

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

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

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

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

  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. HDU 5867 Sparse Graph (2016年大连网络赛 I bfs+补图)

    题意:给你n个点m条边形成一个无向图,问你求出给定点在此图的补图上到每个点距离的最小值,每条边距离为1 补图:完全图减去原图 完全图:每两个点都相连的图 其实就是一个有技巧的bfs,我们可以看到虽然点 ...

  9. HDU 5876 关于补图的bfs

    1.HDU 5876  Sparse Graph 2.总结:好题,把STL都过了一遍 题意:n个点组成的完全图,删去m条边,求点s到其余n-1个点的最短距离. 思路:把点分为两个集合,A为所有没有到达 ...

随机推荐

  1. SpingMVC ModelAttribute的用法

    @Controller @RequestMapping(value = "/test") public class TestController { @RequestMapping ...

  2. Netty 的Downstream 和 Upstream

    Netty的Downstream 和 Upstream 如果一个event从第一个handler传递直到最后一个handler就是 Upstream 相反的如果一个event从最后一个handler传 ...

  3. vlc/ffmepg/mplayer/gstreamer/openmax/mpc/ffdshow/directshow

    一些应该学习的开源框架与库用途和差别 一.播放器层次 这个层次上,是直接可以用的软件,已经做完了一切工作,如果我们需要用他们,是不需要写一行代码的,编译通过就可以拿来使用了,对于国内这些山寨公司来说, ...

  4. 搜索之BM25和BM25F模型

    www.netfoucs.com/article/wdxin1322/94603.html#

  5. 对Memcached使用的总结和使用场景

    1.memcached是什么 Memcached 常被用来加速应用程序的处理,在这里,我们将着重于介绍将它部署于应用程序和环境中的最佳实践.这包括应该存储或不应存储哪些.如何处理数据的灵活分布以 及如 ...

  6. xmlsechema验证

                   //创建xmlDocument                 XmlDocument doc = new XmlDocument();                  ...

  7. 加密解密(9)Diffie-Hellman密钥交换协议

    过程如下 : 1,小李把KeyX经过加密变化,生成MsgA传给老王. 2,老王得到MsgA,保存在本地. 3,老王把KeyY经过加密变化,生成MsgB传给小李, 4,小李得到MsgB保存在本地, 5, ...

  8. awk当中使用外部变量

    1.awk命令使用双引号的情况下 此时在awk命令里面使用\"$var\"就可以引用外部环境变量的var的值 $ var="BASH";echo "u ...

  9. [POJ1330]Nearest Common Ancestors(LCA, 离线tarjan)

    题目链接:http://poj.org/problem?id=1330 题意就是求一组最近公共祖先,昨晚学了离线tarjan,今天来实现一下. 个人感觉tarjan算法是利用了dfs序和节点深度的关系 ...

  10. 【转载】Java中如何写一段内存泄露的程序 & ThreadLocal 介绍和使用

    可以参考这段文章: link A1:通过以下步骤可以很容易产生内存泄露(程序代码不能访问到某些对象,但是它们仍然保存在内存中): 上文中提到了使用ThreadLocal造成了内存泄露,但是写的不清不楚 ...