floyd poj 3660 Cow Contest

Determining the rank order problem


dfs : By traversing each vertex forward and reverse (reverse storage graph) respectively, the sum of the out-degree and the in-degree of the vertex can be calculated. If D in + D out == N-1, its ranking can be determined. See discrete mathematics.

floyd: relation[i][j] = 1 or relation[j][i] = 1 means there is a relationship between i and j. If a vertex is related to the other n-1 vertices, its ranking is determined. Floyd's algorithm can be used to find whether there is a relationship between any two vertices.

Note that it is a directed graph

#include <iostream>
#include <memory.h>
using namespace std;
int G[105][105];
int Indegree[105];
int Outdegree[105];
int N,M;
void Floyd()
{
    for( int u = 1; u <= N; u++ )
        for( int v = 1; v <= N; v++ )
            for( int w = 1; w <= N; w++ )
            {
                if( G[v][u] && G[u][w] )
                {
                    G[v][w] = 1;
                }
            }
}
intmain()
{
    cin >> N >> M;
    memset(G, 0, sizeof(G));
    memset(Outdegree, 0, sizeof(Outdegree));
    memset(Indegree, 0, sizeof(Indegree));
    while( M-- )
    {
        int Begin,End;
        cin >> Begin >> End;
        G[Begin][End] = 1;
    }
    Floyd();
    for( int i = 1; i <= N; i++ )
        for( int j = 1; j <= N; j++ )
    {
        if( G[i][j] )
        {
        Indegree[j]++;
        Outdegree[i]++;
        }
    }
    int num = 0;
    for( int i = 1; i <= N; i++ )
    {
        if( Indegree[i] + Outdegree[i] == N-1 )
            num++;
    }
    cout << num << endl;
    return 0;
}




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325992041&siteId=291194637