BZOJ3832: [Poi2014]Rally

Description

An annual bicycle rally will soon begin in Byteburg. The bikers of Byteburg are natural long distance cyclists. Local representatives of motorcyclists, long feuding the cyclists, have decided to sabotage the event.
There are   intersections in Byteburg, connected with one way streets. Strangely enough, there are no cycles in the street network - if one can ride from intersection U to intersection V , then it is definitely impossible to get from V to U.
The rally's route will lead through Byteburg's streets. The motorcyclists plan to ride their blazing machines in the early morning of the rally day to one intersection and completely block it. The cyclists' association will then of course determine an alternative route but it could happen that this new route will be relatively short, and the cyclists will thus be unable to exhibit their remarkable endurance. Clearly, this is the motorcyclists' plan - they intend to block such an intersection that the longest route that does not pass through it is as short as possible.
给定一个N个点M条边的有向无环图,每条边长度都是1。
请找到一个点,使得删掉这个点后剩余的图中的最长路径最短。

Input

In the first line of the standard input, there are two integers, N and M(2<=N<=500 000,1<=M<=1 000 000), separated by a single space, that specify the number of intersections and streets in Byteburg. The intersections are numbered from   to  . The   lines that follow describe the street network: in the  -th of these lines, there are two integers, Ai, Bi(1<=Ai,Bi<=N,Ai<>Bi), separated by a single space, that signify that there is a one way street from the intersection no. Ai to the one no. Bi.
第一行包含两个正整数N,M(2<=N<=500 000,1<=M<=1 000 000),表示点数、边数。
接下来M行每行包含两个正整数A[i],B[i](1<=A[i],B[i]<=N,A[i]<>B[i]),表示A[i]到B[i]有一条边。

Output

The first and only line of the standard output should contain two integers separated by a single space. The first of these should be the number of the intersection that the motorcyclists should block, and the second - the maximum number of streets that the cyclists can then ride along in their rally. If there are many solutions, your program can choose one of them arbitrarily.
包含一行两个整数x,y,用一个空格隔开,x为要删去的点,y为删除x后图中的最长路径的长度,如果有多组解请输出任意一组。

Sample Input

6 5
1 3
1 4
3 6
3 4
4 5

Sample Output

1 2

HINT

Source

(神)思路题
由于没有确定最长路的起点和终点,考虑新建S和T,f[i]表示S到i的最长路,g[i]表示i到T的最长路
这样一条边的权值就是f[u]+g[v]+1
这道题的做法是这样的
拓扑排序
维护一个堆
如果当前删除了i
那么在它之前的f加入堆,之后的g加入堆,以及跨过两边的边的权值加入堆,堆顶就是删除i之后的最长路
考虑这个做法的正确性
对于一条最长路,如果i不在这上面,那么i的前驱(拓扑序)和后继在i的两边,有边连接(当然在一边的时候f和g也加入了答案)
而如果i在最长路上,那么就会在i这个位置断开
(似乎可以理解成一个割集?)
#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define fill( x, y ) memset( x, y, sizeof x )
#define copy( x, y ) memcpy( x, y, sizeof x )
using namespace std;

typedef long long LL;
typedef pair < int, int > pa;

const int MAXN = 500010;
const int MAXM = 2000020;
const int INF = 1e9 + 7;

struct Priority_queue
{
	priority_queue < int > a, b;
	inline void push(int x) { a.push( x ); }
	inline void pop(int x) { b.push( x ); }
	inline int top() { while( b.size() && a.top() == b.top() ) a.pop(), b.pop(); return a.top(); }
}Q;

struct edge{ int to, nxt; }e[MAXM];
int head[MAXN], head2[MAXN], f[MAXN], g[MAXN], ans = INF, pos, d[MAXN], e_cnt, n, m, q[MAXN], ql, qr;

inline void addedge(int x, int y, int *head) { e[ ++e_cnt ] = { y, head[ x ] }; head[ x ] = e_cnt; }

int main()
{
#ifdef wxh010910
	freopen( "data.in", "r", stdin );
#endif
	scanf( "%d%d", &n, &m );
	while( m-- )
	{
		int x, y;
		scanf( "%d%d", &x, &y );
		addedge( x, y, head ); addedge( y, x, head2 );
		d[ y ]++;
	}
	for( int i = 1 ; i <= n ; i++ ) if( !d[ i ] ) q[ ++qr ] = i;
	while( ql ^ qr )
	{
		int x = q[ ++ql ];
		for( int i = head[ x ] ; i ; i = e[ i ].nxt )
			if( !--d[ e[ i ].to ] ) q[ ++qr ] = e[ i ].to;
	}
	for( int i = 1 ; i <= n ; i++ ) f[ i ] = g[ i ] = 1;
	for( int i = 1 ; i <= n ; i++ )
	{
		int x = q[ i ];
		for( int j = head[ x ] ; j ; j = e[ j ].nxt ) f[ e[ j ].to ] = max( f[ e[ j ].to ], f[ x ] + 1 );
	}
	for( int i = n ; i ; i-- )
	{
		int x = q[ i ];
		for( int j = head2[ x ] ; j ; j = e[ j ].nxt ) g[ e[ j ].to ] = max( g[ e[ j ].to ], g[ x ] + 1 );
	}
	for( int i = 1 ; i <= n ; i++ ) Q.push( g[ i ] ); Q.push( 0 );
	for( int i = 1 ; i <= n ; i++ )
	{
		int x = q[ i ];
		for( int j = head2[ x ] ; j ; j = e[ j ].nxt ) Q.pop( f[ e[ j ].to ] + g[ x ] );
		Q.pop( g[ x ] );
		if( ans > Q.top() ) ans = Q.top(), pos = x;
		for( int j = head[ x ] ; j ; j = e[ j ].nxt ) Q.push( f[ x ] + g[ e[ j ].to ] );
		Q.push( f[ x ] );
	}
	return printf( "%d %d\n", pos, ans - 1 ), 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值