【刷题】BZOJ 3832 [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

Solution

一道神题(而且好像BZOJ上这段时间没开SPJ)

2:由于新换judge程序存在一些问题,致使OJ需要Spj的题提交失效,请不要提交此类试题,Bug正在排查中

这题没用主席树,但用了权值线段树
建一个源点和汇点
拓扑排序后,用dp的方法求得图上正向边的最长路\(d[0]\)数组和反向边的最长路\(d[1]\)数组,类似于SPFA的\(d\)数组,但不要用SPFA求。本来我用的SPFA,结果T掉了
那么对于每一条边,一定包含这条边的图上的最长路就是这条边的出发点的\(d[0]\)加上这条边到达点的\(d[1]\),我们把这个值当做这条边的权值
那么删去一个点的话,就把以这个点为到达点的边的权值在权值线段树里删掉,然后就维护了删去了这个点后的最长路
更新答案后再把以这个点为出发点的边的权值加到权值线段树里
每次删掉一个点的时候并没有把与它相连的所有边都删掉,这样省时间

PS:这题还有一些地方没理解透,以后还会填坑

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

原文地址:https://www.cnblogs.com/hongyj/p/8622812.html

时间: 2024-11-08 22:42:43

【刷题】BZOJ 3832 [Poi2014]Rally的相关文章

bzoj 3832: [Poi2014]Rally(线段树+拓扑排序)

3832: [Poi2014]Rally Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special Judge Submit: 113  Solved: 56 [Submit][Status][Discuss] Description An annual bicycle rally will soon begin in Byteburg. The bikers of Byteburg are natural long distance cyclis

BZOJ 3832: [Poi2014]Rally

Sol 线段树+拓扑序. 先把图的拓扑序搞出来,然后统计从起点到该点最长链,从该点到终点的最长链,然后建个起点终点,这里跟网络流很像,把它统一到一个有起点的图中,这里也要注意下细节处理.S,T的一个边割掉后最长链就是答案. 然后一开始所有点都在T的集合中,一个个将点加入S集合,用线段树维护每个节点 (从起点到该点最长链+从终点到该点的最长链)的长度,其实就是一个权值线段树,然后就是加加减减的... Code /*******************************************

BZOJ第一页刷题计划

BZOJ第一页刷题计划 已完成:1 / 100 BZOJ1000:A+B

BZOJ 刷题记录 PART 4

[BZOJ1143]CTSC的题目...先用floyed传递闭包,然后直接上匈牙利算法. [BZOJ1452]从未写过的二维树状数组.好像很简单.. struct two_bit { int f[305][305]; inline void add(int x,int z,int A) { for (;x<=n;x+=L(x)) for (int y=z;y<=m;y+=L(y)) f[x][y]+=A; } inline int ask(int x,int z) { int ans=0; f

BZOJ 刷题记录 PART 5

拖了好久才写的. [BZOJ2821]接触分块大法.这道题略有点新颖.首先我们先分块,然后统计每块中每个数出现的个数. 下面是联立各个方块,预处理出第I个方块到第J个方块出现正偶数次数的个数. for (i=1;i<=s;i++) { for (j=i;j<=s;j++) { sum[i][j]=sum[i][j-1]; for (k=a[j].l;k<=a[j].r;k++) { temp[data[k]]++; if (!(temp[data[k]]&1)) sum[i][j

BZOJ 刷题记录 PART 1

作者 : Dolphin 原文地址:http://blog.csdn.net/qingdujun/article/details/27109035 一.实体完整性定义 [例1]将Student表中的Sno属性定义为码. CREATE TABLE Student ( Sno CHAR(10) PRIMARY KEY, /*在列定义主码*/ Sname CHAR(20) NOT NULL, Sage SMALLINT ); 或者: CREATE TABLE Student ( Sno CHAR(10

BZOJ 刷题记录 PART 2

[前言]最近感觉状态不错.做题几乎不看题解了.(一群大牛(FZ&WCY)在旁边喷:你刷水题有意思!)但是至少这也是一种进步吧.特别是权限题中有很多思维题. [BZOJ1055]就是一个简单的区间DP.重要代码: for (l=2;l<=L;l++) for (i=1;i<=L-l+1;i++) { j=i+l-1; for (k=0;k<4;k++) for (cut=i;cut<j;cut++) for (p=0;p<4;p++) if (f[i][cut][p])

BZOJ 刷题记录 PART 3

[前言]还是强调要少看题解. [BZOJ1090]简单的区间DP.值得注意的是:在压缩的时候,如果是10个A压缩,那么化成(10)A后有5个字符而不是4个!(我在这里被坑了好长时间!)以下是核心代码: for (len=2;len<=L;len++) for (i=1;i<=L-len+1;i++) { j=i+len-1; for (k=i;k<j;k++) f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]); for (l=1;l<=len/2;l++

【刷题】BZOJ 3365 [Usaco2004 Feb]Distance Statistics 路程统计

Description 在得知了自己农场的完整地图后(地图形式如前三题所述),约翰又有了新的问题.他提供 一个整数K(1≤K≤109),希望你输出有多少对农场之间的距离是不超过K的. Input 第1到I+M行:与前三题相同: 第M+2行:一个整数K. Output 农场之间的距离不超过K的对数. Sample Input 7 6 1 6 13 E 6 3 9 E 3 5 7 S 4 1 3 N 2 4 20 W 4 7 2 S 10 Sample Output 5 有五对道路之间的距离小于10