zoukankan      html  css  js  c++  java
  • HDU 3861 The King’s Problem (Tarjan + 二分匹配)

    The King’s Problem

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


    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
     
    Recommend
    lcy
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    
    using namespace std;
    
    const int VM=50100;
    const int EM=100010;
    const int INF=0x3f3f3f3f;
    
    struct Edge{
        int to,nxt;
    }edge[EM<<1];
    
    int n,m,cnt,head[VM];
    int dep,top,atype;
    int dfn[VM],low[VM],vis[VM],stack[VM],belong[VM];
    vector<int> vt[VM];
    
    void addedge(int cu,int cv){
        edge[cnt].to=cv;    edge[cnt].nxt=head[cu];     head[cu]=cnt++;
    }
    
    void Tarjan(int u){
        dfn[u]=low[u]=++dep;
        stack[top++]=u;
        vis[u]=1;   //开始这里写成=0,WA~~~~~!!!!!!!!
        for(int i=head[u];i!=-1;i=edge[i].nxt){
            int v=edge[i].to;
            if(!dfn[v]){
                Tarjan(v);
                low[u]=min(low[u],low[v]);
            }else if(vis[v]){
                low[u]=min(low[u],dfn[v]);
            }
        }
        int j;
        if(dfn[u]==low[u]){
            atype++;
            do{
                j=stack[--top];
                belong[j]=atype;
                vis[j]=0;
            }while(u!=j);
        }
    }
    
    void init(){
        cnt=0;
        memset(head,-1,sizeof(head));
        dep=0,  top=0,  atype=0;
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(vis,0,sizeof(vis));
        memset(belong,0,sizeof(belong));
    }
    
    int linker[VM];
    
    int DFS(int u){
        int v;
        for(int i=0;i<(int)vt[u].size();i++){
            v=vt[u][i];
            if(!vis[v]){
                vis[v]=1;
                if(linker[v]==-1 || DFS(linker[v])){
                    linker[v]=u;
                    return 1;
                }
            }
        }
        return 0;
    }
    
    int Hungary(){
        int ans=0,u;
        memset(linker,-1,sizeof(linker));
        for(u=1;u<=atype;u++){
            memset(vis,0,sizeof(vis));
            if(DFS(u))
                ans++;
        }
        return ans;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int t;
        scanf("%d",&t);
        while(t--){
            scanf("%d%d",&n,&m);
            init();
            for(int i=1;i<=n;i++)
                vt[i].clear();
            int u,v;
            while(m--){
                scanf("%d%d",&u,&v);
                addedge(u,v);
            }
            for(int i=1;i<=n;i++)
                if(!dfn[i])
                    Tarjan(i);
            for(int u=1;u<=n;u++)
                for(int i=head[u];i!=-1;i=edge[i].nxt){
                    int v=edge[i].to;
                    if(belong[u]!=belong[v])
                        vt[belong[u]].push_back(belong[v]);
                }
            //printf("atype=%d\n",atype);
            printf("%d\n",atype-Hungary());
        }
        return 0;
    }
  • 相关阅读:
    TensorFlow 使用预训练好的卷积神经网络和循环神经网络(lstm)模型处理图片转文字(im2txt)
    45度做人,90度做事,180度为人,360度处事
    SQL Server 和 Oracle 数据类型对应关系
    SQL Server 创建数据库
    SQL Server 2012 忘记sa用户处理方法
    剑指offer50:数组中重复的数字
    剑指offer49:把字符串转换成整数
    剑指offer48:不用加减乘除做加法
    剑指offer47:位运算+递归。求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
    剑指offer46:圆圈中最后剩下的数字(链表,递归)
  • 原文地址:https://www.cnblogs.com/jackge/p/3137719.html
Copyright © 2011-2022 走看看