现在的位置: 首页 > 综合 > 正文

POJ 2762 Going from u to v or from v to u? (有向图求单连通性)

2018年01月12日 ⁄ 综合 ⁄ 共 2623字 ⁄ 字号 评论关闭

POJ 2762 Going from u to v or from v to u? 

题意:为了让他们的儿子变得更勇敢些,Jiajia 和Wind 将他们带到一个大洞穴中。洞穴中有n 个房间,有一些单向的通道连接某些房间。每次,Wind 选择两个房间x 和y,要求他们的一个儿子从一个房间走到另一个房间,这个儿子可以从x 走到y,也可以从y 走到x。Wind 保证她布置的任务是可以完成的,但她确实不知道如何判断一个任务是否可以完成。为了使Wind 下达任务更容易些,Jiajia 决定找这样的一个洞穴,每对房间(设为x 和y)都是相通(可以从x 走到y,或者可以从y
走到x)的。给定一个洞穴,你能告诉Jiajia,Wind 是否可以任意选择两个房间而不用担心这两个房间可能不相通吗?
思路:这道题的本质是求有向图的单连通性。
单连通性:对于有向图中的任意两点u,v。一定存在从u到v的路径或是从v到u的路径。
1. 先对图求一次强连通分量,然后将每个强连通分量内的点缩成一个点。缩点之后便是一个DAG(有向无环图)。
2. 在DAG中,如果这个树有分叉,对于分叉的两点一定是不能互相到达的。所以该图一定要是一个单链才行。
3. 用拓扑排序来判断是否为单链,每次将入度为0的点加入队列,如果同时两个以上的点入度为0,那么这两个点一定是不能互相到达的,即为非单连通。
代码:
/*
ID: wuqi9395@126.com
PROG:
LANG: C++
*/
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<fstream>
#include<cstring>
#include<ctype.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF (1<<30)
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a))
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define eps 1e-6
#define debug puts("===============")
#define pb push_back
#define mkp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define POSIN(x,y) (0 <= (x) && (x) < n && 0 <= (y) && (y) < m)
typedef long long ll;
typedef unsigned long long ULL;
const int maxn = 1100;
vector<int> g[maxn];
int dfn[maxn], low[maxn], sccno[maxn], dfs_clock, scc_cnt;
stack<int> s;
void dfs(int u) {
    dfn[u] = low[u] = ++ dfs_clock;
    s.push(u);
    for (int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if (!dfn[v]) {
            dfs(v);
            low[u] = min(low[u], low[v]); //通过儿子更新low值
        }
        else if (!sccno[v]) low[u] = min(low[u], dfn[v]);   //通过回边更新low值(且回边访问的点必须不在已划分的强连通分量中)
    }
    if (low[u] == dfn[u]) { //得到强连通分量
        scc_cnt++;
        while(1) {
            int x = s.top(); s.pop();
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}
void find_scc(int n) {
    dfs_clock = scc_cnt = 0;
    memset(sccno, 0, sizeof(sccno));
    memset(dfn, 0, sizeof(dfn));
    while(!s.empty()) s.pop();
    for (int i = 1; i <= n; i++) if (!dfn[i]) dfs(i);
}
const int maxm = 6100;
int mp[maxm][2];
vector<int> newg[maxn];
int degree[maxn];
void build_new_map(int m) {
    for (int i = 1; i <= scc_cnt; i++) newg[i].clear(), degree[i] = 0;
    for (int i = 0; i < m; i++) {
        int u = sccno[mp[i][0]], v = sccno[mp[i][1]];
        if (u != v) {
            degree[v]++;
            newg[u].pb(v);
        }
    }
}
bool toposort() {
    queue<int> q;
    for (int i = 1; i <= scc_cnt; i++) if (!degree[i]) q.push(i);
    if (q.size() > 1) return 0;
    int tot = 0;
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        for (int i = 0; i < newg[u].size(); i++) {
            int v = newg[u][i];
            degree[v]--;
            if (!degree[v]) q.push(v);
        }
        if (q.size() > 1) return 0;
    }
    return 1;
}
int main () {
    int n, m;
    int t;
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &n, &m);
        for (int i = 0; i <= n; i++) g[i].clear();
        int u, v;
        rep(i, 0, m) {
            scanf("%d%d", mp[i], mp[i] + 1);
            g[mp[i][0]].pb(mp[i][1]);
        }
        find_scc(n);
        build_new_map(m);
        if (toposort()) puts("Yes");
        else puts("No");
    }
    return 0;
}

抱歉!评论已关闭.