Rank
64-bit integer IO format: %I64d Java class name: Main
ZJPCPC Sunny Cup 2007 is coming, and lcy want to select some excellent ACMers to attend the contest. There have been M matches since the last few days(No two ACMers will meet each other at two matches, means between two ACMers there will be at most one match). lcy also asks"Who is the winner between A and B?" But sometimes you can't answer lcy's query, for example, there are 3 people, named A, B, C.and 1 match was held between A and B, in the match A is the winner, then if lcy asks "Who is the winner between A and B", of course you can answer "A", but if lcy ask "Who is the winner between A and C", you can't tell him the answer.
As lcy's assistant, you want to know how many queries at most you can't tell lcy(ask A B, and ask B A is the same; and lcy won't ask the same question twice).
Input
The first line has one integer,represent the number of test cases.
Each case first contains two integers N and M(N , M <= 500), N is the number of ACMers in HDU team, and M is the number of matchs have been held.The following M lines, each line means a match and it contains two integers A and B, means A wins the match between A and B.And we define that if A wins B, and B wins C, then A wins C.
Output
Sample Input
3 3 3 1 2 1 3 2 3 3 2 1 2 2 3 4 2 1 2 3 4
Sample Output
0 0 4
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 510; 18 int n,m; 19 bool mp[maxn][maxn]; 20 void Floyd(){ 21 for(int k = 1; k <= n; k++){ 22 for(int i = 1; i <= n; i++){ 23 for(int j = 1; mp[i][k]&&j <= n; j++) 24 mp[i][j] |= mp[i][k]&mp[k][j]; 25 } 26 } 27 } 28 int main() { 29 int t,u,v,ans; 30 scanf("%d",&t); 31 while(t--){ 32 scanf("%d %d",&n,&m); 33 memset(mp,false,sizeof(mp)); 34 for(int i = 0; i < m; i++){ 35 scanf("%d %d",&u,&v); 36 mp[u][v] = true; 37 } 38 Floyd(); 39 ans = 0; 40 for(int i = 1; i <= n; i++){ 41 for(int j = i+1; j <= n; j++) 42 if(mp[i][j] == 0 && mp[j][i] == 0) ans++; 43 } 44 printf("%d\n",ans); 45 } 46 return 0; 47 }