---恢复内容开始---
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 ;
}