天天看點

HDU 3861 The King’s Problem(tarjan縮點+最小路徑覆寫ISAP)

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

1

3 2

1 2

1 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的,顯然二分圖比對會逾時。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<stack>
#define INF 0x3f3f3f3f
#define N 100010
#define M 200010
using 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;
}