zoukankan      html  css  js  c++  java
  • HDU 4635 Strongly connected

    Strongly connected

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

     
    解题:不错的题目,首先做有向图的强连通分量缩点,同时记录各个scc的点,那么可以想象下,我们把可以加的边都加进去,会导致全图分成两个完全图的强连通块,但是这两个块之间,只有一个到另一个块的边,这样保证全图不是一个SCC
    所以这样需要找入度或出度为0的SCC
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 using LL = long long;
     4 const int INF = 0x3f3f3f3f;
     5 const int maxn = 100010;
     6 
     7 vector<int>g[maxn];
     8 stack<int>stk;
     9 int dfn[maxn],low[maxn],belong[maxn],cnt[maxn],clk,scc;
    10 int ind[maxn],oud[maxn];
    11 bool instack[maxn];
    12 void tarjan(int u) {
    13     dfn[u] = low[u] = ++clk;
    14     stk.push(u);
    15     instack[u] = true;
    16     for(int i = g[u].size()-1; i >= 0; --i) {
    17         if(!dfn[g[u][i]]) {
    18             tarjan(g[u][i]);
    19             low[u] = min(low[u],low[g[u][i]]);
    20         } else if(instack[g[u][i]])
    21             low[u] = min(low[u],dfn[g[u][i]]);
    22     }
    23     if(low[u] == dfn[u]) {
    24         int v;
    25         do {
    26             instack[v = stk.top()] = false;
    27             belong[v] = scc;
    28             stk.pop();
    29             ++cnt[scc];
    30         } while(v != u);
    31         ++scc;
    32     }
    33 }
    34 int main() {
    35     int kase,n,m,u,v,cs = 1;
    36     scanf("%d",&kase);
    37     while(kase--) {
    38         scanf("%d%d",&n,&m);
    39         for(int i = 0; i <= n; ++i) {
    40             g[i].clear();
    41             dfn[i] = cnt[i] = 0;
    42             ind[i] = oud[i] = 0;
    43         }
    44         for(int i = scc = 0; i < m; ++i) {
    45             scanf("%d%d",&u,&v);
    46             g[u].push_back(v);
    47         }
    48         for(int i = 1; i <= n; ++i)
    49             if(!dfn[i]) tarjan(i);
    50         for(int i = 1; i <= n; ++i){
    51             for(int j = g[i].size()-1; j >= 0; --j){
    52                 if(belong[i] == belong[g[i][j]]) continue;
    53                 ++ind[belong[g[i][j]]];
    54                 ++oud[belong[i]];
    55             }
    56         }
    57         int x = INF,y = n;
    58         for(int i = 0; i < scc; ++i)
    59             if(!ind[i] || !oud[i]) x = min(x,cnt[i]);
    60         y -= x;
    61         LL ret = (LL)x*(x - 1) + (LL)y*(y - 1) + (LL)x*y - m;
    62         printf("Case %d: %I64d
    ",cs++,scc == 1?-1LL:ret);
    63     }
    64     return 0;
    65 }
    View Code
  • 相关阅读:
    2019软工实践_作业4_1(结对编程实现博客)
    2019软工实践_作业3_2(团队介绍博客)
    跨域资源共享CORS
    解决IntelliJ无法导入maven包的问题
    IntelliJ IDEA中各种小图标的含义
    IntelliJ 发布Maven项目时所需的Jar没有打包的问题
    IntelliJ跳转到抽象方法的实现
    IntelliJ IDEA利用Maven下载所需的JAR包到项目中
    Java编程思想代码环境配置
    查看window用户登录日志
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4966236.html
Copyright © 2011-2022 走看看