floyd transitive closure POJ - 3660

https://vjudge.net/problem/POJ-3660

 

Transitive closure, is to transfer transitive apart relationship by side and even some known relationship is obtained between the dots.

Set f [i] [j] indicates whether the communication with i j, f [i] [j] = f [i] [k] && f [k] [j]

Then analyzes each point, if we can determine the n-1 relationship, it can determine his ranking.

The time complexity of O (N ^ 3)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <algorithm>
#include <queue>
#define INF (1<<30)
using namespace std;

const int maxn=4507;
int g[maxn][maxn];
int n,m;
int main(){
    scanf("%d%d",&n,&m);
    int x,y;
    while(m--){
        scanf("%d%d",&x,&y);
        g[x][y] = 1;
    }
    for(int k=1; k<=n; k++){
        for(int i=1; i<=n; i++){
            for(int j=1; j<=n; j++){
                // g[i][j] = max( g[i][j],(g[i][k] & g[k][j]) );
                if(g[i][k]&&g[k][j]){
                    g[i][j] = 1;
                }
            }
        }
    }
    int ans=0;
    for(int i=1; i<=n; i++){
        int cnt=0;
        for(int j=1; j<=n; j++){
            if(g[i][j] || g[j][i]){
                cnt++;
            }
        }
        if(cnt==(n-1)) { 
            Years ++ ; 
        } 
    } 
    Printf ( " % d \ n " , year); 
}

 

Guess you like

Origin www.cnblogs.com/-Zzz-/p/11470422.html