HDU 1704 Rank

Rank

Time Limit: 1000ms

Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 1704
64-bit integer IO format: %I64d      Java class name: Main

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.

Source

2007省赛集训队练习赛(1)

解题:求有多少组关系没法确定。无相图传递闭包。

 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 }

时间: 2024-08-09 00:19:28

HDU 1704 Rank的相关文章

hdu 1704 Rank(floyd传递闭包)

题目链接:hdu 1704 Rank 题意: 有n个人,m场比赛,a与b打,每场都是awin,问不能确定其中两个人的win情况数. 题解: floyd传递闭包,这里我用bitset优化了一下. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 bitset<501>a[501]; 5 6 int n,m,t; 7 8 int main(){ 9

HDU 1704 Rank【传递闭包】

解题思路:给出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: 32

hdu - 1704 Rank(简单dfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1704 遇到标记过的就dfs,把隐含的标记,最后计数需要注意. 1 #include <cstdio> 2 #include <cstring> 3 int n; 4 int vis[501][501]; 5 void dfs(int x,int y) 6 { 7 for(int i=1;i<=n;i++) 8 if(vis[y][i]) 9 { 10 vis[x][i]=1; 11 dfs

HDU 1718 Rank counting sort解法

本题是利用counting sort的思想去解题. 注意本题,好像利用直接排序,然后查找rank是会直接被判WA的,奇怪的判断系统. 因为分数值的范围是0到100,非常小,而student 号码又非常大,故此天然的需要利用counting sort的情况. #include <stdio.h> #include <string.h> const int MAX_N = 101; int arr[MAX_N]; int main() { int Jackson, JackScore,

hdu 1811 Rank of Tetris (拓扑排序+并查集)

Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4931 Accepted Submission(s): 1359 Problem Description自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他

HDU 1811 Rank of Tetris(并查集按秩合并+拓扑排序)

Rank of Tetris Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9267    Accepted Submission(s): 2668 Problem Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想

hdu 1811 Rank of Tetris(拓扑排序+并查集)

1 #include "cstdio" 2 #include "iostream" 3 #include "cstring" 4 #include "vector" 5 #include "queue" 6 using namespace std; 7 const int N = 10005; 8 int n, m, t; 9 int fa[N]; 10 int rank[N]; 11 int X[2*N]

hdu 1811 Rank of Tetris 并查集+拓扑排序,,提高题

Rank of Tetris Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5672    Accepted Submission(s): 1616 Problem Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想

HDU 2643 Rank:第二类Stirling数

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2643 题意: 有n个个选手参赛,问排名有多少种情况(可以并列). 题解: 简化问题: 将n个不同的元素放到i个有差别的盒子中,情况数为P(n,i),求∑P(n,i) (1<=i<=n) 再简化: 将n个不同的元素放到i个无差别的盒子中,情况数为S(n,i),求∑( S(n,i)*i! ) (1<=i<=n) 哇这是第二类Stirling数 ( ̄▽ ̄)~* 递推式:s(n,k) = s(