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

时间: 2024-10-14 14:24:57

floyd 传递闭包 POJ - 3660的相关文章

POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】

Treasure Exploration Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2594 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploratio

POJ3660:Cow Contest(Floyd传递闭包)

Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16941   Accepted: 9447 题目链接:http://poj.org/problem?id=3660 Description: N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all k

poj 3660 Cow Contest(warshall算法)

poj 3660 Cow Contest Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the co

UVA 247 电话圈 (floyd传递闭包 + dfs输出连通分量)

题意:输出所有的环: 思路:数据比较小,用三层循环的floyd传递闭包(即两条路通为1,不通为0,如果在一个环中,环中的所有点能互相连通),输出路径用dfs,递归还没有出现过的点(vis),输出并递归该点与其他点能互达的点: 1 #include <cstdio> 2 #include <vector> 3 #include <string> 4 #include <cstring> 5 #include <iostream> 6 using n

hdu 1704 Rank(floyd传递闭包)

题目链接:hdu 1704 Rank 题意: 有n个人,m场比赛,a与b打,每场都是awin,问不能确定其中两个人的win情况数. 题解: floyd传递闭包,这里我用bitset优化了一下. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 bitset<501>a[501]; 5 6 int n,m,t; 7 8 int main(){ 9

POJ 3660 (Floyd判断传递闭包)

题目:输入n头牛,m个关系.接下来m行每行两个int数a,b,代表a可以打败b 问:能确定多少头牛的排名 思路:floyd算法可以判断传递闭包问题(通过传递性推导出尽量多的元素之间的关系叫做传递闭包),模板题 #include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <string> #include <map> #includ

Cow Contest POJ - 3660 floyd传递闭包

#include<iostream> #include<cstring> using namespace std; const int N=110,INF=0x3f3f3f3f; int f[N][N]; int main() { int n,m; cin>>n>>m; memset(f,0x3f,sizeof f); int x,y; for(int i=0;i<m;i++) { cin>>x>>y; //x>y f[x

POJ 1094 Sorting It All Out【floyd传递闭包+拓扑排序】

Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31994 Accepted: 11117 Description An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from small

ACM: POJ 3660 Cow Contest - Floyd算法

链接 Cow Contest Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Eac