• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
未来が好き!
博客园    首页    新随笔    联系   管理    订阅  订阅
hdu 5876 ACM/ICPC Dalian Online 1009 Sparse Graph

题目链接

分析:这叫补图上的BFS,萌新第一次遇到= =。方法很简单,看了别人的代码后,自己也学会了。方法就是开两个集合,一个A表示在下一次bfs中能够到达的点,另一个B就是下一次bfs中到不了的点。一开始先把出了起点的所有点都加入A,然后从bfs的点跑一遍边, 把边相连的点从A中取出放到B中。然后遍历A集合,进行bfs。然后把B全部放入A中,清空B。于是又回到了通过边把边连接的点从A移动到B,重复bfs。。。解释地不是很清楚,大体意思就是这样的了。在这个方法中,每个点和每条边都值操作过一次,复杂度是O(N+M)但是set好像有一点常数(?)萌新表示不是很清楚。

代码:

/*****************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define   offcin        ios::sync_with_stdio(false)
#define   sigma_size    26
#define   lson          l,m,v<<1
#define   rson          m+1,r,v<<1|1
#define   slch          v<<1
#define   srch          v<<1|1
#define   sgetmid       int m = (l+r)>>1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   bits(a)       __builtin_popcount(a)
#define   mk            make_pair
#define   pb            push_back
#define   fi            first
#define   se            second

const int    INF    = 0x3f3f3f3f;
const LL     INFF   = 1e18;
const double pi     = acos(-1.0);
const double inf    = 1e18;
const double eps    = 1e-9;
const LL     mod    = 1e9+7;
const int    maxmat = 10;
const ull    BASE   = 31;

/*****************************************************/

const int maxn = 2e5 + 5;
std::vector<int> G[maxn];
set<int> in, out;
int N, M;
int dis[maxn];
bool inn[maxn];
void bfs(int s) {
    queue<int> que;
    in.clear(); out.clear();
    mem(inn, false);
    mem(dis, INF);
    for (int i = 1; i <= N; i ++) if (i != s) {
        in.insert(i);
        inn[i] = true;
    }
    dis[s] = 0;
    que.push(s);
    while (!que.empty() && in.size() != 0) {
        int u = que.front(); que.pop();
        for (int i = 0; i < G[u].size(); i ++) {
            int v = G[u][i];
            if (inn[v]) {
                inn[v] = false;
                in.erase(v);
                out.insert(v);
            }
        }
        for (set<int> :: iterator it = in.begin(); it != in.end(); it ++) {
            int v = *it;
            inn[v] = false;
            if (dis[v] > dis[u] + 1) {
                dis[v] = dis[u] + 1;
                que.push(v);
            }
        }
        in.clear();
        for (set<int> :: iterator it = out.begin(); it != out.end(); it ++) {
            int k = *it;
            inn[k] = true;
            in.insert(k);
        }
        out.clear();
    }
}
int main(int argc, char const *argv[]) {
    int T; cin>>T;
    while (T --) {
        scanf("%d%d", &N, &M);
        for (int i = 1; i <= N; i ++) G[i].clear();
        for (int i = 0; i < M; i ++) {
            int u, v;
            scanf("%d%d", &u, &v);
            G[u].pb(v);
            G[v].pb(u);
        }
        int s; scanf("%d", &s);
        bfs(s);
        for (int i = 1; i <= N; i ++) if (i != s) {
            printf("%d", dis[i] == INF ? -1 : dis[i]);
            if (i == N) puts("");
            else printf(" ");
        }
    }
    return 0;
}
posted on 2016-09-11 20:19  未来が好き!  阅读(126)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3