HDU 3861 The King’s Problem(tarjan缩点+最小路径覆盖ISAP)

来源:互联网 发布:hbase快速清空表数据 编辑:程序博客网 时间:2024/05/18 08:33

The King’s Problem

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

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

13 21 21 3

Sample Output

2

Source

2011 Multi-University Training Contest 3 - Host by BIT

Recommend

lcy



         一道裸的图论题,算是长点见识。

         首先相互连通的点要在同一部分,相当于缩点,用tarjan求连通分量缩点即可。

         缩点之后就是一个DAG,在DAG上把所有的点分成最少的部分,然后每部分任意两点是单连通的。这就相当于是用最少的路径把所有的点给覆盖,最小路径覆盖问题。

         关于DAG的最小路径覆盖问题,可以用二分图匹配的方法,把每一个点i分为i和i'。然后假设有边u->v,那么连边u->v'。然后跑二分图匹配,用n-最大匹配就是最小路径覆盖。

         但是,如果点的个数达到10W呢?不管怎么样,二分图匹配的复杂度都是大于n^2的,显然二分图匹配会超时。

         于是我们想到了用最大流取替代二分图匹配。在10W个点的时候,边的个数不会很多,相当于是一个稀疏图,而在稀疏图上跑最大流是非常快的。dinic、isap什么都行。

         具体见代码:

#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<vector>#include<stack>#define INF 0x3f3f3f3f#define N 100010#define M 200010using namespace std;typedef pair<int,int> P;vector<int> g[N];stack<int> sta;map<P,bool> mp;int dfn[N],low[N],belong[N];int n,m,dindex,cnt;char v[N];namespace ISAP{    int H[M],d[M],cur[M],pre[M],gap[M],Q[M],RC[M];    struct Edge{int u,v,c,n;} E[M];    int nv,flow,head,tail,cntE,f;    void init(){cntE=0; memset(H,-1,sizeof(H));}    void addedge(int u,int v,int c)    {        E[cntE]=Edge{u,v,c,H[u]}; H[u]=cntE++;        E[cntE]=Edge{v,u,0,H[v]}; H[v]=cntE++;    }    void revbfs(int s,int t)    {        head=tail=0 ;        memset(d,-1,sizeof(d));        memset(gap,0,sizeof(gap));        Q[tail++]=t;d[t]=0;gap[d[t]]=1;        while (head!=tail)        {            int u=Q[head++];            for (int i=H[u];~i;i=E[i].n)            {                int v=E[i].v; if (~d[v]) continue;                d[v]=d[u]+1; gap[d[v]]++; Q[tail++]=v;            }        }    }    int isap(int s,int t)    {        memcpy(cur,H,sizeof(cur)); nv=t;        flow=0; revbfs(s,t); int u=pre[s]=s,i;        while (d[s]<nv)        {            if (u==t)            {                f=INF;                for (i=s;i!=t;i=E[cur[i]].v)                    if (f>E[cur[i]].c) f=E[cur[i]].c,u=i;                flow += f;                for (i=s;i!=t;i=E[cur[i]].v)                    E[cur[i]].c-=f,E[cur[i]^1].c+=f;            }            for (i=cur[u];~i;i=E[i].n)                if (E[i].c&&d[u]==d[E[i].v]+1) break ;            if (~i) cur[u]=i,pre[E[i].v]=u,u=E[i].v;            else            {                if (0==--gap[d[u]]) break ;                int minv=nv,v;                for (int i=H[u];~i;i=E[i].n)                {                    v=E[i].v;                    if (E[i].c&&minv>d[v]) minv=d[v],cur[u]=i;                }                d[u]=minv+1; gap[d[u]]++; u=pre[u];            }        }        return flow ;    }}inline void tarjan(int x){low[x]=dfn[x]=++dindex;v[x]=true; sta.push(x);for(int i=0;i<g[x].size();i++){int y=g[x][i];if (!dfn[y]){tarjan(y);low[x]=min(low[x],low[y]);} else if (v[y]) low[x]=min(low[x],dfn[y]);}if (dfn[x]==low[x]){cnt++; int j=0;do{j=sta.top();sta.pop();v[j]=0; belong[j]=cnt;} while (j!=x);}}void init(){    mp.clear();    dindex=cnt=0;    ISAP::init();    memset(g,0,sizeof(g));    memset(dfn,0,sizeof(dfn));}int main(){    int T_T;    scanf("%d",&T_T);    while(T_T--)    {        init();        scanf("%d%d",&n,&m);        for(int i=1;i<=m;i++)        {            int u,v; scanf("%d%d",&u,&v);            g[u].push_back(v);        }        for(int i=1;i<=n;i++)            if (!dfn[i]) tarjan(i);//缩点        for(int i=1;i<=n;i++)            for(int j=0;j<g[i].size();j++)            {                int u=belong[i];                int v=belong[g[i][j]];                if (mp[P{u,v}]||u==v) continue;//重构图,保证每条边只被加进去一次                mp[P{u,v}]=1;                ISAP::addedge(u,v+cnt,1);//连边u->v'            }        int s=2*cnt+1,t=2*cnt+2;        for(int i=1;i<=cnt;i++)        {            ISAP::addedge(s,i,1);            ISAP::addedge(i+cnt,t,1);        }        printf("%d\n",cnt-ISAP::isap(s,t));//最小路径覆盖=n-最大流(最大匹配)    }    return 0;}

阅读全文
0 0
原创粉丝点击