hdu 1704 (Floyd 传递闭包)

Rank

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


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
 
给出n人的m对关系图,a b表示a能战胜b,战胜有传递性,即(a,b)(b,c)--->(a,c)
mdzz一开始bfs跑一波,双向建图,后来发现不服对,假如(1,5)(1,6),5-6是没关系的,但由于双向建图反而有了关系,所以WA。
不如(a,b)时,e[a][b]=1表示a战胜b,e[b][a]=-1,表示b输给了a,接着Floyd跑一波,最后统计。
#include<bits/stdc++.h>
using namespace std;
int e[505][505];
int main()
{
    int t,n,m,i,j,k;
    cin>>t;
    while(t--){int s=0,a,b;
        cin>>n>>m;memset(e,0,sizeof(e));
        for(i=1;i<=m;++i){
            cin>>a>>b;
            e[a][b]=1;
            e[b][a]=-1;
        }
    for(k=1;k<=n;++k)
        for(i=1;i<=n;++i)
if(e[i][k]==1){
    for(j=1;j<=n;++j)
        if(e[k][j]==1)  e[i][j]=1,e[j][i]=-1;

}
    for(i=1;i<=n;++i)
    for(j=1+i;j<=n;++j){
        if(!e[i][j]) s++;
    } cout<<s<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zzqc/p/6729293.html