题目描述

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。
请找到一个点,使得删掉这个点后剩余的图中的最长路径最短。

输入

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]有一条边。

输出

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后图中的最长路径的长度,如果有多组解请输出任意一组。

样例输入

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

样例输出

1 2
 
如果不删点求最长路很好求,直接拓扑排序求出每个点为起点的最长路取max即可。
但现在要求删点,那么就按拓扑序删点,删除经过删除点的最长路,也就是删除经过删除点的入边的最长路。
需要维护出以每个点为起点和终点的最长路,那么删除经过一条边(x,y)的最长路就是end[x]+1+start[y]。
每次删除点之后取一下剩下最长路中的最大值来更新答案。再将经过删除点出边的最长路加上就好了。
删点时只要用一个支持删除、插入、求最大值的数据结构即可。
那么为什么要按拓扑序删点?
因为只有按拓扑序删除插入才能保证不会删多或者删少。
如果随便顺序删除的话,可能一个点的入边只有一条,但经过这条入边的最长路却有很多条导致删少了。
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
int n,m;
int s[500010];
int t[500010];
int sum[4000010];
int p[500010];
int cnt;
int x,y;
int in[500010];
int out[500010];
vector<int>g[500010];
vector<int>h[500010];
queue<int>q;
int ans1,ans2;
void change(int rt,int l,int r,int k,int v)
{
if(l==r)
{
sum[rt]+=v;
return ;
}
int mid=(l+r)>>1;
if(k<=mid)
{
change(rt<<1,l,mid,k,v);
}
else
{
change(rt<<1|1,mid+1,r,k,v);
}
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
int query(int rt,int l,int r)
{
if(l==r)
{
return l;
}
int mid=(l+r)>>1;
if(sum[rt<<1|1]>0)
{
return query(rt<<1|1,mid+1,r);
}
else
{
return query(rt<<1,l,mid);
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
out[x]++;
in[y]++;
g[x].push_back(y);
h[y].push_back(x);
}
for(int i=1;i<=n;i++)
{
if(!in[i])
{
q.push(i);
p[++cnt]=i;
}
}
while(!q.empty())
{
int now=q.front();
q.pop();
int len=g[now].size();
for(int j=0;j<len;j++)
{
in[g[now][j]]--;
s[g[now][j]]=max(s[g[now][j]],s[now]+1);
if(!in[g[now][j]])
{
q.push(g[now][j]);
p[++cnt]=g[now][j];
}
}
}
for(int i=1;i<=n;i++)
{
if(!out[i])
{
q.push(i);
}
}
while(!q.empty())
{
int now=q.front();
q.pop();
int len=h[now].size();
for(int j=0;j<len;j++)
{
out[h[now][j]]--;
t[h[now][j]]=max(t[h[now][j]],t[now]+1);
if(!out[h[now][j]])
{
q.push(h[now][j]);
}
}
}
int S=n+1;
int T=n+2;
for(int i=1;i<=n;i++)
{
g[i].push_back(T);
g[S].push_back(i);
h[i].push_back(S);
h[T].push_back(i);
change(1,0,n,t[i],1);
}
s[S]=-1;
t[T]=-1;
ans2=n+2;
for(int i=1;i<=cnt;i++)
{
int now=p[i];
int len=h[now].size();
for(int j=0;j<len;j++)
{
change(1,0,n,s[h[now][j]]+1+t[now],-1);
}
int res=query(1,0,n);
if(res<ans2)
{
ans2=res;
ans1=now;
}
len=g[now].size();
for(int j=0;j<len;j++)
{
change(1,0,n,s[now]+1+t[g[now][j]],1);
}
}
printf("%d %d\n",ans1,ans2);
}

BZOJ3832[Poi2014]Rally——权值线段树+拓扑排序的更多相关文章

  1. [bzoj3524==bzoj2223][Poi2014]Couriers/[Coci 2009]PATULJCI——主席树+权值线段树

    题目大意 给定一个大小为n,每个数的大小均在[1,c]之间的数列,你需要回答m个询问,其中第i个询问形如\((l_i, r_i)\),你需要回答是否存在一个数使得它在区间\([l_i,r_i]\)中出 ...

  2. HDU-6704 K-th occurrence (后缀自动机father树上倍增建权值线段树合并)

    layout: post title: HDU-6704 K-th occurrence (后缀自动机father树上倍增建权值线段树合并) author: "luowentaoaa&quo ...

  3. 【树状数组套权值线段树】bzoj1901 Zju2112 Dynamic Rankings

    谁再管这玩意叫树状数组套主席树我跟谁急 明明就是树状数组的每个结点维护一棵动态开结点的权值线段树而已 好吧,其实只有一个指针,指向该结点的权值线段树的当前结点 每次查询之前,要让指针指向根结点 不同结 ...

  4. 【BZOJ-2892&1171】强袭作战&大sz的游戏 权值线段树+单调队列+标记永久化+DP

    2892: 强袭作战 Time Limit: 50 Sec  Memory Limit: 512 MBSubmit: 45  Solved: 30[Submit][Status][Discuss] D ...

  5. BZOJ 3110 ZJOI 2013 K大数查询 树套树(权值线段树套区间线段树)

    题目大意:有一些位置.这些位置上能够放若干个数字. 如今有两种操作. 1.在区间l到r上加入一个数字x 2.求出l到r上的第k大的数字是什么 思路:这样的题一看就是树套树,关键是怎么套,怎么写.(话说 ...

  6. 动态求区间K大值(权值线段树)

    我们知道我们可以通过主席树来维护静态区间第K大值.我们又知道主席树满足可加性,所以我们可以用树状数组来维护主席树,树状数组的每一个节点都可以开一颗主席树,然后一起做. 我们注意到树状数组的每一棵树都和 ...

  7. 线段树(单标记+离散化+扫描线+双标记)+zkw线段树+权值线段树+主席树及一些例题

    “队列进出图上的方向 线段树区间修改求出总量 可持久留下的迹象 我们 俯身欣赏” ----<膜你抄>     线段树很早就会写了,但一直没有总结,所以偶尔重写又会懵逼,所以还是要总结一下. ...

  8. 【BZOJ3685】【zkw权值线段树】普通van Emde Boas树

    原题传送门 因为马上要开始搞树套树了,所以学了一波权值线段树...毕竟是会点zkw线段树的,所以zkw线段树大法好! 解题思路: 介绍一下权值线段树吧,其实感觉就是线段树的本义,就是你用线段树维护了数 ...

  9. BZOJ_2161_布娃娃_权值线段树

    BZOJ_2161_布娃娃_权值线段树 Description 小时候的雨荨非常听话,是父母眼中的好孩子.在学校是老师的左右手,同学的好榜样.后来她成为艾利斯顿第二 代考神,这和小时候培养的良好素质是 ...

随机推荐

  1. 【ZOJ 3463】Piano

    ZOJ 3463 题意:有一个钢琴,一个人把左手放在L位置上,右手放在R位置上,要弹某\(n\)个键,每个手最多能够得着9个位置,并且两只手不能交叉.把手移动的代价是大拇指移动的距离的平方根.问弹完这 ...

  2. Luogu P1525 关押罪犯

    传送门 首先 这是一个并查集= = 这道题其实明白了还挺简单的qwq 思路: 因为只看仇恨值最大的一对儿,所以把他们从大到小排序,越大的就尽量分开,直到不能再分为止qwq q[x]表示x最大的敌人(x ...

  3. Android ExpandableListView和ScrollView联用的一些注意事项

    之前有整理过ScrollView嵌套ListView的例子,讲的是计算listview的每一项的高度.已达到目标效果.同样的ExpandableListView嵌套ScrollView也是这么个思路, ...

  4. linux下比较两个文件:diff、 vimdiff

     diff更加具体的命令,比如file1, file2 >  diff -u file1 file2 >  vimdiff file1 file2 vimdiff 有点类似于  vim - ...

  5. POJ3301 Texas Trip 计算几何、随机化贪心

    传送门--Vjudge 三分写法似乎有问题,可以去Udebug上看Morass的\(666\)个测试点的数据,我的乱搞有很多比正解答案小,但还是能在SPOJ和POJ过,可见数据之水. 可以对正方形的角 ...

  6. UVA1265 Tour Belt Kruskal重构树、倍增、树上差分

    题目传送门 题意:定义$Tour \, Belt$为某张图上的一个满足以下条件的点集:①点集中至少有$2$个点②任意两点互相连通③图上两个端点都在这个点集中的边的权值的最小值严格大于图上只有一个端点在 ...

  7. ES6-课程介绍

    ES6 可以提高开发效率,把ES3比做斧头,ES5比做锯子,那么ES6就是电锯. ES6新特性 默认参数.字符串模板.结构赋值.箭头函数.set\mat .异步操作.类和对象 .模块化

  8. [Oracle][DataGuard]Standby数据库文件有损坏时的处理方法

    需要参考: [Oracle]Recovering the primary database's datafile using the physical standby, and vice versa ...

  9. 解读tensorflow之rnn

    from: http://lan2720.github.io/2016/07/16/%E8%A7%A3%E8%AF%BBtensorflow%E4%B9%8Brnn/ 这两天想搞清楚用tensorfl ...

  10. .Net版本依赖之坑引发的搜查

    前言 今天上午,一个客户反馈XX消息没有推送到第三方链接.于是我查看了推送日志列表,并没有今天的.接着登录服务器查询文件日志,看到了记录.我们的代码步骤是消息先推送到消息队列,消费消息队列时,记录文件 ...