POJ - 3660 Cow Contest (floyd, transitive closure)

Meaning of the questions: There are N number of cows, M comparators, ab a landmark name can beat b, ask ultimately determine the number of cows ranking is how many
ideas: from the perspective of the sort we certainly will first think of topological sorting, but topology sorting can only wonder if the only sort can not be obtained
to determine the ordering number
so for an idea if a point has the relationship between all the other points and then the point will be able to determine rankings
which involves the closure of the transfer operation, the use of floyd algorithm

 

Complete code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 105;
const int maxm = 1e4;
const int inf = 0x3f3f3f3f;
int g[maxn][maxn]; 
int n,m;
int floyd(){
    int cnt = 0;
    for(int i =1;i<=n;i++)
        for(int k = 1;k<=n;k++)
            for(int j=1;j<=n;j++){
                if(g[k][i]&&g[i][j]) g[k][j] = 1;//传递关系 
            }
    int tmp = 0;
    for(int i=1;i<=n;i++){
        for(int k=1;k<=n;k++){
            if(k==i) continue;
            if(g[i][k]||g[k][i]) tmp++;//确定是否有联系
        }
        if(tmp == n-1) cnt++;
        tmp = 0;
    }
    return cnt;
}
int main(){
    while(cin>>n>>m){
        memset(g,0,sizeof(g));
        for(int i=0;i<m;i++){
            int u ,v;
            cin>>u>>v;
            g [u] [v] = 1 ;
        }
        cout<<floyd()<<endl;
    }
}

 

Guess you like

Origin www.cnblogs.com/Tianwell/p/11288856.html