The King’s Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3064 Accepted Submission(s): 1080
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
Recommend
lcy
题目大意:给出一张有向图,要求你将这些点进行划分,划分依据如下
1.如果两个点互相可达,那么这两个点必须在一个集合中
2.同一个集合中任意两个点u,v要满足,要么u能到达v,要么v能到达u
3.一个点只能被划分到一个集合
问最少能划分成几个点集
解题思路:首先先求出所有的强连通分量,满足条件1
满足条件2,3的话,就要求出最小路径覆盖
所以可以将所有的强连通分量进行缩点,桥作为连接,然后匈牙利一下,求出最大匹配数,再用强连通分量的数量-最大匹配数,就是答案了
//china no.1
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;
#define pi acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x) memset(x,0,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=1e3+10;
const int maxx=2e5+100;
const double EPS=1e-7;
const int MOD=10000007;
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
int vis[maxx],dfn[maxx],low[maxx],bel[maxx],num[maxx],res,pre[maxx],a[maxx],in[maxx];
int cont=1,n,m,num2[maxx],num3[maxx],ans[maxx],link[maxx];
struct node
{
int u,v,w;
}s[maxx];
vector<int>G[maxx],G_new[maxx];
queue<int>Q;
stack<int>S;
vector<int>g[maxx];
void dfs(int x)
{
dfn[x]=low[x]=cont++;
S.push(x);vis[x]=1;
int len=G[x].size();
for(int i=0; i<len; i++)
if(!vis[G[x][i]])
{
dfs(G[x][i]);
low[x]=min(low[x],low[G[x][i]]);
}
else if(vis[G[x][i]]==1)
low[x]=min(low[x],dfn[G[x][i]]);
if(dfn[x]==low[x])
{
res++;
while(1)
{
int t=S.top();
S.pop();
vis[t]=2; //访问完成
bel[t]=res;
num[res]++;
if(x==t)break;
}
// if(res>1)ans++;
}
}
void init()
{
me(pre);me(vis);(low);me(bel);me(dfn);me(num);me(in);
me(num);me(num2);me(num3);me(ans);me(link);
for(int i=1;i<=n;i++)
{
G[i].clear();
pre[i]=INF;
g[i].clear();
}
me(s);
// memset(a,INF,sizeof(a));
res=0;cont=1;
}
bool Find(int u)
{
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i];
if(!vis[v])
{
vis[v]=1;
if(!link[v]||Find(link[v]))
{
link[v]=u;
return 1;
}
}
}
return 0;
}
int t;
//void solve()
int main()
{
cin>>t;
for(int cas=1;cas<=t;cas++)
{
// while(~scanf("%d%d",&n,&m))
// {
//close();
scanf("%d%d",&n,&m);
init();
for(int i=1;i<=m;i++)
{
int x,y,z;
//scanf("%d%d%d",&x,&y,&z);
scanf("%d%d",&x,&y);
// x++;y++;
// s[i].u=x;s[i].v=y;s[i].w=z;
G[x].push_back(y);
}
for(int i=1;i<=n;i++)
if(!vis[i]) dfs(i);
for(int i=1; i<=n; i++)
{
int len=G[i].size();
for(int j=0; j<len; j++)
if(bel[i]!=bel[G[i][j]])
g[bel[i]].push_back(bel[G[i][j]]);
}
int ans=0;
for(int i=1;i<=res;i++)
{
me(vis);
if(!link[i]&&Find(i))
ans++;
}
cout<<res-ans<<endl;
// cout<<ans<<endl;
}
}
/*
int main()
{
int t;
//t=1;
// close();
cin>>t;
for(cas=1;cas<=t;cas++)
solve();
}*/