题目描述

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

输入输出样例

输入样例#1:

6 5

1 3

1 4

3 6

3 4

4 5

输出样例#1:

1 2

题解

一道神题

这题没用主席树,但用了权值线段树

建一个源点和汇点

拓扑排序后,用dp的方法求得图上正向边的最长路\(d[0]\)数组和反向边的最长路\(d[1]\)数组,类似于SPFA的\(d\)数组,但不要用SPFA求。本来我用的SPFA,结果T掉了

那么对于每一条边,一定包含这条边的图上的最长路就是这条边的出发点的\(d[0]\)加上这条边到达点的\(d[1]\),我们把这个值当做这条边的权值

那么删去一个点的话,就把以这个点为到达点的边的权值在权值线段树里删掉,然后就维护了删去了这个点后的最长路(存的就是跨过了要删去的点的路径的长度)

更新答案后再把以这个点为出发点的边的权值加到权值线段树里

(每次删掉一个点的时候并没有把与它相连的所有边都删掉,这样省时间)

#include<bits/stdc++.h>
#define ll long long
#define db double
#define ld long double
#define Mid ((l+r)>>1)
#define lson rt<<1,l,Mid
#define rson rt<<1|1,Mid+1,r
const int MAXM=2000000+10,MAXN=2000000+10,inf=0x3f3f3f3f;
int n,m,e[2],beg[2][MAXN],nex[2][MAXM],to[2][MAXM],w[2][MAXM],s,t,d[2][MAXN],p[MAXN],degree[MAXN],ans=inf,num,topo[MAXN],cnt;
std::queue<int> q;
struct Q_Tree{
int Max[MAXM],Num[MAXM];
inline void PushUp(int rt)
{
if(Max[rt<<1]>Max[rt<<1|1])Max[rt]=Max[rt<<1],Num[rt]=Num[rt<<1];
else Max[rt]=Max[rt<<1|1],Num[rt]=Num[rt<<1|1];
}
inline void Insert(int rt,int l,int r,int pos)
{
if(l==r)Max[rt]=pos,Num[rt]++;
else
{
if(pos<=Mid)Insert(lson,pos);
else Insert(rson,pos);
PushUp(rt);
}
}
inline void Delete(int rt,int l,int r,int pos)
{
if(l==r)
{
Num[rt]--;
if(!Num[rt])Max[rt]=0;
}
else
{
if(pos<=Mid)Delete(lson,pos);
else Delete(rson,pos);
PushUp(rt);
}
}
};
Q_Tree T;
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char c='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(c!='\0')putchar(c);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void insert(int x,int y,int z)
{
to[0][++e[0]]=y;
nex[0][e[0]]=beg[0][x];
beg[0][x]=e[0];
w[0][e[0]]=z;
to[1][++e[1]]=x;
nex[1][e[1]]=beg[1][y];
beg[1][y]=e[1];
w[1][e[1]]=z;
}
inline void toposort()
{
for(register int i=1;i<=n;++i)
if(!degree[i])q.push(i);
while(!q.empty())
{
int x=q.front();
q.pop();
topo[++cnt]=x;
for(register int i=beg[0][x];i;i=nex[0][i])
{
degree[to[0][i]]--;
if(!degree[to[0][i]])q.push(to[0][i]);
}
}
}
int main()
{
read(n);read(m);
s=n+1;t=n+2;
for(register int i=1;i<=m;++i)
{
int u,v;
read(u);read(v);
degree[v]++;
insert(u,v,1);
}
toposort();
for(register int p=n;p>=1;--p)
for(register int x=topo[p],i=beg[0][x];i;i=nex[0][i])chkmax(d[1][x],d[1][to[0][i]]+1);
for(register int p=1;p<=n;++p)
for(register int x=topo[p],i=beg[0][x];i;i=nex[0][i])chkmax(d[0][to[0][i]],d[0][x]+1);
for(register int i=1;i<=n;++i)insert(s,i,0),insert(i,t,0);
for(register int i=1;i<=n;++i)T.Insert(1,1,n+2,d[1][i]);
d[0][s]=d[1][t]=-1;
for(register int t=1;t<=n;++t)
{
int x=topo[t];
for(register int i=beg[1][x];i;i=nex[1][i])T.Delete(1,1,n+2,d[0][to[1][i]]+d[1][x]+1);
if(T.Max[1]<ans)ans=T.Max[1],num=x;
for(register int i=beg[0][x];i;i=nex[0][i])T.Insert(1,1,n+2,d[0][x]+d[1][to[0][i]]+1);
}
write(num,' '),write(ans,'\n');
return 0;
}

【刷题】洛谷 P3573 [POI2014]RAJ-Rally的更多相关文章

  1. 洛谷 P3573 [POI2014]RAJ-Rally 解题报告

    P3573 [POI2014]RAJ-Rally 题意: 给定一个\(N\)个点\(M\)条边的有向无环图,每条边长度都是\(1\). 请找到一个点,使得删掉这个点后剩余的图中的最长路径最短. 输入输 ...

  2. 【刷题】BZOJ 4543 [POI2014]Hotel加强版

    Description 同OJ3522 数据范围:n<=100000 Solution dp的设计见[刷题]BZOJ 3522 [Poi2014]Hotel 然后发现dp的第二维与深度有关,于是 ...

  3. 洛谷 P3580 - [POI2014]ZAL-Freight(单调队列优化 dp)

    洛谷题面传送门 考虑一个平凡的 DP:我们设 \(dp_i\) 表示前 \(i\) 辆车一来一回所需的最小时间. 注意到我们每次肯定会让某一段连续的火车一趟过去又一趟回来,故转移可以枚举上一段结束位置 ...

  4. 2018.10.30 一题 洛谷4660/bzoj1168 [BalticOI 2008]手套——思路!问题转化与抽象!+单调栈

    题目:https://www.luogu.org/problemnew/show/P4660 https://www.lydsy.com/JudgeOnline/problem.php?id=1168 ...

  5. 洛谷 P3576 [POI2014]MRO-Ant colony

    P3576 [POI2014]MRO-Ant colony 题目描述 The ants are scavenging an abandoned ant hill in search of food. ...

  6. 洛谷P3576 [POI2014]MRO-Ant colony [二分答案,树形DP]

    题目传送门 MRO-Ant colony 题目描述 The ants are scavenging an abandoned ant hill in search of food. The ant h ...

  7. 洛谷P3567[POI2014]KUR-Couriers(主席树+二分)

    题意:给一个数列,每次询问一个区间内有没有一个数出现次数超过一半 题解: 最近比赛太多,都没时间切水题了,刚好日推了道主席树裸题,就写了一下 然后 WA80 WA80 WA0 WA90 WA80 ?? ...

  8. AC日记——大爷的字符串题 洛谷 P3709

    大爷的字符串题 思路: 莫队,需开O2,不开50: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 20000 ...

  9. 洛谷P3572 [POI2014]PTA-Little Bird

    P3572 [POI2014]PTA-Little Bird 题目描述 In the Byteotian Line Forest there are nn trees in a row. On top ...

随机推荐

  1. 1、AutoCAD ObjectARX开发版本对照表

    ObjectARX开发版本对照表 序号 CAD版本 版本号 二进制兼容 .net框架 ObjectARX开发环境 VC版本号 MAC OS平台 WINDOWS平台 VC版本 _MSC_VER 1 R1 ...

  2. Catlike学习笔记(1.2)-使用Unity画函数图像

    『Catlike系列教程』第二篇来了~今天周六,早上(上午11点)醒来去超市买了一周的零食回来以后就玩了一整天游戏非常有负罪感.现在晚上九点天还亮着感觉像下午7点左右的样子好像还不是很晚...所以就写 ...

  3. springboot 前后端分离开发 从零到整(三、登录以及登录状态的持续)

    今天来写一下怎么登录和维持登录状态. 相信登录验证大家都比较熟悉,在Javaweb中一般保持登录状态都会用session.但如果是前后端分离的话,session的作用就没有那么明显了.对于前后端分离的 ...

  4. Aria2 Linux 完整安装及使用教程

    Aria2 嘛,主要是用来离线下载,功能强大,支持 http/https 直链.ftp.电驴.磁力链接等等,且可以跨平台使用,配合网页端操作,简直是一代下载神器. 安装 Debian/Ubuntu: ...

  5. Gitlab CI-2.CI流程

    参考文档: GitLab Documentation:https://docs.gitlab.com/ce/ Installation and Configuration using omnibus ...

  6. 如何批量删除QQ浏览器指定历史记录和导出指定的历史记录

    QQ浏览器的历史记录只有清空历史记录和删除选中项两个功能.有时我不想删除所有的历史记录,只是想删除指定的历史记录保留对自己有用的历史记录,方便自己以后查找.但是删除选中项功能只能一项一项的选择,才能批 ...

  7. 记一次centos6升级salt-minion启动失败的问题

    记一次centos6升级salt-minion启动失败的问题 作者:耀耀 blog:https://www.liuyao.me 一.起因 升级Salt-minion后 使用/etc/init.d/sa ...

  8. java中重要的多线程工具类

    前言 之前学多线程的时候没有学习线程的同步工具类(辅助类).ps:当时觉得暂时用不上,认为是挺高深的知识点就没去管了.. 在前几天,朋友发了一篇比较好的Semaphore文章过来,然后在浏览博客的时候 ...

  9. Python之并发编程-协程

    目录 一.介绍 二. yield.greenlet.gevent介绍 1.yield 2.greenlet 3.gevent 一.介绍 协程:是单线程下的并发,又称微线程,纤程.英文名Coroutin ...

  10. jaxb教程(忘记了过来看看)

    链接 原文链接