luogu题解 P2419 【牛大赛Cow Contest】传递丢包

  • 题目链接:

https://www.luogu.org/problemnew/show/P2419

  • 分析:
“在交际网络中,给定若干元素和若干对二元关系,且关系具有传递性。

 通过传递性推导出尽量多元素之间的关系的问题叫做传递丢包”
 
                                       --《算法竞赛进阶指南》

所以这道题就用传递丢包来做,怎么实现呢?用Floyd

\(f[x][y]\)表示\(x>y\)的关系

最后判断一下对于一个元素\(x\),是不是其他\(n-1\)个元素都与它有传递关系,如果是的话,那么它的位置自然也就确定了

  • 代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <vector>
using namespace std;
const int maxn=105;
int n,m;
int f[maxn][maxn];
bool vis[maxn][maxn];
int main(){
    int x,y;
    scanf("%d %d",&n,&m);
    for(register int i=1;i<=m;i++){
        scanf("%d %d",&x,&y);
        f[x][y]=1;  //x>y;
    }
    for(register int k=1;k<=n;k++){
        for(register int i=1;i<=n;i++){
            for(register int j=1;j<=n;j++){
                f[i][j]|=f[i][k]&f[k][j];
            }
        }
    }
    int ans=0;
    for(register int i=1;i<=n;i++){
        int cnt=0;
        for(register int j=1;j<=n;j++){
            if(i==j)continue;
            if(f[i][j]||f[j][i])cnt++;
        }
        if(cnt==n-1)ans++;
    }
    cout<<ans;
    return 0;
}
  • 进阶题:

POJ 1094

猜你喜欢

转载自www.cnblogs.com/Rye-Catcher/p/9122641.html
今日推荐