hdu 1704 Rank (传递闭包)

Rank

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


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.
ps:此题貌似是传递闭包 反正我用搜索和floyd()做不了 (用floyd()会超时) 不过其实传递闭包就是借用的floyd()思想
#include<stdio.h>
#include<math.h>
#include<string.h>
#define maxn 505

int city[maxn][maxn];
int n,m,ans;

void floyd()    // 改良版 变为传递闭包
{
    int i,j,k;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=n; j++)
        {
            if(city[j][i])     // 如果j,i 连通  如果这里变为city[i][j] 你会发现会WA
            // 因为会会漏掉很多win关系, 主要是历遍的顺序不一样造成的 不信的话不妨试一下我贴的数据
            {
                for(k=1; k<=n; k++)
                {
                    if(city[i][k]) city[j][k]=1;   // 如果i,k连通 则j,k连通
                }
            }
        }
    }
}
void solve()
{
    int i,j;
    ans=0;
    for(i=1; i<=n; i++)
    {
        for(j=i+1; j<=n; j++)
        {
            if(!city[i][j]&&!city[j][i]) ans++;
        }
    }
}
int main()
{
    int i,j,t,l,r;
    scanf("%d",&t);
    while(t--)
    {
        memset(city,0,sizeof(city));
        scanf("%d%d",&n,&m);
        for(i=1; i<=m; i++)
        {
            scanf("%d%d",&l,&r);
            city[r][l]=1;
        }
        floyd();
        solve();
        printf("%d\n",ans);
    }
    return 0;
}
/*
这组数据可以测试第16行为什么是city[j][i]
1
8 8
1 2
2 3
3 4
4 5
6 5
7 5
8 7
4 8
*/







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值