Problem Description
there are N ACMers in HDU team.
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 input contains multiple test cases.
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
For each test case, output a integer which represent the max possible number of queries that you can't tell lcy.
 
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

Hint

in the case3, if lcy ask (1 3 or 3 1) (1 4 or 4 1) (2 3 or 3 2) (2 4 or 4 2), then you can't tell him who is the winner.

 
 
大意:给出M对胜负关系,胜负关系有传递性(若A胜B,B胜C则A胜C),求有多少对不能确定的胜负关系
 
解法:思路很简单,floyd 一遍 做传递闭包,然后暴力枚举就行辣,但是竟然会TLE,然后上网学了一种新的优化姿势(其实这种优化用处不大,但由于本题是非常稀疏的图,所以O(N^3)几乎变成了O(N^2))
 
优化方法详见主函数里的floyd
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
int read(){
int xx=0,ff=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
return xx*ff;
}
const int maxn=510;
int T,N,M,ans;
bool e[maxn][maxn];
int main(){
//freopen("in","r",stdin);
//freopen("out","w",stdout);
T=read();
while(T--){
N=read(),M=read();
memset(e,0,sizeof(e));
for(int i=1;i<=M;i++)
e[read()][read()]=1;
for(int k=1;k<=N;k++)
for(int i=1;i<=N;i++)
if(e[i][k])
for(int j=1;j<=N;j++)
e[i][j]|=e[k][j];
ans=0;
for(int i=1;i<=N;i++)
for(int j=i+1;j<=N;j++)
if(e[i][j]|e[j][i])
ans++;
printf("%d\n",N*(N-1)/2-ans);
}
return 0;
}

  

 

最新文章

  1. H5坦克大战之【画出坦克】
  2. ASP.NET MVC5+EF6+EasyUI 后台管理系统--系统模块部分图
  3. JQuery源码分析(七)
  4. iOS Bluetooth Reconnect
  5. ip地址+进程端口号+路径参数同样实现restful访问
  6. Linux下的paste合并命令详解
  7. LINUX CACHE IO THREAD
  8. jQuery基础选择器
  9. hibernate-mapping的各种属性配置
  10. php之购物车类思路及代码
  11. 【C语言】结构组成(函数、语句、注释)
  12. Linux下静态编译Qt
  13. HDU5832
  14. 原生封装ajax
  15. 分别用face++和百度获取人脸属性(python单机版)
  16. 关于WinSock编程的多线程控制
  17. Linux时间戳转换成BCD码(转载)
  18. 模拟poj1350
  19. redis常用命令及结构
  20. TCP/IP协议详解内容总结(怒喷一口老血)

热门文章

  1. 浅谈animation里的forwards
  2. C++入职学习篇--代码规范(持续更新)
  3. enote笔记语言(4)(ver0.2)——“5w1h2k”分析法
  4. CODEVS 3500
  5. HDU 3401 Trade
  6. 洛谷 P2155 BZOJ 2186 codevs 2301 [SDOI2008]沙拉公主的困惑
  7. win7安装gmpy2
  8. CODEVS1281 Xn数列 (矩阵乘法+快速乘)
  9. 造成segment fault,产生core dump的可能原因
  10. [bzoj3126][USACO2013]Photo_动态规划_单调队列