hdu 3861 The King’s Problem (强连通+最小路径覆盖)

The King’s Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1637    Accepted Submission(s): 600

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
#include"stdio.h"
#include"string.h"
#include"queue"
#include"vector"
#include"algorithm"
using namespace std;
#define N 5005
#define M 100005
#define min(a,b) (a<b?a:b)
vector<int>g[N];
struct node
{
    int u,v,next;
}e[M];
int t,stop,index,bcnt,head[N],link[N];
int mark[N],dfn[N],low[N],stap[N],be[N];
bool vis[N];
int find(int k)
{
    int i,v;
    for(i=0;i<g[k].size();i++)
    {
        v=g[k][i];
        if(!vis[v])
        {
            vis[v]=true;
            if(link[v]==-1||find(link[v]))
            {
                link[v]=k;
                return 1;
            }
        }
    }
    return 0;
}
void add(int u,int v)
{
    e[t].u=u;
    e[t].v=v;
    e[t].next=head[u];
    head[u]=t++;
}
void tarjan(int u)
{
    int i,v;
    dfn[u]=low[u]=++index;
    stap[++stop]=u;
    mark[u]=1;
    for(i=head[u];i!=-1;i=e[i].next)
    {
        v=e[i].v;
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(mark[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])
    {
        bcnt++;
        do
        {
            v=stap[stop--];
            mark[v]=0;
            be[v]=bcnt;
        }
        while(u!=v);
    }
}
void solve(int n)
{
    int i;
    index=bcnt=stop=0;
    memset(dfn,0,sizeof(dfn));
    for(i=1;i<=n;i++)
    {
        if(!dfn[i])
            tarjan(i);
    }
}
void work(int n)
{
    int i,j,u,v;
    for(i=0;i<N;i++)
        g[i].clear();
    for(i=1;i<=n;i++)
    {
        u=be[i];
        for(j=head[i];j!=-1;j=e[j].next)
        {
            v=be[e[j].v];
            if(u!=v)
            {
                g[u].push_back(v);
               // g[v].push_back(u);
            }
        }
    }
    int ans=0;
    memset(link,-1,sizeof(link));
    for(i=1;i<=bcnt;i++)
    {
        memset(vis,false,sizeof(vis));
        ans+=find(i);
    }
    printf("%d\n",bcnt-ans);
}
int main()
{
    int T,n,m,u,v,i;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        t=0;
        memset(head,-1,sizeof(head));
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            add(u,v);
        }
        solve(n);
        //printf("bcnt%d\n",bcnt);
        work(n);
    }
    return 0;
}

hdu 3861 The King’s Problem (强连通+最小路径覆盖),布布扣,bubuko.com

时间: 2024-10-12 23:00:26

hdu 3861 The King’s Problem (强连通+最小路径覆盖)的相关文章

HDU - 3861 The King’s Problem(强连通分量+最小路径覆盖)

题目大意:给出一张有向图,要求你将这些点进行划分,划分依据如下 1.如果两个点互相可达,那么这两个点必须在一个集合中 2.同一个集合中任意两个点u,v要满足,要么u能到达v,要么v能到达u 3.一个点只能被划分到一个集合 问最少能划分成几个点集 解题思路:首先先求出所有的强连通分量,满足条件1 满足条件2,3的话,就要求出最小路径覆盖 所以可以将所有的强连通分量进行缩点,桥作为连接,然后匈牙利一下,求出最大匹配数,再用强连通分量的数量-最大匹配数,就是答案了 #include <cstdio>

HDU 3861 The King’s Problem (强连通+二分匹配)

题目地址:HDU 3861 这题虽然是两个算法结合起来的.但是感觉挺没意思的..结合的一点也不自然,,硬生生的揉在了一块...(出题者不要喷我QAQ.) 不过这题让我发现了我的二分匹配已经好长时间没用过了..都快忘了..正好在省赛之前又复习了一下. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm>

HDU 3861 The King&#39;s Problem(强连通分量缩点+最小路径覆盖)

http://acm.hdu.edu.cn/showproblem.php?pid=3861 题意: 国王要对n个城市进行规划,将这些城市分成若干个城市,强连通的城市必须处于一个州,另外一个州内的任意两个城市u,v,有从u到v的路径或从v到u的路径.求最少可以分成几个州. 思路: 这道题目挺好. 首先,强连通分量进行缩点,重新建图. 新建的图就是一个DAG图,接下来就转换成了最小路径覆盖问题. 最小路径覆盖就是用尽量少的不相交的简单路径覆盖DAG的所有顶点.每个顶点只属于一条路径,单个顶点也可以

HDU 3861 The King’s Problem(强连通+二分图最小路径覆盖)

HDU 3861 The King's Problem 题目链接 题意:给定一个有向图,求最少划分成几个部分满足下面条件 互相可达的点必须分到一个集合 一个对点(u, v)必须至少有u可达v或者v可达u 一个点只能分到一个集合 思路:先强连通缩点,然后二分图匹配求最小路径覆盖 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #include <

HDU 3861 The King’s Problem 最小路径覆盖(强连通分量缩点+二分图最大匹配)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 最小路径覆盖的一篇博客:https://blog.csdn.net/qq_39627843/article/details/82012572 题意: 把城市至少分成几个块,规则有三 1. A能B,B能到A,那么A,B一定要在一起. 2. 一个城市只能属于一个块. (说明了是最小不相交覆盖)3. 在一个块里的城市,任意2点之间必须有路径. 对于规则1,就是说强连通的必须在一起,所以用Tarjan

HDU - 3861 The King’s Problem (强连通分量+最小路径覆盖)

思路:tarjarn缩点,然后剩下的就是纯粹的最小路径覆盖,最小路径覆盖=顶点数-匹配数.匹配数跑一遍匈牙利即可. 1 #include <iostream> 2 #include <queue> 3 #include <stack> 4 #include <cstdio> 5 #include <vector> 6 #include <map> 7 #include <set> 8 #include <bitset

HDU 3861.The King’s Problem 强联通分量+最小路径覆盖

The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2947    Accepted Submission(s): 1049 Problem Description In the Kingdom of Silence, the king has a new problem. There are N cit

hdu 3861 The King’s Problem trajan缩点+二分图匹配

The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) 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 betw

hdu3861 强连通+最小路径覆盖

题意:有 n 个点,m 条边的有向图,需要将这些点分成多个块,要求:如果两点之间有路径能够互相到达,那么这两个点必须分在同一块:在同一块内的任意两点相互之间至少要有一条路径到达,即 u 到达 v 或 v 到达 u:每个点都只能存在于单独一个块内.问最少需要划分多少块. 首先,对于如果两点之间能够相互到达则必须在同一块,其实也就是在同一个强连通分量中的点必须在同一块中,所以首先就是强连通缩点.然后在同一块内的任意两点之间要有一条路,那么其实就是对于一块内的强连通分量,至少要有一条路径贯穿所有分量.