解题思路:给出n个选手,m场比赛,问不能判断胜负的询问最多有多少种

用传递闭包即可 但是如果直接用3重循环会超时 在判断d[i][j]=d[i][k]||d[k][j]是否连通的时候 可以加一个if语句判断一下d[i][k]是否为1,为1再进行第三重循环,不为1则不进行第三次循环

反思:例如询问 3和1,1和3是相同的情况,所以最后判断的时候,只需要判断上三角矩阵即可。

Rank

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1134    Accepted Submission(s): 429

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.

#include<stdio.h>
#include<string.h>
int d[505][505],a[505][505];
int main()
{
int ncase,n,m,i,j,k,sum,x,y;
scanf("%d",&ncase);
while(ncase--)
{
memset(d,0,sizeof(d));
sum=0;
scanf("%d %d",&n,&m);
while(m--)
{
scanf("%d %d",&x,&y);
d[x][y]=1;
} for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
{
if(d[i][k])
for(j=1;j<=n;j++)
d[i][j]=d[i][j]||(d[k][j]);
} for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
if(!(d[i][j]||d[j][i]))
sum++;
printf("%d\n",sum);
}
}

  

最新文章

  1. Android—自定义控件实现ListView下拉刷新
  2. CoreData
  3. Java,Android 项目导入Eclipse常见错误
  4. AC日记——石头剪子布 openjudge 1.7 04
  5. 【leetcode】Edit Distance
  6. 简单的bat命令去获取wsdl的源码
  7. POJ 1321棋盘问题
  8. code forces Watermelon
  9. java——String的那边破事
  10. 关于caffe-windows中 compute_image_mean.exe出现的问题
  11. openresty 前端开发轻量级MVC框架封装二(渲染篇)
  12. Servlet小总结(转)
  13. scala攻略--简介
  14. AI 新技术革命将如何重塑就业和全球化格局?深度解读 UN 报告(上篇)
  15. c++指针函数的使用——回调函数
  16. 【一天一道LeetCode】#19. Remove Nth Node From End of List
  17. [ 高危 ] my存在sql注入
  18. centos7如何安装部署Zabbix
  19. lientDataset的Delta与XML相互转换
  20. BTA 常问的 Java基础40道常见面试题及详细答案

热门文章

  1. css盒模型:BFC、IFC边距重叠解决方案
  2. Android宝宝点点乐游戏源码完整版
  3. oracle导入导出操作
  4. depth peeling实现半透明
  5. Eclipse中重置(还原)GIT分支
  6. Python测试(二)
  7. js 正则表达式 整合
  8. com.jasson.im.api.APIClient jar包 下载
  9. springboot---web 应用开发-文件上传
  10. 首页 &gt; 所有书 &gt; 操作系统 &gt; Linux &gt;