• HDU 3836 Equivalent Sets


    Equivalent Sets

    Time Limit: 4000ms
    Memory Limit: 104857KB
    This problem will be judged on HDU. Original ID: 3836
    64-bit integer IO format: %I64d      Java class name: Main
     
     
    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

     
    解题:本意就是最少添加多少条边,使得全图强连通。tarjan求极大强连通子图后缩点。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 const int maxn = 20100;
    18 int dfn[maxn],low[maxn],belong[maxn];
    19 bool instack[maxn];
    20 vector<int>g[maxn];
    21 stack<int>stk;
    22 int n,m,cnt,scc,in[maxn],out[maxn];
    23 void tarjan(int u){
    24     dfn[u] = low[u] = ++cnt;
    25     stk.push(u);
    26     instack[u] = true;
    27     for(int i = 0; i < g[u].size(); i++){
    28         if(!dfn[g[u][i]]){
    29             tarjan(g[u][i]);
    30             low[u] = min(low[u],low[g[u][i]]);
    31         }else if(instack[g[u][i]]) low[u] = min(low[u],dfn[g[u][i]]);
    32     }
    33     if(dfn[u] == low[u]){
    34         scc++;
    35         int v;
    36         do{
    37             v = stk.top();
    38             instack[v] = false;
    39             belong[v] = scc;
    40             stk.pop();
    41         }while(v != u);
    42     }
    43 }
    44 int main() {
    45     int i,j,u,v,a,b;
    46     while(~scanf("%d %d",&n,&m)){
    47         for(i = 0; i <= n; i++){
    48             dfn[i] = low[i] = belong[i] = 0;
    49             instack[i] = false;
    50             g[i].clear();
    51             in[i] = out[i] = 0;
    52         }
    53         cnt = scc = 0;
    54         while(!stk.empty()) stk.pop();
    55         for(i = 1; i <= m; i++){
    56             scanf("%d %d",&u,&v);
    57             g[u].push_back(v);
    58         }
    59         for(i = 1; i <= n; i++)
    60             if(!dfn[i]) tarjan(i);
    61         for(i = 1; i <= n; i++){
    62             for(j = 0; j < g[i].size(); j++){
    63                 if(belong[i] != belong[g[i][j]]){
    64                     in[belong[g[i][j]]]++;
    65                     out[belong[i]]++;
    66                 }
    67             }
    68         }
    69         for(a = b = 0,i = 1; i <= scc; i++){
    70             if(!in[i]) a++;
    71             if(!out[i]) b++;
    72         }
    73         printf("%d
    ",scc == 1?0:max(a,b));
    74     }
    75     return 0;
    76 }
    View Code
  • 相关阅读:
    sql over(partition by) 开窗函数的使用
    利用curl函数处理GET数据获取微信公众号的access_token
    2018.4.12
    字段和属性
    C#实现回车键登录
    判断DataTable里面数据是否有重复数据
    一个强大的人民币大写转换的正则表达式
    C#将image中的显示的图片转换成二进制
    遍历Dev LayoutControl中的所有控件信息
    遍历窗体中所有控件的信息
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3935773.html
Copyright © 2020-2023  润新知