Sparse GraphTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3083 Accepted Submission(s): 1035 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 .
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 2016 ACM/ICPC Asia Regional Dalian Online
Recommend wange2014
|
BFS求解最短路,给出补图,给出一个点S,求S点到其原图其他点的最短距离。
以S点为源点跑BFS,初始的时候把所有点加入set中,先去除与s点相连的点,然后放入队列,开另一个set存放与s相连的点,每次从s2替换s1,s2清空。不利用第二个set会超时。
#include <algorithm> //STL通用算法
#include <bitset> //STL位集容器
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex> //复数类
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque> //STL双端队列容器
#include <exception> //异常处理类
#include <fstream>
#include <functional> //STL定义运算函数(代替运算符)
#include <limits>
#include <list> //STL线性列表容器
#include <map> //STL 映射容器
#include <iomanip>
#include <ios> //基本输入/输出支持
#include<iosfwd> //输入/输出系统使用的前置声明
#include <iostream>
#include <istream> //基本输入流
#include <ostream> //基本输出流
#include <queue> //STL队列容器
#include <set> //STL 集合容器
#include <sstream> //基于字符串的流
#include <stack> //STL堆栈容器
#include <stdexcept> //标准异常类
#include <streambuf> //底层输入/输出支持
#include <string> //字符串类
#include <utility> //STL通用模板类
#include <vector> //STL动态数组容器
#include <cwchar>
#include <cwctype>
#define ll long long
using namespace std;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,1,-1};
//priority_queue<int,vector<int>,less<int> >q;
const int maxn = 200000+66;
const ll mod=1e9+7;
int n;
vector<int>g[maxn];
int a[maxn];
int s;
int dis[maxn];
int vis[maxn];
void bfs()
{
queue<int>q;
q.push(s);
set<int>s1;
set<int>s2;
//s1.clear();
for(int i=1; i<=n; i++)
{
if(i!=s)
{
s1.insert(i);//插入点
//cout<<i<<"===="<<endl;
}
}
while(q.size())
{
int fro=q.front();
q.pop();
//cout<<fro<<"---"<<endl;
int len=g[fro].size();
//cout<<len<<"---";
for(int i=0; i<len; i++)
{
int v=g[fro][i];
s1.erase(v);//删除
s2.insert(v);
}
set<int>::iterator it;
for(it=s1.begin(); it!=s1.end(); it++)
{
if(vis[*it])
continue;
dis[*it]=dis[fro]+1;
//cout<<dis[*it]<<endl;
q.push(*it);
vis[*it]=1;
//cout<<*it<<" ";
}
s1.swap(s2);
s2.clear();
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(dis,9999999,sizeof(dis));
memset(vis,0,sizeof(vis));//--
for(int i=1; i<=n; i++)
g[i].clear();
int m;
scanf("%d %d",&n,&m);
while(m--)
{
int u,v;
scanf("%d %d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
scanf("%d",&s);
dis[s]=0;
bfs();
int cnt=0;
for(int i=1; i<=n; i++)
{
if(i!=s)
{
cnt++;
if(dis[i]>=9999999)
cout<<"-1";
else
cout<<dis[i]<<"";
if(cnt!=n-1)cout<<" ";
}
}
cout<<endl;
}
return 0;
}