编程介的小学生 2017-11-16 12:47 采纳率: 20.5%
浏览 612
已结题

Strongly connected

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

  • 写回答

1条回答 默认 最新

  • Traveling_DING 2018-08-07 02:06
    关注

    #include
    #include
    #include
    #include
    using namespace std;
    const int N = 100005;
    const int M = 200005;
    const int INF = 0xffffffff;
    typedef long long ll;
    int n, m, cnt, head[N];
    int dep, top, atype;
    int dfn[N], low[N], Stack[N], belong[N], in[N], out[N], sum[N];
    bool vis[N];
    struct Edge
    {
    int to, nxt;
    }edge[M];
    void addedge(int u, int v)
    {
    edge[cnt].to = v;
    edge[cnt].nxt = head[u];
    head[u] = cnt++;
    }
    void Tarjan(int u)
    {
    dfn[u] = low[u] = ++dep;
    Stack[top++] = u;
    vis[u] = true;
    for(int i = head[u]; i != -1; i = edge[i].nxt)
    {
    int v = edge[i].to;
    if(!dfn[v])
    {
    Tarjan(v);
    low[u] = min(low[u], low[v]);
    }
    else if(vis[v])
    low[u] = min(low[u], dfn[v]);
    }
    int j;
    if(dfn[u] == low[u])
    {
    atype++;
    do
    {
    j = Stack[--top];
    belong[j] = atype;
    sum[atype]++;
    vis[j] = false;
    }
    while(u != j);
    }
    }
    void solve()
    {
    if(n == 1)
    {
    puts("-1");
    return ;
    }
    cnt = dep = top = atype = 0;
    memset(head, -1, sizeof(head));
    memset(dfn, 0, sizeof(dfn));
    memset(low, 0, sizeof(low));
    memset(vis, false, sizeof(vis));
    memset(belong, 0, sizeof(belong));
    memset(in, 0, sizeof(in));
    memset(out, 0, sizeof(out));
    memset(sum, 0, sizeof(sum));
    int u, v;
    for(int i = 0; i < m; i++)
    {
    scanf("%d%d", &u, &v);
    addedge(u, v);
    }
    for(int i = 1; i <= n; i++)
    if(!dfn[i])
    Tarjan(i);
    if(atype == 1)
    {
    puts("-1");
    return ;
    }
    for(int u = 1; u <= n; u++)
    for(int i = head[u]; i != -1; i = edge[i].nxt)
    {
    int v = edge[i].to;
    if(belong[u] != belong[v])
    {
    out[belong[u]]++;
    in[belong[v]]++;
    }
    }
    ll ans = 0, tmp;
    for(int i = 1; i <= atype; i++)
    if(in[i] == 0 || out[i] == 0)
    {
    tmp = sum[i];
    ans = max(ans, tmp*(tmp-1) + (n-tmp)*(n-tmp-1) + tmp*(n-tmp) - m);
    }
    printf("%I64d\n", ans);
    }
    int main()
    {
    int T;
    scanf("%d", &T);
    for(int kase = 1; kase <= T; kase++)
    {
    scanf("%d%d", &n, &m);
    printf("Case %d: ", kase);
    solve();
    }
    return 0;
    }

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)