zoukankan      html  css  js  c++  java
  • HUD——T 3836 Equivalent Sets

    http://acm.hdu.edu.cn/showproblem.php?pid=3836

    Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Others)
    Total Submission(s): 4802    Accepted Submission(s): 1725


    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.
     
    Source
     
    Recommend
    xubiao   |   We have carefully selected several similar problems for you:  3835 3828 3834 3830 3829 
     
     
    题意:求最少连几条边可以使整个图成为强连通图
    可以先将图缩点,然后统计新图中入读==0,和出度==0 的点的个数,因为使加边最少,
    所以应该是先给出度==0的点连一条向入读==0的点得边,然后再加上多余的(入读==0||出度==0)的点
    ans=max(入读==0的点数,出读==0的点数)
     1 #include <algorithm>
     2 #include <cstring>
     3 #include <cstdio>
     4 
     5 using namespace std;
     6 
     7 const int N(20000+5);
     8 const int M(50000+5);
     9 int n,m;
    10 
    11 int head[N],sumedge;
    12 struct Edge
    13 {
    14     int v,next;
    15     Edge(int v=0,int next=0):v(v),next(next){} 
    16 }edge[M];
    17 inline void ins(int u,int v)
    18 {
    19     edge[++sumedge]=Edge(v,head[u]);
    20     head[u]=sumedge;
    21 }
    22 
    23 int tim,dfn[N],low[N];
    24 int top,Stack[N],instack[N];
    25 int sumcol,col[N];
    26 void DFS(int now)
    27 {
    28     dfn[now]=low[now]=++tim;
    29     Stack[++top]=now; instack[now]=1;
    30     for(int i=head[now];i;i=edge[i].next)
    31     {
    32         int v=edge[i].v;
    33         if(!dfn[v])  DFS(v),low[now]=min(low[now],low[v]);
    34         else if(instack[v]) low[now]=min(low[now],dfn[v]);
    35     }
    36     if(dfn[now]==low[now])
    37     {
    38         col[now]=++sumcol;
    39         for(;Stack[top]!=now;top--)
    40         {
    41             col[Stack[top]]=sumcol;
    42             instack[Stack[top]]=0;
    43         }
    44         instack[now]=0; top--;
    45     }
    46 }
    47 
    48 int ans,ans1,ans2,rd[N],cd[N];
    49 inline void init()
    50 {
    51     top=ans=ans1=ans2=tim=sumcol=sumedge=0;
    52     memset(rd,0,sizeof(rd));
    53     memset(cd,0,sizeof(cd));
    54     memset(low,0,sizeof(low));
    55     memset(dfn,0,sizeof(dfn));
    56     memset(head,0,sizeof(head));
    57     memset(Stack,0,sizeof(Stack));
    58     memset(instack,0,sizeof(instack));
    59 }
    60 
    61 int main()
    62 {
    63     for(;~scanf("%d%d",&n,&m);init())
    64     {
    65         for(int u,v,i=1;i<=m;i++)
    66             scanf("%d%d",&u,&v),ins(u,v);
    67         for(int i=1;i<=n;i++)
    68             if(!dfn[i]) DFS(i);
    69         for(int u=1;u<=n;u++)
    70             for(int i=head[u];i;i=edge[i].next)
    71             {
    72                 int v=edge[i].v;
    73                 if(col[u]==col[v]) continue;
    74                 rd[col[v]]++; cd[col[u]]++;
    75             }
    76         for(int i=1;i<=sumcol;i++)
    77         {
    78             if(!cd[i]) ans1++;
    79             if(!rd[i]) ans2++;
    80         }
    81         ans=max(ans1,ans2);
    82         if(sumcol==1) ans=0;
    83         printf("%d
    ",ans);
    84     }
    85     return 0;
    86 }
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    mysql日期转化
    跳来跳去,到底去大公司还是小公司?
    年度重大升级,IntelliJ IDEA 2019.2 稳定版发布
    3年Java,鏖战腾讯
    重磅!挑战Oracle,华为将开源 GaussDB 数据库
    厉害了,Java EE 再次更名为 Jakarta EE
    彻底干掉恶心的 SQL 注入漏洞, 一网打尽!
    PostgreSQL 荣获 2019 年 O'Reilly 终身成就奖
    Intellij IDEA 智能补全的 10 个姿势,简直不能太牛逼!
    注意,千万不要打断工作中的程序员
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7380720.html
Copyright © 2011-2022 走看看