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
题意:给出一个完全图G和一个图H,给出起点,求补图中每个点的最短路,按照顶点序号输出路径,不能到达输出“-1”。
从起点开始BFS,找到到达所有点的最短路径。
#include <cstdio>
#include <iterator>
#include <queue>
#include <set>
using namespace std;
set<int> g[200000 + 5];
set<int> p;
int vis[200000 + 5];
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i)
{
p.insert(i);
g[i].clear();
vis[i] = -1;
}
for (int i = 1; i <= m; ++i)
{
int a, b;
scanf("%d %d", &a, &b);
g[a].insert(b);
g[b].insert(a);
}
int start;
scanf("%d", &start);
queue<int> Q;
Q.push(start);
p.erase(start);
vis[start] = 0;
while (!Q.empty())
{
int temp = Q.front();
Q.pop();
vector<int> v;
set<int>::iterator it;
for (it = p.begin(); it != p.end(); ++it)
{
if (!g[temp].count(*it))
{
vis[*it] = vis[temp] + 1;
v.push_back(*it);
Q.push(*it);
}
}
int len = v.size();
for (int i = 0; i < len; ++i)
p.erase(v[i]);
}
bool flag = true;
for (int i = 1; i <= n; ++i)
{
if (i != start)
{
if (flag == true)
{
printf("%d", vis[i]);
flag = false;
}
else
printf(" %d", vis[i]);
}
}
printf("\n");
}
return 0;
}