Cow Contest_poj3660_floyd

来源:互联网 发布:肖申克的救赎知乎影评 编辑:程序博客网 时间:2024/06/05 01:00

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 competitors.

The contest is conducted in several head-to-head rounds, each between two cows. If cow A has a greater skill level than cow B (1 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ B), then cow A will always beat cow B.

Farmer John is trying to rank the cows by skill level. Given a list the results of M (1 ≤ M ≤ 4,500) two-cow rounds, determine the number of cows whose ranks can be precisely determined from the results. It is guaranteed that the results of the rounds will not be contradictory.

Input


  • Line 1: Two space-separated integers: N and M
  • Lines 2..M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer, A, is the winner) of a single round of competition: A and B

Output


  • Line 1: A single integer representing the number of cows whose ranks can be determined

Analysis


跟以前做过的一道珍珠很像但是我脑胡了(┬_┬)
大概思路就是floyd,然后看看比这个点大和比这个点小的是不是刚好n-1个,因为要算上本身所以-1
一开始想的是拓扑然后看每次是不是只入队了1个,但是后面发现这样不能保证唯一

Code


/*ID:wjp13241PROG:cow conestLANG:C++*/#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <iostream>#include <vector>#include <cmath>#include <stack>#include <queue>#define fo(i,a,b) for(int i=a;i<=b;i++)#define dfo(i,a,b) for(int i=a;i>=b;i--)#define fore(i,x,e) for(int i=ls[x];i;i=e[i].next)#define fil(x,t) memset(x,t,sizeof(x))#define STP system("pause")#define min(x,y) x<y?x:y#define max(x,y) x>y?x:y#define PuB(v,x) v.push_back(x)#define PoB(v) v.pop_back()#define ld long double#define ll long long#define db double#define INF 0x3f3f3f3f#define LIM 100000000#define EPS 1e-4#define N 201#define E N*N+1using namespace std;int f[N][N];int main(){    ios::sync_with_stdio(false);    int n,m;    cin>>n>>m;    fo(i,1,m)    {        int x,y;        cin>>x>>y;        f[x][y]=1;        f[y][x]=-1;    }    fo(k,1,n)        fo(i,1,n)            fo(j,1,n)                if (i^j&&i^k&&j^k)                {                    if (f[i][k]==1&&f[k][j]==1)                        f[i][j]=1;                    else                        if (f[i][k]==2&&f[k][j]==2)                            f[i][j]=2;                }    int ans=0;    fo(i,1,n)    {        int a=0,b=0;        fo(j,1,n)        {            if (f[i][j]==1)                a++;            if (f[i][j]==2)                b++;        }        // if (a>=(n+1)/2||b>=(n+1)/2)        if (a+b+1==n)            ans++;    }    cout<<ans<<endl;    return 0;}
1 0
原创粉丝点击