zoukankan      html  css  js  c++  java
  • HDU 3836 Equivalent Sets(强连通缩点)

    Equivalent Sets



    Problem Description
    To prove two sets A and B are equivalent, we can first prove A is a subset of B, and then prove B is a subset of A, so finally we got that these two sets are equivalent.
    You are to prove N sets are equivalent, using the method above: in each step you can prove a set X is a subset of another set Y, and there are also some sets that are already proven to be subsets of some other sets.
    Now you want to know the minimum steps needed to get the problem proved.
     
    Input
    The input file contains multiple test cases, in each case, the first line contains two integers N <= 20000 and M <= 50000.
    Next M lines, each line contains two integers X, Y, means set X in a subset of set Y.
     
    Output
    For each case, output a single integer: the minimum steps needed.
     
    Sample Input
    4 0
    3 2
    1 2
    1 3
     
     
    Sample Output
    4
    2
    Hint
    Case 2: First prove set 2 is a subset of set 1 and then prove set 3 is a subset of set 1.
     
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<stack>
     5 #include<vector>
     6 using namespace std;
     7 
     8 const int maxn=20005;
     9 vector<int>G[maxn];
    10 stack<int>s;
    11 int in[maxn],out[maxn],dfn[maxn],lowlink[maxn],sccno[maxn];
    12 int scc_cnt,dfs_clock;
    13 int m,n;
    14 
    15 void init()
    16 {
    17     for(int i=1;i<=n;i++)G[i].clear();
    18     memset(in,0,sizeof(in));
    19     memset(out,0,sizeof(out));
    20     memset(dfn,0,sizeof(dfn));
    21     memset(lowlink,0,sizeof(lowlink));
    22     memset(sccno,0,sizeof(sccno));
    23     scc_cnt=dfs_clock=0;
    24 }
    25 
    26 void tarjan(int u)
    27 {
    28     lowlink[u]=dfn[u]=++dfs_clock;
    29     s.push(u);
    30     for(int i=0;i<G[u].size();i++)
    31     {
    32         int v=G[u][i];
    33         if(!dfn[v])
    34         {
    35             tarjan(v);
    36             lowlink[u]=min(lowlink[u],lowlink[v]);
    37         }
    38         else if(!sccno[v])
    39             lowlink[u]=min(lowlink[u],dfn[v]);
    40     }
    41     if(lowlink[u]==dfn[u])
    42     {
    43         scc_cnt++;
    44         while(1)
    45         {
    46             int x=s.top();
    47             s.pop();
    48             sccno[x]=scc_cnt;
    49             if(x==u)break;
    50         }
    51     }
    52 }
    53 
    54 
    55 
    56 int main()
    57 {
    58     while(scanf("%d%d",&n,&m)!=EOF)
    59     {
    60         init();
    61         for(int i=0;i<m;i++)
    62         {
    63             int u,v;
    64             scanf("%d%d",&u,&v);
    65             G[u].push_back(v);
    66         }
    67         for(int i=1;i<=n;i++)
    68             if(!dfn[i])
    69                 tarjan(i);
    70         for(int i=1;i<=n;i++)
    71             for(int j=0;j<G[i].size();j++)
    72                 if(sccno[G[i][j]]!=sccno[i])
    73                 {
    74                     in[sccno[G[i][j]]]++;
    75                     out[sccno[i]]++;
    76                 }
    77         int cnt1=0,cnt2=0;
    78         for(int i=1;i<=scc_cnt;i++)
    79         {
    80             if(!in[i])
    81                 cnt1++;
    82             if(!out[i])
    83                 cnt2++;
    84         }
    85         if(scc_cnt==1)
    86             puts("0");
    87         else
    88             printf("%d
    ",max(cnt1,cnt2));
    89     }
    90     return 0;
    91 }
  • 相关阅读:
    个人网站
    物理读,逻辑读,预读
    正则表达式
    面向对象五大基本原则
    工作总结
    sql性能优化
    sqlServer游标的使用
    ASP.NET安全[开发ASP.NET MVC应用程序时值得注意的安全问题](转)
    一个简单问题引发对IEnumerable和IQueryable的思考
    EFCodeFirst 各种命令整理
  • 原文地址:https://www.cnblogs.com/homura/p/4864783.html
Copyright © 2011-2022 走看看