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[]) {;}
    我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
  • 相关阅读:
    Objective-C NSFileManager 文件管理总结
    ScrollView在RelativeLayout失效问题
    解决myeclipse中struts2 bug问题包的替换问题
    SOA究竟是个啥
    Flash--元件和实例
    MyEclipse中加入web项目到tomcat
    [C]if (CONDITION)语句中CONDITION的情况
    GTK经常使用控件之笔记本控件( GtkNotebook )
    org.apache.solr.handler.dataimport.DataImportHandlerException: Data Config problem: 对实体 &quot;characterEn
    Android自动测试之Monkey工具
  • 原文地址:https://www.cnblogs.com/ruojisun/p/7528209.html
Copyright © 2011-2022 走看看