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

The King’s Problem

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

Total Submission(s): 2085    Accepted Submission(s): 741

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

2011 Multi-University Training Contest 3 - Host
by BIT

Recommend

lcy

题意:n个城市m条有向边,把这些城市分成若干个州,分的原则是(1)u和v可以互相到达的话他们两个必须在同一个州(2)同一个州里任意两个城市u和v要满足u可以到达v或者v可以到达u。问州的最小个数是多少。

思路:先用Tarjan算法进行缩点,在缩点后的图上进行二分图匹配,最后求得最小路径覆盖=强连通个数-最大匹配数。

可以看一下:

http://blog.csdn.net/hellobabygogo3/article/details/7900812

http://www.cnblogs.com/ka200812/archive/2011/07/31/2122641.html

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 1005;
const int MAXN = 5005;
const int MAXM = 100010;

int n,m;

struct Edge
{
    int to,next;
}edge[MAXM],e[MAXM];

int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Belong[MAXN],Stack[MAXN];
int Index,scc,top;
bool Inq[MAXN];

int uN,vN;
int linker[MAXN];
bool used[MAXN];
int hed[MAXN],num;

void init()
{
    tot=0;num=0;
    memset(hed,-1,sizeof(hed));
    memset(head,-1,sizeof(head));
}

void add(int u,int v)
{
    e[num].to=v;
    e[num].next=hed[u];
    hed[u]=num++;
}

void addedge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}

void Tarjan(int u)
{
    int v;
    Low[u]=DFN[u]=++Index;
    Stack[top++]=u;
    Inq[u]=true;
    for (int i=head[u];i+1;i=edge[i].next)
    {
        v=edge[i].to;
        if (!DFN[v])
        {
            Tarjan(v);
            if (Low[u]>Low[v]) Low[u]=Low[v];
        }
        else if (Inq[v]&&Low[u]>DFN[v])
            Low[u]=DFN[v];
    }
    if (Low[u]==DFN[u])
    {
        scc++;
        do{
            v=Stack[--top];
            Inq[v]=false;
            Belong[v]=scc;
        }while (v!=u);
    }
}
void solve(int N)
{
    memset(DFN,0,sizeof(DFN));
    memset(Inq,false,sizeof(Inq));
    Index=scc=top=0;
    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;i=edge[i].next)
        {
            int v=edge[i].to;
            if (Belong[u]==Belong[v]) continue;
            add(Belong[u],Belong[v]);
        }
    }
    uN=vN=scc;
}

bool dfs(int u)
{
    for (int i=hed[u];~i;i=e[i].next)
    {
        int v=e[i].to;
        if (!used[v])
        {
            used[v]=true;
            if (linker[v]==-1||dfs(linker[v]))
            {
                linker[v]=u;
                return true;
            }
        }
    }
    return false;
}

int hungary()
{
    int ans=0;
    memset(linker,-1,sizeof(linker));
    for (int i=1;i<=uN;i++)
    {
        memset(used,false,sizeof(used));
        if (dfs(i)) ans++;
        if (ans==uN) break;
    }
    return ans;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
    int i,j,t,u,v;
    sf(t);
    while (t--)
    {
        sff(n,m);
        init();
        for (i=0;i<m;i++)
        {
            sff(u,v);
            addedge(u,v);
        }
        solve(n);
        int ans=hungary();
        printf("scc=%d \n",scc);
        printf("%d\n",scc-ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-29 12:21:37

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

hdu3861The King’s Problem (强连通 缩点+最小路径覆盖)

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

HDU3861-The King’s Problem(有向图强连通缩点+最小路径覆盖)

题目链接 题意:题目大意:一个有向图,让你按规则划分区域,要求划分的区域数最少. 规则如下: 1.有边u到v以及有边v到u,则u,v必须划分到同一个区域内. 2.一个区域内的两点至少要有一方能到达另一方. 3.一个点只能划分到一个区域内. 思路:根据规则1可知必然要对强连通分量进行缩点,缩点后变成了一个弱连通图.根据规则2.3可知即是要求图的最小路径覆盖. 代码: #include <iostream> #include <cstdio> #include <cstring&

Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖

题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 这里可能有环 所以要缩点 可是看例子又发现 一个强连通分量可能要拆分 n最大才15 所以就状态压缩 将全图分成一个个子状态 每一个子状态缩点 求最小路径覆盖 这样就攻克了一个强连通分量拆分的问题 最后状态压缩DP求解最优值 #include <cstdio> #include <cstri

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 1151 Air Raid(最小路径覆盖 = 顶点数 - 最大匹配数)

Air Raid Problem Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can never reach the same

hdu 1151 Air Raid (最小路径覆盖)

Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3085    Accepted Submission(s): 2004 Problem Description Consider a town where all the streets are one-way and each street leads from on

Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖

题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走完全图 可以重复走 有向图 思路:如果是DAG图并且每个点不能重复走 那么就是裸的最小路径覆盖 现在不是DAG 可能有环 并且每个点可能重复走 对于有环 可以缩点 缩点之后的图是DAG图 另外点可以重复走和POJ 2594一样 先预处理连通性 #include <cstdio> #include <cstring> #include <vector> #include &

hdu 1151 Air Raid(二分图最小路径覆盖)

http://acm.hdu.edu.cn/showproblem.php?pid=1151 Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4029    Accepted Submission(s): 2675 Problem Description Consider a town where all the st

HDU 1151 Air Raid(最小路径覆盖)

二分图匹配(匈牙利算法的DFS实现) 初始化:g[][]两边顶点的划分情况 建立g[i][j]表示i->j的有向边就可以了,是左边向右边的匹配 g没有边相连则初始化为0 uN是匹配左边的顶点数,vN是匹配右边的顶点数 调用:res=hungary();输出最大匹配数 优点:适用于稠密图,DFS找增广路,实现简洁易于理解 时间复杂度:O(VE) ***************************************************************************/