poj 3660Cow Contest

题目链接:http://poj.org/problem?id=3660

有n头奶牛还有m种关系a,b表示a牛逼b彩笔,所以a排名比b高

最后问你给出的关系最多能确定多少头奶牛的排名,而且给出的数据不会有矛盾

 

其实就是给出了一个无环的有向图,只要有一点他能跟所有点有联系那么这个点的排名就知道了。

可以用一下floyd的思想还是能简单实现的。

其实这题简单来说就是能走完所有点的路都经过哪些点,最容易想到的就是floyd求多源点的最短路

然后稍微改一下就好了。

 

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int n , m , a , b , mmp[110][110] , In[110] , Out[110] , counts;
bool vis[110][110];
void bfs() {
    memset(vis , false , sizeof(vis));
    for(int k = 1 ; k <= n ; k++) {
        for(int i = 1 ; i <= n ; i++) {
            for(int j = 1 ; j <= n ; j++) {
                if(mmp[i][k] != 0 && mmp[k][j] != 0) {
                    if(!vis[i][j]) {
                        In[j]++;
                        Out[i]++;
                        vis[i][j] = true;
                        mmp[i][j] = 1;
                    }
                }
            }
        }
    }
    for(int i = 1 ; i <= n ; i++) {
        //cout << i << ' ' << In[i] << ' ' << Out[i] << endl;
        if(In[i] + Out[i] == n + 1) {
            counts++;
        }
    }
}
int main() {
    cin >> n >> m;
    for(int i = 1 ; i <= n ; i++) {
        In[i] = 0 , Out[i] = 0;
        for(int j = 1 ; j <= n ; j++) {
            mmp[i][j] = 0;
        }
        mmp[i][i] = 1;
    }
    for(int i = 1 ; i <= m ; i++) {
        cin >> a >> b;
        mmp[a][b] = 1;
    }
    counts = 0;
    bfs();
    cout << counts << endl;
    return 0;
}
posted @ 2017-03-08 21:48  Gealo  阅读(144)  评论(0编辑  收藏  举报