zoukankan      html  css  js  c++  java
  • hdu 3861 The King’s Problem

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3289    Accepted Submission(s): 1165


    Problem Description
    In the Kingdom of Silence, the king has a new problem. There are N cities in the kingdom and there are M directional roads between the cities. That means that if there is a road from u to v, you can only go from city u to city v, but can’t go from city v to city u. In order to rule his kingdom more effectively, the king want to divide his kingdom into several states, and each city must belong to exactly one state. What’s more, for each pair of city (u, v), if there is one way to go from u to v and go from v to u, (u, v) have to belong to a same state. And the king must insure that in each state we can ether go from u to v or go from v to u between every pair of cities (u, v) without passing any city which belongs to other state.
      Now the king asks for your help, he wants to know the least number of states he have to divide the kingdom into.
     
    Input
    The first line contains a single integer T, the number of test cases. And then followed T cases.

    The first line for each case contains two integers n, m(0 < n <= 5000,0 <= m <= 100000), the number of cities and roads in the kingdom. The next m lines each contains two integers u and v (1 <= u, v <= n), indicating that there is a road going from city u to city v.
     
    Output
    The output should contain T lines. For each test case you should just output an integer which is the least number of states the king have to divide into.
     
    Sample Input
    1
    3 2
    1 2
    1 3
     
    Sample Output
    2
     
    Source
     
     
    Tarjan缩点+匈牙利算法
    Tarjan写错了。。
    #include <cstring>
    #include <cstdio>
    #define N 100005
    
    struct Edge
    {
        Edge *next;
        int to;
    }*head[N],edge[N];
    struct EDge
    {
        EDge *next;
        int to;
    }*newhead[N],newedge[N];
    bool instack[N],vis[N];
    int cnt,T,n,m,ans,stack[N],top,low[N],dfn[N],tim,col[N],sumcol,f[N];
    inline void init()
    {
        ans=top=tim=sumcol=cnt=0;
        memset(f,-1,sizeof(f));
        memset(col,0,sizeof(col));
        memset(low,0,sizeof(low));
        memset(dfn,0,sizeof(dfn));
        memset(head,0,sizeof(head));
        memset(edge,0,sizeof(edge));
        memset(newhead,0,sizeof(newhead));
        memset(newedge,0,sizeof(newedge));
    }
    struct node
    {
        int x,y;
    }e[N<<10];
    inline int min(int a,int b) {return a>b?b:a;}
    void tarjan(int x)
    {
        low[x]=dfn[x]=++tim;
        instack[x]=1;
        stack[++top]=x;
        for(Edge * u=head[x];u;u=u->next)
        {
            int v=u->to;
            if(instack[v]) low[x]=min(low[x],dfn[v]);
            else if(!dfn[v])
            {
                tarjan(v);
                low[x]=min(low[x],low[v]);
            }
        }
        if(low[x]==dfn[x])
        {
            int k;
            sumcol++;
            do
            {
                k=stack[top--];
                instack[k]=false;
                col[k]=sumcol;
            }while(k!=x);
        }
    }
    bool dfs(int x)
    {
        for(EDge * u=newhead[x];u;u=u->next)
        {
            int v=u->to;
            if(!vis[v])
            {
                vis[v]=1;
                if(f[v]==-1||dfs(f[v]))
                {
                    f[v]=x;
                    return 1;
                }
            }
        }
        return 0;
    }
    inline void ins(int u,int v)
    {
        edge[++cnt].next=head[u];
        edge[cnt].to=v;
        head[u]=edge+cnt;
    }
    inline void insnew(int u,int v)
    {
        newedge[++cnt].next=newhead[u];
        newedge[cnt].to=v;
        newhead[u]=newedge+cnt;
    }
    int Main()
    {
        scanf("%d",&T);
        for(;T--;)
        {
            scanf("%d%d",&n,&m);
            init();
            for(int i=1;i<=m;++i)
            {
                scanf("%d%d",&e[i].x,&e[i].y);
                ins(e[i].x,e[i].y);
            }
            for(int i=1;i<=n;++i)
             if(!dfn[i]) tarjan(i);
            cnt=0;
            for(int i=1;i<=m;++i)
            {
                int cx=col[e[i].x],cy=col[e[i].y];
                if(cx!=cy) insnew(cx,cy);
            }
            for(int i=1;i<=sumcol;++i)
            {
                memset(vis,0,sizeof(vis));
                if(dfs(i)) ans++;
            }
            printf("%d
    ",sumcol-ans);
        }
        return 0;
    }
    int sb=Main();
    int main(int argc,char *argv[]) {;}
    我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
  • 相关阅读:
    SDUT 2143 图结构练习——最短路径 SPFA模板,方便以后用。。 Anti
    SDUT ACM 1002 Biorhythms 中国剩余定理 Anti
    nyist OJ 119 士兵杀敌(三) RMQ问题 Anti
    SDUT ACM 2157 Greatest Number Anti
    SDUT ACM 2622 最短路径 二维SPFA启蒙题。。 Anti
    二叉索引树 区间信息的维护与查询 Anti
    SDUT ACM 2600 子节点计数 Anti
    UVA 1428 Ping pong 二叉索引树标准用法 Anti
    2010圣诞Google首页效果
    Object
  • 原文地址:https://www.cnblogs.com/ruojisun/p/7528209.html
Copyright © 2011-2022 走看看