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 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
**题意:**给你n个点的完全图,再给你m条边需要删除,给定s,问s到其他所有点的距离 \\(2\le N\le 200000 \\) \\(0\le M\le 20000\\)
**思路:**因为边权值为1,所以直接BFS,再者就是考虑如何枚举点了,用vis标记的话,时间会不够(TLE了一发),在这里使用set维护点集(多方便)但是要注意把点从集合中去除时,不能直接边遍历边去,而是一遍遍历完一起去掉,因为使用的迭代器地址不会随删除而改变...
/** @Date : 2016-11-14-17.20
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version :
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>
#define pii pair<int , int>
#define MP(x, y) make_pair((x) ,(y))
#define ff first
#define ss second
#define LL long long
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 2e5+20;
map<pii ,bool>mp;
set<int>st;
set<int>::iterator it;
int res[N];
int n, m;
void bfs(int s,int n)
{
queue<int>q, t;
res[s] = 0;
q.push(s);
st.erase(s);
while(!q.empty())
{
int nw = q.front();
//cout << nw;
q.pop();
for(it = st.begin(); it != st.end(); it++)
{
//cout << "~"<< *it << " " ;
if(!mp[MP(nw, *it)])
{
res[*it] = res[nw] + 1;
q.push(*it);
t.push(*it);
//st.erase(*it);//不能直接删会有问题
//if(st.empty())
//break;
}
}
while(!t.empty())
{
int x = t.front();
t.pop();
st.erase(x);
}
//cout << endl;
}
}
int main()
{
int T;
while(~scanf("%d", &T))
{
while(T--)
{
mp.clear();
st.clear();
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
st.insert(i);
while(m--)
{
int x, y;
scanf("%d%d", &x, &y);
mp[MP(x, y)] = mp[MP(y, x)] = 1;
}
int s;
scanf("%d", &s);
bfs(s, n);
////
int flag = 0;
for(int i = 1; i <= n; i++)
{
if(i != s)
{
if(flag)
printf(" ");
if(res[i] != 0)
printf("%d", res[i]);
else printf("-1");
flag = 1;
}
}
printf("\n");
}
}
return 0;
}
/*
99
6 9
1 3
1 4
2 3
2 4
2 5
3 5
3 6
4 6
5 6
1
*/
HDU 5876 Sparse Graph BFS+set删点的更多相关文章
- HDU 5876 Sparse Graph BFS 最短路
Sparse Graph Problem Description In graph theory, the complement of a graph G is a graph H on the ...
- 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 ...
- hdu 5876 Sparse Graph 无权图bfs求最短路
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) P ...
- HDU 5876 Sparse Graph
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- HDU 5876 Sparse Graph(补图上BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 有一个 n 个点无向图,再给你 m 对顶点, 代表着这 m 对顶点之间没有边, 除此之外 ...
- HDU 5876 Sparse Graph(补图中求最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 在补图中求s到其余各个点的最短路. 思路:因为这道题目每条边的距离都是1,所以可以直接用bfs来做 ...
- hdu 5876 Sparse Graph icpc大连站网络赛 1009 补图最短路
BFS+链表 代码改自某博客 #include<stdio.h> #include<iostream> #include<algorithm> #include&l ...
- HDU 5867 Sparse Graph (2016年大连网络赛 I bfs+补图)
题意:给你n个点m条边形成一个无向图,问你求出给定点在此图的补图上到每个点距离的最小值,每条边距离为1 补图:完全图减去原图 完全图:每两个点都相连的图 其实就是一个有技巧的bfs,我们可以看到虽然点 ...
- HDU 5876 关于补图的bfs
1.HDU 5876 Sparse Graph 2.总结:好题,把STL都过了一遍 题意:n个点组成的完全图,删去m条边,求点s到其余n-1个点的最短距离. 思路:把点分为两个集合,A为所有没有到达 ...
随机推荐
- Python中的__future__
在Python中,你如果在某一个版本的Python想使用未来版本中的功能,可以使用如下语法实现: from __future__ import futurename 这条语句必须放在module文件的 ...
- Python中如何Getting Help
在Python中Gettting Help有如下两种方法: 1 使用dir函数,dir的参数可以是一个真正的对象实例,也可以是一个数据类型,无论哪种情形,dir函数都返回与这个对象或者数据类型相关联的 ...
- C++读取文件统计单词个数及频率
1.Github链接 GitHub链接地址https://github.com/Zzwenm/PersonProject-C2 2.PSP表格 PSP2.1 Personal Software Pro ...
- request.getRequestDispatcher不能实现页面跳转的原因
我在JS里面写了个Ajax,传值给servlet,然后利用request.getRequestDispatcher(),打算跳转至另外一个页面.但是没有跳转成功,运行之后没反应. 在网上搜了资料发现, ...
- 简单理解SQL Server锁机制
多个用户同时对数据库的并发操作时,可能会遇到下面几种情况,导致数据前后不一致: 1,A.B事务同时对同一个数据进行修改,后提交的人的修改结果会破坏先提交的(丢失更新): 2,事务A修改某一条数据还未提 ...
- Win10修改编辑hosts文件无法保存怎么办
Win10无法修改编辑保存hosts文件怎么办?Win10系统默认是没有权限去编辑保存系统里的文件,这也是权限不够才导致修改编辑hosts后无法保存的原因,解决的办法就是把自己的帐户权限给提高就行了. ...
- C#中的unsafe
为了保持类型安全性,默认情况下,C# 不支持指针算法. 但是,通过使用 unsafe 关键字,可以定义可在其中使用指针的不安全上下文. 有关指针的详细信息,请参阅主题指针类型. 备注 在公共语言运行时 ...
- Maven 生命周期 和插件
1.3 生命周期1.3.1 什么是生命周期? Maven生命周期就是为了对所有的构建过程进行抽象和统一.包括项目清理.初始化.编译.打包.测试.部署等几乎所有构建步骤. 生命周期可以理解为构建工程的步 ...
- HTML5可用的css reset
html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, abbr, address, ci ...
- FTP-Server
1.目录: 2. ftp_client.py import socket,os,json client=socket.socket() client.connect(('localhost',9999 ...