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


    In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors connecting some rooms. Each time, Wind choose two rooms x and y, and ask one of their little sons go from one to the other. The son can either go from x to y, or from y to x. Wind promised that her tasks are all possible, but she actually doesn't know how to decide if a task is possible. To make her life easier, Jiajia decided to choose a cave in which every pair of rooms is a possible task. Given a cave, can you tell Jiajia whether Wind can randomly choose two rooms without worrying about anything?

    Input

    The first line contains a single integer T, the number of test cases. And followed T cases. 

    The first line for each case contains two integers n, m(0 < n < 1001,m < 6000), the number of rooms and corridors in the cave. The next m lines each contains two integers u and v, indicating that there is a corridor connecting room u and room v directly. 

    Output

    The output should contain T lines. Write 'Yes' if the cave has the property stated above, or 'No' otherwise.

    Sample Input

    1
    3 3
    1 2
    2 3
    3 1
    

    Sample Output

    Yes

    问:
    给出的图是否存在对任意的u 和 v 要么u -> v, 要么 v -> u,这两者是或者的关系 不是并且
    先求强连通,因为每个强连通分量中的点都可以相互到达,然后缩点 求最小路径覆盖
    网上都是用的拓扑求最长路径 但最小路径覆盖就是这个思想
    所以最后只要判断是否只有一个最小路径即可
    代码就是改了一下HDU 3861的输出 所以就是水题啦

    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <cstring>
    #include <map>
    #include <cctype>
    #include <set>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <bitset>
    #define rap(i, a, n) for(int i=a; i<=n; i++)
    #define rep(i, a, n) for(int i=a; i<n; i++)
    #define lap(i, a, n) for(int i=n; i>=a; i--)
    #define lep(i, a, n) for(int i=n; i>a; i--)
    #define rd(a) scanf("%d", &a)
    #define rlld(a) scanf("%lld", &a)
    #define rc(a) scanf("%c", &a)
    #define rs(a) scanf("%s", a)
    #define rb(a) scanf("%lf", &a)
    #define rf(a) scanf("%f", &a)
    #define pd(a) printf("%d
    ", a)
    #define plld(a) printf("%lld
    ", a)
    #define pc(a) printf("%c
    ", a)
    #define ps(a) printf("%s
    ", a)
    #define MOD 2018
    #define LL long long
    #define ULL unsigned long long
    #define Pair pair<int, int>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define _  ios_base::sync_with_stdio(0),cin.tie(0)
    //freopen("1.txt", "r", stdin);
    using namespace std;
    const int maxn = 110000, INF = 0x7fffffff;
    
    int n, m, s, t;
    
    vector<int> G[maxn];
    int pre[maxn], low[maxn], sccno[maxn], dfs_clock, scc_cnt;
    stack<int> S;
    
    
    void dfs(int u)
    {
        pre[u] = low[u] = ++dfs_clock;
        S.push(u);
        for(int i = 0; i < G[u].size(); i ++)
        {
            int v = G[u][i];
            if(!pre[v])
            {
                dfs(v);
                low[u] = min(low[u], low[v]);
            }
            else if(!sccno[v])
            {
                low[u] = min(low[u], pre[v]);
            }
        }
        if(low[u] == pre[u])
        {
            scc_cnt++;
            for(;;)
            {
                int x = S.top(); S.pop();
                sccno[x] = scc_cnt;
                if(x == u) break;
            }
        }
    }
    
    
    int cur[maxn], head[maxn], cnt, d[maxn], nex[maxn << 1];
    
    struct node{
        int u, v, c;
    }Node[maxn << 1];
    
    void add_(int u, int v, int c)
    {
        Node[cnt].u = u;
        Node[cnt].v = v;
        Node[cnt].c = c;
        nex[cnt] = head[u];
        head[u] = cnt++;
    }
    
    void add(int u, int v, int c)
    {
        add_(u, v, c);
        add_(v, u, 0);
    }
    
    bool bfs()
    {
        queue<int> Q;
        mem(d, 0);
        d[s] = 1;
        Q.push(s);
        while(!Q.empty())
        {
            int u = Q.front(); Q.pop();
            for(int i = head[u]; i!= -1; i = nex[i])
            {
                int v = Node[i].v;
                if(!d[v] && Node[i].c > 0)
                {
                    d[v] = d[u] + 1;
                    Q.push(v);
                    if(v == t) break;
                }
            }
        }
        return d[t] != 0;
    }
    
    int dfs(int u, int cap)
    {
        int ret = 0;
        if(u == t || cap == 0)
            return cap;
        for(int &i = cur[u];i != -1; i = nex[i])
        {
            int v = Node[i].v;
            if(d[v] == d[u] + 1 && Node[i].c > 0)
            {
                int V = dfs(v, min(Node[i].c, cap));
                Node[i].c -= V;
                Node[i ^ 1].c += V;
                ret += V;
                cap -= V;
                if(cap == 0) break;
            }
        }
        return ret;
    }
    
    int Dinic()
    {
        int ret = 0;
        while(bfs())
        {
            memcpy(cur, head, sizeof head);
            ret += dfs(s, INF);
        }
        return ret;
    }
    
    int graph[5010][5010];
    
    int main()
    {
        int T;
        rd(T);
        while(T--)
        {
            int u, v;
            mem(head, -1);
            cnt = 0;
            rd(n), rd(m);
            mem(sccno, 0);
            mem(pre, 0);
            dfs_clock = scc_cnt = 0;
            for(int i = 1; i <= n; i++) G[i].clear();
            for(int i = 1; i <= m; i++)
            {
                int u, v;
                rd(u), rd(v);
                G[u].push_back(v);
            }
            for(int i = 1; i <= n; i ++) if(!pre[i]) dfs(i);
            s = 0, t = maxn - 1;
            rap(u, 1, n)
            {
                for(int i = 0; i < G[u].size(); i ++)
                {
                    int  v = G[u][i];
                     //cout << sccno[u] << "   " << sccno[v] << endl;
                    if(sccno[u] != sccno[v])
                        add(sccno[u], scc_cnt + sccno[v], 1);
                }
            }
            rap(i, 1, scc_cnt)
                add(s, i, 1), add(scc_cnt + i, t, 1);
            if(scc_cnt - Dinic() == 1)
                printf("Yes
    ");
            else
                printf("No
    ");
               
        }
    
        return 0;
    }
    View Code


    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    排序算法<四>希尔排序
    排序算法<三>快速排序
    排序算法<二>冒泡排序
    排序算法<一>选择排序
    相信我这是水题ny1313
    斐波那契查找
    插值查找
    第四届蓝桥杯c/c++B组4
    如何使用NPM来管理你的Node.js依赖
    javascript深入学习
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/10740225.html
Copyright © 2020-2023  润新知