zoukankan      html  css  js  c++  java
  • HDU 3861 The King’s Problem(tarjan缩点+最小路径覆盖:sig-最大二分匹配数,经典题)

    The King’s Problem

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


    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
     
    题目意思:
    现在有n个点,m条边的有向图,要求划分的区域最少
    规则如下:
    1.可以互相到达的点必须属于一个区域
    2.u可以到v或者v可以到v,即一个区域内任意两点u,v,必须存在路径从u->v或者从v->u
    3.一个点只能属于一个区域
    4.所有点都应该被划分
    分析:
    可以互相到达的点肯定是属于一个强连通分量的,所以利用tarjan将属于同一个强连通分量的点缩成一个点
    得到新图,现在新图是一个DAG图,有向无环图
    最小路径的定义:在一个有向图中,找出最少的路径,使得这些路径经过了所有的点
    对照一下题目:一个区域其实就是一条路径
    最少的区域数目就是最少的路径数目
    所以题目转换成最小不相交的路径覆盖,注意:不相交的路径,疑问一个点只能属于一个区域
    最小不相交路径覆盖=点数-最大二分匹配
    所以对得到的新图,求一遍最大二分匹配就好
    最大二分匹配用匈牙利算法写
     
    #include<stdio.h>
    #include<iostream>
    #include<math.h>
    #include<string.h>
    #include<set>
    #include<map>
    #include<list>
    #include<math.h>
    #include<queue>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    #define INF 0x7fffffff
    #define mem(a,x) memset(a,x,sizeof(a))
    int mon1[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
    int mon2[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31};
    int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
    
    int getval()
    {
        int ret(0);
        char c;
        while((c=getchar())==' '||c=='
    '||c=='
    ');
        ret=c-'0';
        while((c=getchar())!=' '&&c!='
    '&&c!='
    ')
            ret=ret*10+c-'0';
        return ret;
    }
    
    #define max_v 5005
    int dfn[max_v];
    int low[max_v];
    int vis[max_v];
    int stk[max_v];
    int color[max_v];
    vector<int> G[max_v];
    vector<int> G2[max_v];
    int n,m;
    int sig,cnt,sp;
    
    int link[max_v];
    int match[max_v];
    void init()
    {
        mem(dfn,0);
        mem(low,0);
        mem(vis,0);
        mem(stk,0);
        mem(color,0);
        for(int i=1;i<=n;i++)
        {
            G[i].clear();
            G2[i].clear();
        }
        sig=0;
        cnt=1;
        sp=-1;
    }
    
    int tarjan(int u)
    {
        vis[u]=1;
        low[u]=dfn[u]=cnt++;
        stk[++sp]=u;
        for(int j=0;j<G[u].size();j++)
        {
            int v=G[u][j];
            if(vis[v]==0)
                tarjan(v);
            if(vis[v]==1)
                low[u]=min(low[u],low[v]);
        }
        if(low[u]==dfn[u])
        {
            sig++;
            do
            {
                color[stk[sp]]=sig;
                vis[stk[sp]]=-1;
            }while(stk[sp--]!=u);
        }
    }
    
    int dfs(int u)
    {
        for(int j=0;j<G2[u].size();j++)
        {
            int v=G2[u][j];
            if(vis[v]==0)
            {
                vis[v]=1;
                if(match[v]==-1||dfs(match[v]))
                {
                    match[v]=u;
                    return 1;
                }
            }
        }
        return 0;
    }
    
    int max_match()//匈牙利算法
    {
        mem(match,-1);
        int ans=0;
        for(int i=1;i<=sig;i++)
        {
            mem(vis,0);
            if(dfs(i))
                ans++;
        }
        return ans;
    }
    
    int main()
    {
        int t;
        cin>>t;
        int x,y;
        while(t--)
        {
            scanf("%d %d",&n,&m);
            init();
            for(int i=1;i<=m;i++)
            {
                scanf("%d %d",&x,&y);
                if(count(G[x].begin(),G[x].end(),y)==0)//重边
                    G[x].push_back(y);
            }
            for(int i=1;i<=n;i++)
            {
                if(vis[i]==0)
                    tarjan(i);
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=0;j<G[i].size();j++)
                {
                    if(color[i]!=color[G[i][j]])
                    {
                        if(count(G2[color[i]].begin(),G2[color[i]].end(),color[G[i][j]])==0)//重边
                            G2[color[i]].push_back(color[G[i][j]]);
                    }
                }
            }
            printf("%d
    ",sig-max_match());//最小不相交路径覆盖=新图点数-最大二分匹配数
        }
        return 0;
    }
    /*
    题目意思:
    现在有n个点,m条边的有向图,要求划分的区域最少
    规则如下:
    1.可以互相到达的点必须属于一个区域
    2.u可以到v或者v可以到v,即一个区域内任意两点u,v,必须存在路径从u->v或者从v->u
    3.一个点只能属于一个区域
    4.所有点都应该被划分
    
    分析:
    可以互相到达的点肯定是属于一个强连通分量的,所以利用tarjan将属于同一个强连通分量的点缩成一个点
    得到新图,现在新图是一个DAG图,有向无环图
    
    最小路径的定义:在一个有向图中,找出最少的路径,使得这些路径经过了所有的点
    
    对照一下题目:一个区域其实就是一条路径
    最少的区域数目就是最少的路径数目
    所以题目转换成最小不相交的路径覆盖,注意:不相交的路径,疑问一个点只能属于一个区域
    
    最小不相交路径覆盖=点数-最大二分匹配
    所以对得到的新图,求一遍最大二分匹配就好
    最大二分匹配用匈牙利算法写
    
    gameover!
    
    */
     
    心之所向,素履以往
  • 相关阅读:
    字符串基本操作
    条件、循环、函数定义 练习
    turtle库基础练习
    Python基础练习
    AutoLayout 教程
    Mac上最佳的SVN管理工具:Cornerstone
    图片上传 关于压缩的问题
    关于单元测试的问题
    获取ios设备的当前IP地址
    关于项目使用ARC的管理方式
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9983400.html
Copyright © 2011-2022 走看看