HDU 3861 强联通+二分匹配
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.
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.
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
题意:一个有向图,让你按规则划分区域,要求划分的区域数最少。
规则如下:1、有边u到v以及有边v到u,则u,v必须划分到同一个区域内。2、一个区域内的两点至少要有一方能到达另一方。3、一个点只能划分到一个区域内。
解题思路:首先对图进行tarjin缩点,将其变成一个弱连通图,根据题目的规则可以发现这是一个最小路径覆盖的问题,将所点后的图重新建图,对其进行二分图匹配,则总连通数-最大匹配数即为答案。
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<list>
#include<set>
#include<math.h>
using namespace std;
int head[10000],tol,low[10000],dfn[10000],Stack[100000],instack[100000],indexx,top,scc,belong[10000],linker[6000],vis[6000];
struct node
{
int next,to;
}edge[300000];
void add(int u,int v)
{
edge[tol].to=v;
edge[tol].next=head[u];
head[u]=tol++;
}
vector<int> s[6000];
void tarjin(int u)
{
low[u]=dfn[u]=++indexx;
int i,v;
Stack[top++]=u;instack[u]=1;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].to;
if(dfn[v]==0)
{
tarjin(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
scc++;
do
{
v=Stack[--top];
instack[v]=0;
belong[v]=scc;
}while(v!=u);
}
}
void rebuild(int n)
{
int i,u,v;
for(i=1;i<=scc;i++)s[i].clear();
for(u=1;u<=n;u++)
{
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].to;
if(belong[u]!=belong[v])s[belong[u]].push_back(belong[v]);
}
}
}
bool dfs(int u)
{
int i,len=s[u].size();
for(i=0;i<len;i++)
{
int v=s[u][i];
if(vis[v]==0)
{
vis[v]=1;
if(linker[v]==-1||dfs(linker[v]))
{
linker[v]=u;
return 1;
}
}
}
return 0;
}
void solve(int n)
{
memset(instack,0,sizeof(instack));
memset(dfn,0,sizeof(dfn));
memset(belong,0,sizeof(belong));
indexx=scc=top=0;
int i,u,v;
for(i=1;i<=n;i++)if(dfn[i]==0)tarjin(i);
//tarjin(1);
// if(scc==1){puts("1");return;}
// cout<<scc<<endl;
rebuild(n);
memset(linker,-1,sizeof(linker));
int cnt=0;
for(i=1;i<=scc;i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i))cnt++;
}
//cout<<scc<<endl;
printf("%d\n",scc-cnt);
}
int main()
{
int i,j,k,m,n,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));tol=0;
while(m--)
{
scanf("%d%d",&i,&j);
add(i,j);
}
// cout<<tol<<endl;
solve(n);
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<list>
#include<set>
#include<math.h>
using namespace std;
int head[10000],tol,low[10000],dfn[10000],Stack[100000],instack[100000],indexx,top,scc,belong[10000],linker[6000],vis[6000];
struct node
{
int next,to;
}edge[300000];
void add(int u,int v)
{
edge[tol].to=v;
edge[tol].next=head[u];
head[u]=tol++;
}
vector<int> s[6000];
void tarjin(int u)
{
low[u]=dfn[u]=++indexx;
int i,v;
Stack[top++]=u;instack[u]=1;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].to;
if(dfn[v]==0)
{
tarjin(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
scc++;
do
{
v=Stack[--top];
instack[v]=0;
belong[v]=scc;
}while(v!=u);
}
}
void rebuild(int n)
{
int i,u,v;
for(i=1;i<=scc;i++)s[i].clear();
for(u=1;u<=n;u++)
{
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].to;
if(belong[u]!=belong[v])s[belong[u]].push_back(belong[v]);
}
}
}
bool dfs(int u)
{
int i,len=s[u].size();
for(i=0;i<len;i++)
{
int v=s[u][i];
if(vis[v]==0)
{
vis[v]=1;
if(linker[v]==-1||dfs(linker[v]))
{
linker[v]=u;
return 1;
}
}
}
return 0;
}
void solve(int n)
{
memset(instack,0,sizeof(instack));
memset(dfn,0,sizeof(dfn));
memset(belong,0,sizeof(belong));
indexx=scc=top=0;
int i,u,v;
for(i=1;i<=n;i++)if(dfn[i]==0)tarjin(i);
//tarjin(1);
// if(scc==1){puts("1");return;}
// cout<<scc<<endl;
rebuild(n);
memset(linker,-1,sizeof(linker));
int cnt=0;
for(i=1;i<=scc;i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i))cnt++;
}
//cout<<scc<<endl;
printf("%d\n",scc-cnt);
}
int main()
{
int i,j,k,m,n,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));tol=0;
while(m--)
{
scanf("%d%d",&i,&j);
add(i,j);
}
// cout<<tol<<endl;
solve(n);
}
return 0;
}

浙公网安备 33010602011771号