Zoj 3795 grouping

Source: Internet
Author: User
Grouping Time Limit: 2 seconds memory limit: 65536 KB

Suppose there areNPeople in zju, whose ages are unknown. We have some messages about them.I-Th message shows that the age of personSiIs not smaller than the age of personTi. Now we need to divide all theseNPeople into several groups. one's age shouldn't be compared with each other in the same group, directly or indirectly. and everyone shoshould be assigned to one and only one group. the task is to calculate the minimum number of groups that meet the requirement.

 

Input

There are multiple test cases. For each test case: the first line contains two integersN(1 ≤N≤ 100000 ),M(1 ≤M≤ 300000 ),NIs the number of people, andMIs is the number of messages. Then followedMLines, each line contain two integers Si AND Ti. There is a blank line between every two cases. process to the end of input.

Output

For each the case, print the minimum number of groups that meet the requirement one line.

Sample Input
4 41 21 32 43 4
Sample output
3
Hint

Set1 = {1}, set2 = {2, 3}, set3 = {4}

 

The question contains N points and m edges, and then asks some associated persons (with a path U-> V) which cannot be divided into the same group, with the minimum number of groups.

In this case, we must require an SCC .. then all the people (assuming W people) in the same SCC will be split into W groups, so we will use this w

The vertex weight of the SCC diagram. The objective is to obtain the maximum permission of the longest path of all SCC graphs (DAG.

The longest path is to use a memory-based search. It won't be used during training ~~ .

 

 

#include <bits/stdc++.h>using namespace std;const int N = 100010;int pre[N] , lowlink[N] , sccno[N] , dfs1_clock , scc_cnt ;stack<int>st;int n , m ;int w[N] , dep_w[N];bool vis[N];int in[N];int dp[N];int eh[N] , et[N<<2] ,nxt[N<<2] , tot ;void addedge( int u , int v ){    et[tot] = v , nxt[tot] = eh[u] , eh[u] = tot ++ ;}struct node{    int u , v ;}e[N*3];void dfs1( int u ){    pre[u] = lowlink[u] = ++dfs1_clock;    st.push(u);    for( int i = eh[u]; ~i ; i = nxt[i] ){        int v = et[i] ;        if(!pre[v]){            dfs1(v);            lowlink[u] = min(lowlink[u],lowlink[v]);        }        else if( !sccno[v] ){            lowlink[u] = min( lowlink[u] , pre[v] );        }    }    if( lowlink[u] == pre[u] )    {        scc_cnt++;        for(;;){            int  x = st.top(); st.pop();            sccno[x] = scc_cnt ;            if( x == u ) break;        }    }}void find_scc(){    dfs1_clock = scc_cnt = 0 ;    memset ( pre , 0 , sizeof pre ) ;    memset ( sccno , 0 , sizeof sccno ) ;    while( !st.empty() ) st.pop();    for( int i = 1 ; i <= n ; ++i ){        if( !pre[i] )dfs1(i);    }}void init(){    tot= 0 ;    memset( eh , -1 ,sizeof eh );    memset( dp , -1 ,sizeof dp );    memset( w , 0 ,sizeof w );    memset( vis ,false , sizeof vis );    memset( in , 0, sizeof in );}void dfs( int u ){    if( dp[u] == -1 ){        int sum_now = w[u] ;        for( int i = eh[u] ; ~i ; i = nxt[i] ){            int v = et[i];            dfs(v);            sum_now = max( sum_now , w[u] + dp[v] );        }        dp[u] = sum_now;    }}void run(){    int u , v ;    init();    for( int i = 0 ; i < m ;++i ){        scanf("%d%d",&e[i].u,&e[i].v);        addedge( e[i].u , e[i].v );    }    find_scc();    tot = 0 ;    memset(eh , -1 , sizeof eh );    for( int i = 0 ;i < m ;++i ){        if( sccno[e[i].u] != sccno[e[i].v] ){            addedge(  sccno[e[i].u] , sccno[e[i].v] );            in[ sccno[e[i].v] ] ++;        }    }    for( int i = 1 ; i <= n ; ++i  )w[ sccno[i] ] ++ ;
int ans = 0 ; for( int i = 1 ; i <= scc_cnt ; ++i ) if( !in[i] ) addedge(0,i); dfs(0); for( int i = 1; i <= scc_cnt ; ++i ) ans = max( ans , dp[i] ); printf("%d\n",ans);}int main(){ #ifdef LOCAL freopen("in","r",stdin); #endif ios::sync_with_stdio(0); while( ~scanf("%d%d",&n,&m))run();}

 

Zoj 3795 grouping

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.