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;
    }
  • 相关阅读:
    Android 杀掉当前程序的进程
    Android Studio 2.3更换默认的ConstraintLayout布局
    Android ConstraintLayout约束控件链接整理
    Java 实现字符串反转
    Android 网络技术HTTP
    数据库框架 Litepal
    Android FastJson解析
    Java类的初始化顺序
    Android 免费短信获取国家列表和国家代码
    【转】仿Android 联系人SideBar排序,根据拼音A-Z字母快速导航,以及输入搜索条件过滤,显示姓名的文字图片
  • 原文地址:https://www.cnblogs.com/jackge/p/3137719.html
Copyright © 2011-2022 走看看