The King’s Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1509 Accepted Submission(s): 542
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
Source
强连通缩点,重新构图,最小路径覆盖=点数-最大匹配
最小路径覆盖不会求,还是参考的别人的,自己还得再学习思考下
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define LINF 1000000000000000000LL
#define eps 1e-8
typedef long long ll;
const int maxn=100000+5;
struct node{
int u,to,next;
}e[maxn];
vector<int> g[maxn];
int head[maxn],edge;
int id[maxn],q[maxn],dfn[maxn],low[maxn],num[maxn];
int match[maxn];
bool vis[maxn];
int n,m,tsp,qe,cnt;
void init()
{
MST(head,-1);
edge=0;
}
void addedge(int u,int v)
{
e[edge].u=u;
e[edge].to=v;
e[edge].next=head[u];
head[u]=edge++;
}
void tarjan(int u)
{
int i,v;
dfn[u]=low[q[qe++]=u]=++tsp;
for(i=head[u];i>=0;i=e[i].next)
if(!dfn[v=e[i].to])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else
{
if(id[v]<0)
{
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u])
{
num[id[u]=++cnt]=1;
while((v=q[--qe])!=u)
++num[id[v]=cnt];
}
}
void solve()
{
int i;
tsp=qe=cnt=0;
for(i=0;i<=n;++i)
id[i]=-1,dfn[i]=0;
for(i=1;i<=n;++i)
if(!dfn[i])
tarjan(i);
}
int dfs(int x)
{
vector<int>::iterator it=g[x].begin();
for(;it!=g[x].end();it++)
{
int v=*it;
if(!vis[v])
{
vis[v]=true;
if(match[v]==-1||dfs(match[v]))
{
match[v]=x;
return 1;
}
}
}
return 0;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int u,v;
init();
scanf("%d%d",&n,&m);
REP(i,m)
{
scanf("%d%d",&u,&v);
addedge(u,v);
}
solve();
FOR(i,0,n)
g[i].clear();
FOR(i,1,n)
{
for(int j=head[i];~j;j=e[j].next)
{
if(id[i]!=id[e[j].to])
{
g[id[i]].PB(id[e[j].to]);
}
}
}
int ans=0;
MST(match,-1);
FOR(i,1,cnt)
{
//cout<<ans<<endl;
MST(vis,false);
ans+=dfs(i);
}
cout<<cnt-ans<<endl;
}
return 0;
}