zoukankan      html  css  js  c++  java
  • Mining Your Own Business UVALive

    these days I‘m tired!,but very happy。。。

    #include<cstdio>
    #include<cstring>
    #include<stack>
    #include<vector>
    #include<algorithm>
    using namespace std;
     
    typedef long long lld;
     
    const int MAXN = 50005<<1;
     
    struct Edge
    {
        int u,v;
    };
     
    vector <int> G[MAXN],bcc[MAXN];
    stack <Edge> S;
    int pre[MAXN],is_cut[MAXN],bccno[MAXN];
    int bcc_cnt,dfs_clock;
     
    int dfs(int u,int fa)
    {
        int low_u = pre[u] = ++dfs_clock;
        int child = 0;
        for(int i = 0;i < G[u].size();i++)
        {
            int v = G[u][i];
            Edge e = (Edge){u,v};
            if(!pre[v])
            {
                child++;
                S.push(e);
                int low_v = dfs(v,u);
                low_u = min(low_u,low_v);
                if(low_v >= pre[u])
                {
                    is_cut[u] = 1;
                    bcc_cnt++;
                    bcc[bcc_cnt].clear();
                    for(;;)
                    {
                        Edge x = S.top();
                        S.pop();
                        if(bccno[x.u] != bcc_cnt)
                        {
                            bcc[bcc_cnt].push_back(x.u);
                            bccno[x.u] = bcc_cnt;
                        }
                        if(bccno[x.v] != bcc_cnt)
                        {
                            bcc[bcc_cnt].push_back(x.v);
                            bccno[x.v] = bcc_cnt;
                        }
                        if(x.u == u && x.v == v)
                            break;
                    }
                }
            }
            else if(pre[v] < pre[u] && v != fa)
            {
                S.push(e);
                low_u = min(low_u,pre[v]);
            }
        }
        if(child == 1 && fa == -1) is_cut[u] = 0;
        return low_u;
    }
     
    void find_bcc(int n)
    {
        memset(pre,0,sizeof(pre));
        memset(is_cut,0,sizeof(is_cut));
        memset(bccno,0,sizeof(bccno));
        dfs_clock = bcc_cnt = 0;
        for(int i = 0;i < n;i++)
            if(!pre[i]) dfs(i,-1);
    }
     
    int main()
    {
        int cas = 0;
        int n;
        while(~scanf("%d",&n) && n)
        {
            for(int i = 0;i < 2*n;i++)
                G[i].clear();
            int max_id = 0;
            for(int i = 0;i < n;i++)
            {
                int a,b;
                scanf("%d%d",&a,&b);
                max_id = max(max_id,a);
                max_id = max(max_id,b);
                a--;b--;
                G[a].push_back(b);
                G[b].push_back(a);
            }
     
            find_bcc(max_id);
            //printf("bcc_cnt = %d
    ",bcc_cnt);
     
            int ans_num = 0;
            lld ans_count = 1;
            if(bcc_cnt == 1)
            {
                ans_num = 2;
                ans_count = (lld)bcc[1].size()*(bcc[1].size()-1)/2;
            }
            else
            {
                for(int i = 1;i <= bcc_cnt;i++)
                {
                    int cc = 0;
                    for(int j = 0;j < bcc[i].size();j++)
                        if(is_cut[bcc[i][j]] == 1)
                            cc++;
                    if(cc == 1)
                    {
                        ans_num++;
                        ans_count *= bcc[i].size()-1;
                    }
                }
            }
            printf("Case %d: %d %lld
    ",++cas,ans_num,ans_count);
        }
        return 0;
    }
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    LinuxShell脚本攻略--第八章 当个好管家
    LinuxShell脚本攻略--第六章 B计划
    LinuxShell脚本攻略--第三章 以文件之名
    LinuxShell脚本攻略--第二章 命令之乐
    LinuxShell脚本攻略--第一章 小试牛刀
    TCP/IP 与OSI结构图
    网络号和主机号的计算
    IP地址分类及私网IP
    转:Cache相关
    原码 反码 补码 移码
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/9360920.html
Copyright © 2011-2022 走看看