J. City traffic tarjan缩点 11TH BUPT Collegiate Programming Contest

9 篇文章 0 订阅
6 篇文章 0 订阅

题目描述

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值