On the mysterious continent of Tamriel, there is a great empire founded by human.
To develope the trade, the East Empire Company is set up to transport goods from place to place.
Recently, the company wants to start their business in Solstheim, which is consists of N islands.
Luckily, there are already M sea routes.
All routes are one-way, and the i-th route can transport person and goods from island ui to vi.
Now, the company nominates you a particular job to plan some new routes to make sure that person and goods can be transported between any two islands.
Furthermore, because the neighboring regions are under attack by an increasing number of dragons, limited resources can be used to set up new routes.
So you should plan to build new routes as few as possible.
Input Format
The first line contains an integer T, indicating that there are T test cases.
For each test case, the first line includes two integers N (N≤10000) and M (M≤100000), as described above.
After that there are M lines. Each line contains two integers ui and vi.
Output Format
For each test case output one integer, represent the least number of routes required to new.
样例输入
2 4 3 1 2 2 3 3 4 4 4 1 2 1 4 3 2 3 4
样例输出
1 2
题目来源
缩点后,统计入度为0的个数A,出度为0的个数B。
1、出度(入读)为0的点都需要加边。
2、 贪心先两两配对;
多的出度(入度)为0的点再和一般的点来做边
所以结果为max(A , B)
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
new Task().solve();
}
}
class Task {
InputReader in = new InputReader(System.in) ;
PrintWriter out = new PrintWriter(System.out) ;
final int N = 10008 ;
final int M = 100008 ;
class Edge {
int u, v, next;
Edge(int u, int v , int next) {
this.u = u;
this.v = v;
this.next = next;
}
}
Edge[] e = new Edge[M];
int[] head = new int[N];
int eid;
void add(int u, int v) {
e[eid] = new Edge(u, v , head[u]);
head[u] = eid++;
}
int[] dfn = new int[N];
int[] low = new int[N];
int[] stack = new int[N];
boolean[] inStack = new boolean[N];
int[] belong = new int[N];
int[] bccSize = new int[N];
int top, dfnTime, bccId;
void tarjan(int u, int father) {
dfn[u] = low[u] = ++dfnTime;
stack[top++] = u;
inStack[u] = true;
for (int i = head[u]; i != -1; i = e[i].next) {
int v = e[i].v;
if (dfn[v] == -1) {
tarjan(v, u);
low[u] = Math.min(low[u], low[v]);
} else if (inStack[v])
low[u] = Math.min(low[u], dfn[v]);
}
if (low[u] == dfn[u]) {
int now;
bccId++;
do {
now = stack[--top];
inStack[now] = false;
belong[now] = bccId;
bccSize[bccId]++;
} while (now != u);
}
}
void clear() {
Arrays.fill(dfn, -1);
Arrays.fill(head, -1);
Arrays.fill(inStack, false);
Arrays.fill(bccSize, 0);
eid = top = dfnTime = bccId = 0;
}
int doTarjan(int n){
for (int i = 1; i <= n; i++) {
if (dfn[i] == -1)
tarjan(i, i);
}
if(bccId == 1){
return 0 ;
}
int[] inCnt = new int[bccId+1] ;
int[] outCnt = new int[bccId+1] ;
Arrays.fill(inCnt, 0) ;
Arrays.fill(outCnt, 0) ;
for (int u = 1; u <= n; u++) {
for (int i = head[u]; i != -1; i = e[i].next) {
if (belong[u] != belong[e[i].v]){
outCnt[belong[e[i].u]]++ ;
inCnt[belong[e[i].v]]++ ;
}
}
}
int inSum = 0 , outSum = 0 ;
for (int i = 1; i <= bccId; i++) {
if(inCnt[i] == 0){
inSum++ ;
}
if(outCnt[i] == 0){
outSum++ ;
}
}
return Math.max(inSum, outSum) ;
}
void solve(){
int t = in.nextInt() ;
while(t-- > 0){
int n = in.nextInt() ;
int m = in.nextInt() ;
clear() ;
for(int i = 1 ; i <= m ; i++){
add(in.nextInt() , in.nextInt()) ;
}
out.println(doTarjan(n)) ;
}
out.flush() ;
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = new StringTokenizer("");
}
private void eat(String s) {
tokenizer = new StringTokenizer(s);
}
public String nextLine() {
try {
return reader.readLine();
} catch (Exception e) {
return null;
}
}
public boolean hasNext() {
while (!tokenizer.hasMoreTokens()) {
String s = nextLine();
if (s == null)
return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] nextInts(int n) {
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = nextInt();
}
return nums;
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
}