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;
}

  

 

hdu1704——floyd的更多相关文章

  1. floyd算法学习笔记

    算法思路 路径矩阵 通过一个图的权值矩阵求出它的每两点间的最短路径矩阵.从图的带权邻接矩阵A=[a(i,j)] n×n开始,递归地进行n次更新,即由矩阵D(0)=A,按一个公式,构造出矩阵D(1):又 ...

  2. 最短路(Floyd)

    关于最短的先记下了 Floyd算法: 1.比较精简准确的关于Floyd思想的表达:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B.所以,我们假设maz ...

  3. 最短路径之Floyd算法

    Floyd算法又称弗洛伊德算法,也叫做Floyd's algorithm,Roy–Warshall algorithm,Roy–Floyd algorithm, WFI algorithm. Floy ...

  4. UVALive 4431 Fruit Weights --floyd,差分约束?

    题意: 给出一些关系用aX <= bY表示, 最后查询aX 和 bY的关系,是>=,==,<=,还是不能确定,还是出现了矛盾. 解法:对每一个关系其实都可以建一条X->Y的边, ...

  5. 洛谷P1119 灾后重建[Floyd]

    题目背景 B地区在地震过后,所有村庄都造成了一定的损毁,而这场地震却没对公路造成什么影响.但是在村庄重建好之前,所有与未重建完成的村庄的公路均无法通车.换句话说,只有连接着两个重建完成的村庄的公路才能 ...

  6. UVA10048 Audiophobia[Floyd变形]

    UVA - 10048 Audiophobia Consider yourself lucky! Consider yourself lucky to be still breathing and h ...

  7. 最短路径—Dijkstra算法和Floyd算法

    原文链接:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html 最后边附有我根据文中Dijkstra算法的描述使用jav ...

  8. 最短路径问题——floyd算法

    floyd算法和之前讲的bellman算法.dijkstra算法最大的不同在于它所处理的终于不再是单源问题了,floyd可以解决任何点到点之间的最短路径问题,个人觉得floyd是最简单最好用的一种算法 ...

  9. 几大最短路径算法比较(Floyd & Dijkstra & Bellman-Ford & SPFA)

    几个最短路径算法的比较:Floyd 求多源.无负权边(此处错误?应该可以有负权边)的最短路.用矩阵记录图.时效性较差,时间复杂度O(V^3).       Floyd-Warshall算法(Floyd ...

随机推荐

  1. 如何设置“用eclipse开发时自动在顶端产生import”?

    eclipse新版本中的默认设置可能导致自动在生成import时是在代码中,而大多数时间我们需要自动生成import在代码顶端,按照如下设置即可纠正

  2. VS2013支持多字节

    使用插件 下载地址:https://www.microsoft.com/zh-cn/search/DownloadResults.aspx?rf=sp&q=mbcs

  3. 【sqli-labs】 less50 GET -Error based -Order By Clause -numeric -Stacked injection(GET型基于错误的整型Order By从句堆叠注入)

    报错没有关闭,直接可以用UpdateXml函数 http://192.168.136.128/sqli-labs-master/Less-50/?sort=1 and UpdateXml(1,conc ...

  4. Django中图片的上传并显示

    一.settings配置文件中配置 MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'medias').replace('\\', ...

  5. 基于python xlsxwriter、xlrd 生成测试报告

    import xlsxwriter,xlrd ''' 思路: 1.获取数据 2.整合数据 3.写入文件 ''' #筛选 def filt(category,table,filt_name=None,r ...

  6. C#当中的out关键字(借鉴于CSDN)

    一丶与ref关键字一样,out关键字也是按引用来传递的.out 关键字会导致参数通过引用来传递.这与 ref 关键字类似,不同之处在于 ref 要求变量必须在传递之前进行初始化.若要使用 out 参数 ...

  7. openstack——horizon篇

    一.horizon 介绍:       理解 horizon   Horizon 为 Openstack 提供一个 WEB 前端的管理界面 (UI 服务 )通过 Horizone 所提供的 DashB ...

  8. list.h在用户态下的应用

    一.背景 list.h文件位于linux/include下,内核中链表的操作函数都在其中.它有许多关于链表的操作函数,所以我们可以尝试将list.h拉到用户态中来使用,这样,我们在用户态中若要用到链表 ...

  9. pip/pip3国内源

    Error 在使用pip3安装PySide2时出现ReadTimeoutError. $ pip3 install PySide2 Solution 使用国内源 例如: $ pip3 install ...

  10. python的转义字符及用法详解

    在需要在字符中使用特殊字符时,python用反斜杠(\)转义字符. Python3.7 的官方文档中列出的所支持的转义字符,如下表: 中文版: 注:\oyy 应该是 \0yy  另外 :\e 和 \0 ...