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
乍一看很像 求scc的个数,但是连样例都过不去,仔细看题,题目中有个关键性的条件,就是 国王要求 在有路的两个点之间,要么有从(u–>v)或者 有(v–>u) ,并且这些点在同一个地区内。
所以就不是简单的scc问题了,其实是求DAG图的最小顶点覆盖。
代码
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN = 5000+100;
const int MAXM = 1e6;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();}
while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar();}
return f*x;
}
/*----------------------------*/
struct Edge{
int from,to,nexts;
}edge[MAXM];
int head[MAXN],top;
int n,m;
void init(){
memset(head,-1,sizeof(head)) ;
top=0;
}
void addedge(int a,int b){
Edge e={a,b,head[a]} ;
edge[top]=e;head[a]=top++;
}
void getmap(){
int a,b;
while(m--){
a=read();b=read();
addedge(a,b);
}
}
int dfn[MAXN],low[MAXN];
int sccno[MAXN],scc_cnt;
stack<int>S;bool Instack[MAXN];
vector<int>scc[MAXN];
bool mp[MAXN][MAXN];
int dfs_clock;
void tarjan(int now){
low[now]=dfn[now]=++dfs_clock;
S.push(now);Instack[now]=1;
for(int i=head[now];i!=-1;i=edge[i].nexts){
Edge e=edge[i];
if(!dfn[e.to]){
tarjan(e.to);
low[now]=min(low[now],low[e.to]);
}else if(Instack[e.to])
low[now]=min(low[now],dfn[e.to]);
}
if(dfn[now]==low[now]){
scc_cnt++;scc[scc_cnt].clear();
for(;;){
int nexts=S.top();S.pop();Instack[nexts]=0;
sccno[nexts]=scc_cnt;
scc[scc_cnt].push_back(nexts);
if(nexts==now) break;
}
}
}
void find_cut(int le,int ri){
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(Instack,0,sizeof(Instack));
memset(sccno,0,sizeof(sccno));
dfs_clock=scc_cnt=0;
for(int i=le;i<=ri;i++)
if(!dfn[i]) tarjan(i);
}
void suodian(){ // 建立DAG图
for(int i=1;i<=scc_cnt;i++)
memset(mp[i],0,sizeof(mp[i]));//都要赋值为0才行。
for(int i=0;i<top;i++){
Edge e=edge[i];
int now=sccno[e.from];
int nexts=sccno[e.to];
if(now!=nexts){
mp[now][nexts]=1;
}
}
}
int pipei[MAXN];
bool vis[MAXN];
bool Find(int x){//二分匹配求最小路径覆盖
for(int i=1;i<=scc_cnt;i++){
if(!vis[i]&&mp[x][i]){
vis[i]=1;
if(!pipei[i]||Find(pipei[i])){
pipei[i]=x;
return 1;
}
}
}
return 0;
}
void solve(){
memset(pipei,0,sizeof(pipei));
int cnt=0;
for(int i=1;i<=scc_cnt;i++){
memset(vis,0,sizeof(vis));
if(Find(i)) cnt++;
}
printf("%d\n",scc_cnt-cnt);
}
int main(){
int t;cin>>t;while(t--){
scanf("%d%d",&n,&m);
init();
getmap();
find_cut(1,n);
suodian();
solve();
}
return 0.;
}