Luogup3183 [haoi2016] Food Chain memory-based search

Source: Internet
Author: User
Description

For the food network of an ecosystem, according to Figure 1st, I am now giving you a relationship between N species and M energy flows, asking for the number of food chains. The name of the species is from 1 to n, and m pieces of energy flow are like A1 b1a2 b2a3 b3 ...... am-1 bm-1am BM where Ai Bi indicates that energy flows from species AI to species Bi, note that a separate isolated creature is not a food chain

Input/Output Format

Input Format:

 

The first line has two integers, N and M. The next line has two integers, AI Bi, to describe the energy flow relationship of M. (Data ensures the biological characteristics of input data symbols without repeated energy flow relationships) 1 <= n <= 100000 0 <= m <= 200000 questions ensure that the answer will not blow up int

 

Output Format:

 

An integer is the number of food chains in the Food Network.

 

Input and Output sample input sample #1: Copy
10 161 21 41 102 32 54 34 54 86 57 67 98 59 810 610 710 9
Output example #1: Copy
9
Question

Mentality collapsed... this kind of questions can be written down...

Directly calculates the degree of input, and the memory-based search is enough.

At the end of each food chain, there must be no outbound or inbound content. At the beginning, there must be an outbound or non-Inbound content.

#include <bits/stdc++.h>#define ll long long#define inf 0x3f3f3f3f #define il inline #define in1(a) a=read()#define in2(a,b) in1(a),in1(b)#define in3(a,b,c) in2(a,b),in1(c)#define in4(a,b,c,d) in2(a,b),in2(c,d)#define out(a) printf( "%d" , a ) #define outn(a) out(a),putchar(‘\n‘)#define I_int int inline I_int read() {        I_int x = 0 , f = 1 ; char c = getchar() ;    while( c < ‘0‘ || c > ‘9‘ ) {        if( c == ‘-‘ ) f = -1 ;        c = getchar() ;    }    while( c >= ‘0‘ && c <= ‘9‘ ) {        x = (x << 1) + (x << 3) + c - 48 ;        c = getchar() ;    }    return x * f ;}#undef I_intusing namespace std ;#define N 100010int n , m ;int in[ N ] , out[ N ] ;int head[ N ] , cnt ;struct node {    int to , nxt ;} e[ N << 1 ] ;void ins( int u , int v ) {    e[ ++ cnt ].to = v ;    e[ cnt ].nxt = head[ u ] ;    head[ u ] = cnt ;}int f[ N ] ;int dfs( int x ) {    if( f[ x ] ) return f[ x ] ;    int ans = 0 ;    if( !out[ x ] && in[ x ] ) ans ++ ;    for( int i = head[ x ] ; i ; i = e[ i ].nxt ) {        ans += dfs( e[ i ].to ) ;    }    f[ x ] = ans ;    return ans ; }int main() {    in2( n , m ) ;    for( int i = 1 , u , v ; i <= m ; i ++ ) {        in2( u , v ) ; ins( u , v ) ;        in[ v ] ++ ; out[ u ] ++ ;    }    int ans = 0 ;    for( int i = 1 ; i <= n ; i ++ ) {        if( !in[ i ] ) ans += dfs( i ) ;    }    outn( ans ) ;}

 

Luogup3183 [haoi2016] Food Chain memory-based search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.