• 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;
    }
  • 相关阅读:
    Zookeeper概念学习系列之分布式事务
    序列化 反序列化 输入流 输出流
    dfs常见的配置文件中的value与description(重要)
    [转]SQL Server Reporting Services
    [转]webpack4.0.1安装问题和webpack.config.js的配置变化
    [转]Vue.js 入门教程
    [转]Webpack 入门教程
    [转]使用C#调用cmd来执行sql脚本
    [转]winform利用读取xml获取webconfig
    [转]bootstrap栅格系统的属性及使用
  • 原文地址:https://www.cnblogs.com/jackge/p/3137719.html
Copyright © 2020-2023  润新知