• ZOJ 3795 Grouping


    Grouping

    Time Limit: 2000ms
    Memory Limit: 65536KB
    This problem will be judged on ZJU. Original ID: 3795
    64-bit integer IO format: %lld      Java class name: Main

    Suppose there are N people in ZJU, whose ages are unknown. We have some messages about them. Thei-th message shows that the age of person si is not smaller than the age of person ti. Now we need to divide all these N people into several groups. One's age shouldn't be compared with each other in the same group, directly or indirectly. And everyone should 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 integers N(1≤ N≤ 100000),M(1≤ M≤ 300000), N is the number of people, and M is is the number of messages. Then followed by Mlines, 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 4
    1 2
    1 3
    2 4
    3 4
    

    Sample Output

    3
    

    Hint

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

     

    Source

    Author

    LUO, Jiewei
     
    解题:tarjan缩点+DAG最长路
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 100010;
     4 struct arc{
     5     int to,next;
     6     arc(int x = 0,int y = -1){
     7         to = x;
     8         next = y;
     9     }
    10 }e[500000];
    11 int head[maxn],dfn[maxn],low[maxn],belong[maxn],num[maxn];
    12 bool instack[maxn];
    13 int tot,idx,scc,n,m,dp[maxn];
    14 stack<int>stk;
    15 vector<int>g[maxn];
    16 void add(int u,int v){
    17     e[tot] = arc(v,head[u]);
    18     head[u] = tot++;
    19 }
    20 void init(){
    21     for(int i = 0; i < maxn; ++i){
    22         dp[i] = head[i] = -1;
    23         dfn[i] = low[i] = 0;
    24         belong[i] = num[i] = 0;
    25         instack[i] = false;
    26         g[i].clear();
    27     }
    28     idx = scc = tot = 0;
    29     while(!stk.empty()) stk.pop();
    30 }
    31 void tarjan(int u){
    32     dfn[u] = low[u] = ++idx;
    33     instack[u] = true;
    34     stk.push(u);
    35     for(int i = head[u]; ~i; i = e[i].next){
    36         if(!dfn[e[i].to]){
    37             tarjan(e[i].to);
    38             low[u] = min(low[u],low[e[i].to]);
    39         }else if(instack[e[i].to]) low[u] = min(low[u],dfn[e[i].to]);
    40     }
    41     if(low[u] == dfn[u]){
    42         scc++;
    43         int v;
    44         do{
    45             instack[v = stk.top()] = false;
    46             belong[v] = scc;
    47             num[scc]++;
    48             stk.pop();
    49         }while(v != u);
    50     }
    51 }
    52 int dfs(int u){
    53     if(dp[u] != -1) return dp[u];
    54     dp[u] = num[u];
    55     for(int i = g[u].size()-1; i >= 0; --i)
    56         dp[u] = max(dp[u],num[u] + dfs(g[u][i]));
    57     return dp[u];
    58 }
    59 int main(){
    60     int u,v;
    61     while(~scanf("%d %d",&n,&m)){
    62         init();
    63         for(int i = 0; i < m; ++i){
    64             scanf("%d %d",&u,&v);
    65             add(u,v);
    66         }
    67         for(int i = 1; i <= n; ++i)
    68             if(!dfn[i]) tarjan(i);
    69         for(int i = 1; i <= n; ++i){
    70             for(int j = head[i]; ~j; j = e[j].next){
    71                 if(belong[i] != belong[e[j].to])
    72                     g[belong[e[j].to]].push_back(belong[i]);
    73             }
    74         }
    75         int ret = 0;
    76         for(int i = 1; i <= scc; ++i)
    77             ret = max(ret,dfs(i));
    78         printf("%d
    ",ret);
    79     }
    80     return 0;
    81 }
    View Code
  • 相关阅读:
    DataTabe使用Linq实现 Group
    通用化NPOI导出xls
    DosBox 的 DOSBOX.CONF 的详细配置说
    wx预览图片
    jqweui Picker使用一个小问题
    一个504错误原因
    DingDing的CorpSecretID和SSOSecret不是一个东西
    Android上禁止屏幕旋转
    Error:Failed to resolve: com.android.support:recyclerview-v7:26.1.0
    glide:4.7.1 与 26.1.0冲突
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4471628.html
Copyright © 2020-2023  润新知