The maximum count of the food chain topological sorting P4017

Topic background

You know the food chain it? Delia biological exam, the subject of counting the food chain all wrong, because she always repeated a few or several missing a few. So she come and help you, but you will not ah! Write a program to help her.

Title Description

Give you a food web, you asked a number of the largest food chain in the food web.

(Here the "biggest food chain", referring to the food chain in the biological sense , that is, the left-most producers are not prey on other species that most consumers will not be right is predation by other organisms .)

Delia is very urgent, so you have only one second.

Because of this result may be too large, you only need the total output of 80,112,002 mold.

Input Format

The first line, two positive integers n, m, and n represents a species of number m of the relationship between food eaten.

Next m lines of two positive integers, A represents eaten and eating A biological organisms B.

Output Format

Line an integer, the result of the food chain, the maximum number of 80,112,002 mold.

Entry
5 7
1 2
1 3
2 3
3 5
2 5
4 5
3 4
Export
5 

Description / Tips

Each test point satisfy the following convention:

 

[Supplement]

Central data does not appear to meet the requirements biology. (Thanks @AKEE)

 answer:

Topological sort O (m) statistics.

Can not eat from someone else (0-degree) begins, denoted by 1, constantly updated, every pass to the next, let the right point value plus the value passed right past this point.

Finally reached the point of all is not out of the food chain to meet the maximum.

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
const int N = 5e3 + 5, mod = 80112002;
int n, m, indeg[N], outdeg[N], a, b, f[N], ans;
int map[N][N];
queue <int> q;
int main(){
    cin >> n >> m;
    for(int i = 1;i <= m;i ++){
        scanf("%d%d", &a, &b);
        map[a][b] = 1; outdeg[a] ++; indeg[b] ++;
    }
    for(int i = 1;i <= n;i ++) if(indeg[i] == 0) f[i] = 1, q.push(i);
    while(! q.empty()){
        int tp = q.front(); q.pop();
        for(int to = 1;to <= n;to ++){
            if(map[tp][to] == 0)continue;
            (f[to] += f[tp]) %= mod;
            indeg[to] --;
            if(indeg[to] == 0) q.push(to);
            if(indeg[to] == 0&&outdeg[to] == 0) {
                (ans += f[to]) %= mod;
                continue;
            }
                
        }
    }
    cout << ans; 
}

 

Guess you like

Origin www.cnblogs.com/Paranoid-LS/p/11246445.html