zoukankan      html  css  js  c++  java
  • HDU 4635 Strongly connected (2013多校4 1004 有向图的强连通分量)

    Strongly connected

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 53    Accepted Submission(s): 15


    Problem Description
    Give a simple directed graph with N nodes and M edges. Please tell me the maximum number of the edges you can add that the graph is still a simple directed graph. Also, after you add these edges, this graph must NOT be strongly connected.
    A simple directed graph is a directed graph having no multiple edges or graph loops.
    A strongly connected digraph is a directed graph in which it is possible to reach any node starting from any other node by traversing edges in the direction(s) in which they point. 
     
    Input
    The first line of date is an integer T, which is the number of the text cases.
    Then T cases follow, each case starts of two numbers N and M, 1<=N<=100000, 1<=M<=100000, representing the number of nodes and the number of edges, then M lines follow. Each line contains two integers x and y, means that there is a edge from x to y.
     
    Output
    For each case, you should output the maximum number of the edges you can add.
    If the original graph is strongly connected, just output -1.
     
    Sample Input
    3 3 3 1 2 2 3 3 1 3 3 1 2 2 3 1 3 6 6 1 2 2 3 3 1 4 5 5 6 6 4
     
    Sample Output
    Case 1: -1 Case 2: 1 Case 3: 15
     
    Source
     
    Recommend
    zhuyuanchen520
     

     Tarjan 缩点。

    /*
     *  Author:kuangbin
     *  1004.cpp
     */
    
    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    #include <iostream>
    #include <map>
    #include <vector>
    #include <queue>
    #include <set>
    #include <string>
    #include <math.h>
    using namespace std;
    /*
     * Tarjan算法
     * 复杂度O(N+M)
     */
    const int MAXN = 100010;//点数
    const int MAXM = 100010;//边数
    struct Edge
    {
        int to,next;
    }edge[MAXM];
    int head[MAXN],tot;
    int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong数组的值是1~scc
    int Index,top;
    int scc;//强连通分量的个数
    bool Instack[MAXN];
    int num[MAXN];//各个强连通分量包含点的个数,数组编号1~scc
    //num数组不一定需要,结合实际情况
    
    void addedge(int u,int v)
    {
        edge[tot].to = v;edge[tot].next = head[u];head[u] = tot++;
    }
    void Tarjan(int u)
    {
        int v;
        Low[u] = DFN[u] = ++Index;
        Stack[top++] = u;
        Instack[u] = true;
        for(int i = head[u];i != -1;i = edge[i].next)
        {
            v = edge[i].to;
            if( !DFN[v] )
            {
                Tarjan(v);
                if( Low[u] > Low[v] )Low[u] = Low[v];
            }
            else if(Instack[v] && Low[u] > DFN[v])
                Low[u] = DFN[v];
        }
        if(Low[u] == DFN[u])
        {
            scc++;
            do
            {
                v = Stack[--top];
                Instack[v] = false;
                Belong[v] = scc;
                num[scc]++;
            }
            while( v != u);
        }
    }
    void solve(int N)
    {
        memset(DFN,0,sizeof(DFN));
        memset(Instack,false,sizeof(Instack));
        memset(num,0,sizeof(num));
        Index = scc = top = 0;
        for(int i = 1;i <= N;i++)
            if(!DFN[i])
                Tarjan(i);
    }
    void init()
    {
        tot = 0;
        memset(head,-1,sizeof(head));
    }
    int in[MAXN],out[MAXN];
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int T;
        scanf("%d",&T);
        int iCase = 0;
        int n,m;
        int u,v;
        while(T--)
        {
            iCase++;
            init();
            scanf("%d%d",&n,&m);
            for(int i = 0;i < m;i++)
            {
                scanf("%d%d",&u,&v);
                addedge(u,v);
            }
            solve(n);
            if(scc == 1)
            {
                printf("Case %d: -1
    ",iCase);
                continue;
            }
            for(int i = 1;i <= scc;i++)
            {
                in[i] = 0;
                out[i] = 0;
            }
            for(int u = 1;u <= n;u++)
                for(int i = head[u];i != -1;i = edge[i].next)
            {
                int v = edge[i].to;
                if(Belong[u]==Belong[v])continue;
                out[Belong[u]]++;
                in[Belong[v]]++;
            }
            long long sss = (long long)n*(n-1) - m;
            long long ans = 0;
            for(int i = 1;i <= scc;i++)
            {
                if(in[i]==0 || out[i] == 0)
                    ans = max(ans,sss - (long long)num[i]*(n-num[i]));
            }
            printf("Case %d: %d
    ",iCase,ans);
        }
        return 0;
    }
  • 相关阅读:
    我开发中的用到的几个框架
    关于ASP.NETCore的分享之学习路线
    首个.NET5+Vue.js业务模块化快速开发框架【NetModular】发布
    [C#] (原创)一步一步教你自定义控件 —— 系列文章
    EFS加密
    博客园样式美化:给博客添加一个音乐播放器
    XSS语义分析
    TCP回放攻击 & DDoS脉冲攻击Hit and Run IoT僵尸网络 在DDoS攻击黑产领域最活跃
    小样本学习,阿里做得比较早,但是效果未知——小样本有3类解决方法(算法维度):迁移学习、元学习(模型基础上学习模型)、度量学习(相似度衡量,也就是搜索思路),数据维度还有GAN
    真实世界中的开集识别问题(Open-Set Recognition Problem)——Walter J. Scheirer研究是最深的,安全里已经有研究了,但是感觉只是触及了皮毛而已
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3230625.html
Copyright © 2011-2022 走看看