floyd 传递闭包 POJ - 3660

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

 

传递闭包 ,就是把具有传递性的关系传递开,通过一些已知的连边求出点与点之间的关系。

设f[i][j]表示i 与 j 是否联通,f[i][j]=f[i][k]&&f[k][j]

再分析每个点,如果能确定 n-1 个关系,那就可以确定他的排名。

时间复杂度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)){
            ans++;
        }
    }
    printf("%d\n", ans);
}

 

转载于:https://www.cnblogs.com/-Zzz-/p/11470422.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值