您的位置:首页 > 其它

ZOJ 3206 (tarjan缩点+DAG带权最长路)

2018-02-02 15:04 615 查看
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3206

Disaster Area Reconstruction
Time Limit: 10 Seconds     
Memory Limit: 32768 KB

There was a large earthquake in Sichuan last May. All the things were ruined in that disaster, the buildings, the railways and the establishments.

In order to send the rescue materials to where the disaster was, the traffic had to be reconstructed first. But just after the earthquake, the government spent a lot of money propitiating the refugees and could not afford much on reconstruction. After investigating
the remaining roads, the government decided to make one new road so that the maximum connected component would be maximized by this new road (in a connected component, each village has directed paths to any other villages). Now your task is to determine how
the new road is to be constructed.

NOTICE that:

Each road is one-way, i.e. you can only go from one end of the road to the other end, but can not go in the opposite direction.
No road is built from and to the same village, since it would be useless. But there could be more than one road from one village to another since this can enlarge the traffic capability between these two villages.

Input

The first line of the input contains an integer T (T <= 50), indicating the number of cases.

The first line of each test case contains two integers N and M (2 <=N <= 50000, 0 <=
M <= 500000), indicating the number of villages and the number of remaining roads. Each of the nextM lines contains two integers
Ai and Bi (1 <=Ai,
Bi <= N), meaning that the i-th road is from villageAi to village
Bi.

Output

For each test case, first output an integer indicating the maximum number of villages to which one can go from any village. The second line contains two integersA and
B indicating the two villages that the new road is connecting to. If there are several solutions leading to the same result, output the one that comes lexicographically first (as defined in Problem C).

Sample Input

1
5 4
1 3
2 3
3 4
3 5


Sample Output

3
4 1


Author: GUAN, Yao

Source: The 6th Zhejiang Provincial Collegiate Programming Contest
我想先说一下,2009年的浙江题。。如果我下面描述的题意是正确的,这题大概出错了吧。。。

【题意】:

给出一个有向图(可能有环),n个点,m条有向边,你可以加一条有向边,从而得到一个最大的强连通分量(即强连通分量的点数最多)。

【分析1】(此分析不能过此题,但我觉得是正确对题意的解读)

我先按上述题意来分析(要么题意有错,要么题目要求有错)。我的思路是,先用tarjan算法缩点,把同在一个强连通分量的点汇聚成一个点。形成一张新图,并且每个点的权值为汇聚的点的个数,新图必为有向无环图DAG!

然后我们在DAG上找两个点连起来,会产生一个新的强连通分量,假如输入数据输入的是下图:



显然,不存在强联通分量(或者认为每个点都单独是一个强连通分量),缩点形成的DAG图和上图相同。

直接连接4->1,获得最大的强连通分量为4。恩我觉得这样没错。

如果按照这个思路,是不是找出一个最大的网络流呢?(这个我就不会找了,枚举起点会不会超时呢....)

【分析2】(此分析与题意相违背,却能AC,呵呵)

在分析1中,我们进行tarjan缩点,然后获得一个DAG图。在DAG图上用记忆化搜索跑一个带权最长路,把最长路的头和尾连接,这样好像就得到了一个最大的强连通分量。。。然而再看分析1中的那个图,完美的否定了当前的分析。。

不知道有没有官方解释........

d1a0

【AC代码】(此代码通过分析2写的。。。)

代码思路:tarjan缩点,形成带点权的DAG图,然后记忆化搜索找最长路(同时考虑字典序,所以代码中的DAG图建图建的是反图,即所有有向边反向)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX=50020;
struct node{
int s,t,next;
node(int S=0,int T=0,int N=0){
s=S;t=T;next=N;
}
}e[MAX*10],e1[MAX*10];
int head[MAX],cnt;
void add(int u,int v)
{
e[cnt]=node(u,v,head[u]);
head[u]=cnt++;
}

int head1[MAX],cnt1;
void add1(int u,int v)
{
e1[cnt1]=node(u,v,head1[u]);
head1[u]=cnt1++;
}
int val[MAX]; //DAG每个点的权值
int dex[MAX]; //映射

int dfn[MAX];//每个节点的访问时间编号
int low[MAX];//每个点能到达的最小编号
int sta[MAX],top;
int Scc[MAX];//每个点所属的分量 序号
void tardfs(int u,int &lay,int &sig)
{
low[u]=dfn[u]=lay++;//到达此点的时间
sta[top++]=u;//压入栈
for(int i=head[u];~i;i=e[i].next)
{
int t=e[i].t;
if(dfn[t]==0)
{
tardfs(t,lay,sig);
low[u]=min(low[u],low[t]);
}
else if(!Scc[t])//访问过了
low[u]=min(low[u],dfn[t]);//强连通求法可以用low
}
if(low[u]==dfn[u])//u不能到达任何之前走过的点
{
sig++;
while(1)
{
int j=sta[--top];//出栈
Scc[j]=sig;
if(j==u)break;
}//包含u的连通分量出栈
}
}
int tarjan(int n)
{
int sig=0;//强连通数,从1计数
int lay=1;//时间戳
top=0;
memset(Scc,0,sizeof(Scc));
memset(dfn,0,sizeof(dfn));//时间戳归0
for(int i=1;i<=n;i++)
{
if(!dfn[i])tardfs(i,lay,sig);
}
return sig;//返回连通数
}

int dp[MAX][2];
int dfs(int u)
{
int &res=dp[u][0];
int &son=dp[u][1];
if(res>=0)return res;
res=val[u];
son=MAX;
for(int i=head1[u];~i;i=e1[i].next)
{
int temp=dfs(e1[i].t)+val[u];
if(temp>res){
res=temp;
son=dp[e1[i].t][1];
}else if(temp==res&&dp[e1[i].t][1]<son)
son=dp[e1[i].t][1];
}
if(son==MAX)son=u;
return res;
}
int main()
{
//freopen("input.txt","r",stdin);
int n,m,u,v,T;
scanf("%d",&T);
int r=1;
while(T--)
{
//printf("\n\ncase %d:\n",r++);
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
cnt=0;
while(m--)
{
scanf("%d%d",&u,&v);
add(u,v);
}
int sig=tarjan(n);  //连通数

memset(val,0,sizeof(val));
memset(dex,0x3f,sizeof(dex));

for(int i=1;i<=n;i++)
dex[Scc[i]]=min(dex[Scc[i]],i); //分量号->最小点
for(int i=1;i<=n;i++)
val[dex[Scc[i]]]++; //DAG value
memset(head1,-1,sizeof(head1));
cnt1=0;
for(int i=1;i<=n;i++)
{
for(int j=head[i];~j;j=e[j].next)
if(Scc[i]!=Scc[e[j].t]) //两个分量 i->e[j].t
{
add1(dex[Scc[e[j].t]],dex[Scc[i]]); //反图
}
}

//DAG
memset(dp,-1,sizeof(dp));
for(int i=1;i<=sig;i++)
dfs(dex[i]);

int s=dex[1];
for(int i=1;i<=sig;i++)
{
if(dp[dex[i]][0]>dp[s][0])s=dex[i];
else if(dp[dex[i]][0]==dp[s][0]&&dex[i]<s)s=dex[i];

}
int flag=0;
for(int i=1;i<=sig;i++)
{
if(dp[dex[i]][0]==dp[s][0]&&dp[dex[i]][1]==dex[i])flag=1;
}
printf("%d\n",dp[s][0]);
int t=dp[s][1];
if(Scc[t]==Scc[s]||flag){
printf("1 2\n");
}else
printf("%d %d\n",s,t);
}
}
/*
1
4 4
1 2
1 3
2 4
3 4
这组数据答案应该是4 4 1,但AC代码输出3 4 1
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: