HDU3861 The King’s Problem

传送门
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

题目大意

有n个点,一些点之间存在有向边,定义一种集合为集合中的点可以互相到达或者可以单向到达,且都不能经过别的集合中的点。问至少需要分成几个集合。(多组数据)

题解

tarjan缩点当然是一眼看出的,但是由于单向到达也可以算在内,所以我们需要将这个新图转换成二分图,跑二分图最大匹配,可以用最大流解决。

CODE:

#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=50005;
const int M=1e5+10;
const int INF=1e9;
struct edge
{
    int nxt,to;
}a[M];
struct Edge
{
    int nxt,to,remain;
}e[M<<4];
int head[N],Head[N];
int dfn[N],low[N],block[N];
int s[N],top;
int deep[N];
bool instack[N];
int Q,n,m,x,y,num,Num,tot,Time,S,T;
queue<int>q;
inline int min(const int &a,const int &b){return a<b?a:b;}
inline void add(int x,int y)
{
    a[++num].nxt=head[x],a[num].to=y,head[x]=num;
}
inline void add2(int x,int y)
{
    e[++Num].nxt=Head[x],e[Num].to=y,e[Num].remain=1,Head[x]=Num;
    e[++Num].nxt=Head[y],e[Num].to=x,e[Num].remain=0,Head[y]=Num;
}
void dfs(int now)
{
    dfn[now]=low[now]=++Time;
    instack[now]=1;
    s[++top]=now;
    for(int i=head[now];i;i=a[i].nxt)
      if(!dfn[a[i].to])
      {
        dfs(a[i].to);
        low[now]=min(low[now],low[a[i].to]);
      }
      else if(instack[a[i].to]) low[now]=min(low[now],dfn[a[i].to]);
    if(low[now]==dfn[now])
    {
        int tmp;
        tot++;
        do tmp=s[top--],instack[tmp]=0,block[tmp]=tot;
        while(tmp!=now);
    }
}
inline bool bfs()
{
    memset(deep,0x3f,sizeof(deep));
    deep[S]=0;q.push(S);
    while(!q.empty())
    {
        int tmp=q.front();q.pop();
        for(int i=Head[tmp];i;i=e[i].nxt)
          if(deep[e[i].to]>INF&&e[i].remain) q.push(e[i].to),deep[e[i].to]=deep[tmp]+1;
    }
    return deep[T]<INF;
}
int dfs(int now,int limit)
{
    if(now==T||!limit) return limit;
    int flow=0,f;
    for(int i=Head[now];i;i=e[i].nxt)
      if(deep[e[i].to]==deep[now]+1&&e[i].remain&&(f=dfs(e[i].to,min(limit,e[i].remain))))
      {
        flow+=f,limit-=f,e[i].remain-=f,e[i^1].remain+=f;
        if(!limit) return flow;
      }
    deep[now]=-1;return flow;
}
inline int dinic(){int ans=0;while(bfs())ans+=dfs(S,INF);return ans;}
int main()
{
    scanf("%d",&Q);
    while(Q--)
    {
        scanf("%d%d",&n,&m);
        memset(dfn,0,sizeof(dfn));
        memset(head,0,sizeof(head));
        memset(Head,0,sizeof(Head));
        num=tot=Time=0;Num=1;
        for(int i=1;i<=m;i++)
          scanf("%d%d",&x,&y),add(x,y);
        for(int i=1;i<=n;i++)
          if(!dfn[i]) dfs(i);
        for(int j=1;j<=n;j++)
          for(int i=head[j];i;i=a[i].nxt)
            if(block[j]!=block[a[i].to]) add2(block[j],block[a[i].to]+tot);
        S=tot<<1|1,T=S+1;
        for(int i=1;i<=tot;i++)
          add2(S,i),add2(tot+i,T);
        printf("%d\n",tot-dinic());
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值