J. City traffic tarjan縮點 11TH BUPT Collegiate Programming Contest

題目描述

Now there are n cities and m unidirectional roads, which means if there is a road from a to b, then you can only travel from a to b but never b to a. At first, no two cities can reach each other(means if you travel from a to b, you have no way to get back a, neither direct nor indirect). Now if you have a chance to change an unidirectional road into a bidirectional road, how many pairs of cities will become mutually connected at most? Note that (a,a) is not a legal pair, and (a,b),(b,a) are regarded as one pair.

輸入格式

The first line is case number T(1T3)T test cases follow.
For each test case:

  • The first line are two integers n,m(1n1000,1m5000)
  • Then m lines follow, each line contains two integers a,b, which means there is an unidirectional road form city a to city b(1a,bn).

輸出格式

For each test case print an integer indicating most mutually connected city pairs after the change.


思路還是比較容易想到,原圖是一個有向無環圖,找出該成無向邊後能形成的最大的強聯通分量,上面所有的點都是強聯通的,取C(2,n)就能得到答案。

最開始用BFS瞎搞,然後TLE,向大佬請教思路後方才發現m的大小完全可以用tarjan縮點,m*(m+n)的複雜度。


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
#define N 1001
#define M 5005
struct Edge
{
    int s;
    int v;
    int next;
};
Edge edge[M];//邊的集合

int node[N];//頂點集合
int instack[N];//標記是否在stack中
int stack[N];
int Belong[N];//各頂點屬於哪個強連通分量
int DFN[N];//節點u搜索的序號(時間戳)
int LOW[N];//u或u的子樹能夠追溯到的最早的棧中節點的序號(時間戳)
int n, m;//n:點的個數;m:邊的條數
int cnt_edge;//邊的計數器
int Index;//序號(時間戳)
int top;
int Bcnt;//有多少個強連通分量

int num[N];
int ans;

int dis(int k){
    return k*(k-1)/2;
}

void add_edge(int u, int v)//鄰接表存儲
{
    edge[cnt_edge].s = u;
    edge[cnt_edge].next = node[u];
    edge[cnt_edge].v = v;
    node[u] = cnt_edge++;
}
void tarjan(int u)
{
    int i,j;
    int v;
    DFN[u]=LOW[u]=++Index;
    instack[u]=true;
    stack[++top]=u;
    for (i = node[u]; i != -1; i = edge[i].next)
    {
        v=edge[i].v;
        if (!DFN[v])//如果點v沒被訪問
        {
            tarjan(v);
            if (LOW[v]<LOW[u])
                LOW[u]=LOW[v];
        }
        else//如果點v已經被訪問過
            if (instack[v] && DFN[v]<LOW[u])
                LOW[u]=DFN[v];
    }
    if (DFN[u]==LOW[u])
    {
        Bcnt++;
        do
        {
            j=stack[top--];
            instack[j]=false;
            Belong[j]=Bcnt;
        }
        while (j!=u);
    }
}
void solve()
{
    int i;
    top=Bcnt=Index=0;
    memset(DFN,0,sizeof(DFN));
    memset(LOW,0,sizeof(LOW));
    for (i=1;i<=n;i++)
        if (!DFN[i])
            tarjan(i);
}
int main()
{
    int T;
    int i,j,k;
    int sum,temp;
    scanf("%d",&T);
    while(T--){
        ans = 0;
        cnt_edge = 0;
        memset(node,-1,sizeof(node));

        scanf("%d%d",&n,&m);
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&j,&k);
            add_edge(j,k);
        }
        for(int p=0;p<m;p++){
            sum =0;
            memset(num,0,sizeof(num));
            edge[cnt_edge].s = edge[p].v;
            edge[cnt_edge].next = node[edge[p].v];
            edge[cnt_edge].v = edge[p].s;
            temp = node[edge[p].v];
            node[edge[p].v] = cnt_edge++;
            solve();
            cnt_edge--;
            node[edge[p].v] = temp;
            for(i=1;i<=n;i++){
                num[Belong[i]]++;
            }
            for(i=1;i<=n;i++){
                sum = max(sum,num[i]);
            }
            ans = max(ans,sum);
            //printf("no.%d.  ans: %d\n",p,ans);
        }
        printf("%d\n",dis(ans));
    }
   return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章