大连网络赛--Sparse Graph--BFS求补图中无权最短路径

本文介绍了一种使用BFS算法求解补图上最短路径的方法,具体为:给定一个无向图及其补图,从指定顶点出发,计算到其他所有顶点的最短距离。通过维护两个集合,分别存储已访问和待访问的顶点,确保了算法的高效运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sparse Graph

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

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

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值