Bzoj 3832: [poi2014]rally (segment tree + topological sort)

Source: Internet
Author: User

3832: [poi2014]rally time limit: Sec Memory Limit: mbsec Special Judge
Submit: 113 Solved: 56
[Submit] [Status] [Discuss] Descriptionan Annual bicycle rally would soon begin in Byteburg. The bikers of Byteburg is natural long distance cyclists. Local representatives of motorcyclists, long feuding the cyclists, has decided to sabotage the event. There is   intersections in Byteburg, connected with one-off streets. Strangely enough, there is no cycles in the-street network-if one can ride from intersection U-intersection V, then It is the definitely impossible to get from V to U.the rally ' s route would lead through Byteburg ' s streets. The motorcyclists plan to ride their blazing machines on the early morning of the rally day to one intersection and comple Tely block it. The Cyclists ' Association would then of course determine a alternative route but it could happen that this new route would Be relatively short, and the cyclists would thus be unable to exhibit their remarkable endurance. Clearly, this was the motorcyclists ' Plan-they intend to block such a intersection that the longest route that does Not pass through it's as short as possible. Given an n-point M-edge of a forward-free graph, each edge length is 1. Please find a point that minimizes the longest path in the remaining graphs after deleting this point.

Inputin the first line of the standard input, there is, integers, N and M (2<=n<=500 000,1<=m<=1 000 000), Separated by a single space, which specify the number of intersections and streets in Byteburg. The intersections is numbered from to. The lines that follow describe the street network:in the-th of these lines, there is both integers, Ai, Bi (1<=ai,b I&LT;=N,AI&LT;&GT;BI), separated by a single space, which signify that there is a one-to-one-street from the intersection No. Ai to the one No. Bi. The first line contains two positive integers n,m (2<=n<=500 000,1<=m<=1 000 000), which represents the number of points and sides. The next M line contains two positive integers a[i],b[i] (1<=a[i],b[i]<=n,a[i]<>b[i]), which means a[i] to B[i] has an edge.

Outputthe first and only line of the standard output should contain, integers separated by a single space. The first of these should is the number of the intersection that the motorcyclists should block, and the Second-the maxi Mum number of streets the cyclists can then ride along in their rally. If There is many solutions, your program can choose one of the them arbitrarily. Contains a row of two integers x, y, separated by a space, X is the point to be deleted, and y is the length of the longest path in the image after deleting X. If there are multiple sets of solutions, output any set.

Sample Input6 5
1 3
1 4
3 6
3 4
4 5
Sample Output1 2
HINT

Source

Acknowledgement Claris provides SPJ and translations

[Submit] [Status] [Discuss] 

Puzzle: Segment Tree + topology sequencing

It's a very magical idea.

F[i] represents the longest path from the starting point to that point

G[i] indicates the longest path from the point to the end (no further walk)

For the side (U,V), then the longest road he can produce is f[u]+1+g[v]

So we can build a tree of weight segments to maintain the longest road after deleting a node.

Start with the g[i of all points] into the segment tree, and then delete each node one at a time in the topology sequence.

When deleting a node, delete all the answers that were updated with his g[i, and then delete the longest road from this point, and why just delete it? Because when we first joined, we only joined the longest road after this point. Then the maximum value in the segment tree is the answer after that point is deleted. So, someone's going to have questions? The longest path on each side of a longest road is the same value that is the same, so just delete this point, will not the maximum value of the other sides on this route affect the answer? In fact, it is not, because we are in accordance with the topological order, the point behind him only joined the G[x], did not join the path of the line of letters.

Then add this point to the edge of the longest path can be added to join the line segment tree, and add f[i] to the line segment tree, because if you later use the point to update the answer, will be selected to the point of the longest road, the other paths are useless. The reason to add the longest path of all out edges is to join the line segment tree, because the point after the point is deleted, it is possible to break the point after the longest road, all need to record all the paths, to ensure that all possible paths to update the answer.

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath> #define N 1000003using namespace Std;int n,m;int next[n],point[n],v[n],f[n],ins[n],q[n],tot;int Next1[n], Point1[n],v1[n],g[n],outs[n],p[n],tot1;int cnt[n*4],maxn[n*4];void Add (int x,int y) {tot++; next[tot]=point[x]; point [X]=tot; V[tot]=y;} void add1 (int x,int y) {tot1++; next1[tot1]=point1[x]; point1[x]=tot1; v1[tot1]=y;} void Solve () {int tail=0; int head=0;for (int i=1;i<=n;i++) if (!ins[i]) q[++tail]=i,f[i]=0;while (head<tail) {int No w=q[++head];for (int i=point[now];i;i=next[i]) {if (f[v[i]]<f[now]+1) f[v[i]]=f[now]+1; if (!--Ins[v[i]]) Q[++tail]    =v[i]; }} tail=0;    Head=0;    for (int i=1;i<=n;i++) if (!outs[i]) p[++tail]=i,g[i]=0;    while (head<tail) {int now=p[++head];     for (int i=point1[now];i;i=next1[i]) {if (g[v1[i]]<g[now]+1) g[v1[i]]=g[now]+1;        if (!--Outs[v1[i]]) p[++tail]=v1[i]; }}}void pointchange (intNow,int l,int r,int X,int v) {if (l==r) {cnt[now]+=v; if (cnt[now]>0) maxn[now]=l; else maxn[now]=-1,cnt[now]=0;//need Attention can not be unrestricted, because it is possible that the weight of more than one, so it will be deleted more than once, but when we join, it must be necessary to add this value, rather than restore the previous lost return; }int mid= (l+r)/2;if (x<=mid) Pointchange (now<<1,l,mid,x,v); else Pointchange (NOW&LT;&LT;1|1,MID+1,R,X,V); Maxn[now]=max (maxn[now<<1],maxn[now<<1|1]);}  int main () {scanf ("%d%d", &n,&m), for (int i=1;i<=m;i++) {int x, Y, scanf ("%d%d", &x,&y), add (x, y); ins[y]++; ADD1 (Y,X); outs[x]++;  }solve (); int ans=1000000000; int ansx=0;    for (int i=1;i<=n;i++) Pointchange (1,0,n,g[i],1);     for (int i=1;i<=n;i++) {int x=q[i];     for (int i=point1[x];i;i=next1[i]) pointchange (1,0,n,g[x]+1+f[v1[i]],-1);     Pointchange (1,0,n,g[x],-1); if (Maxn[1]<ans) ans=maxn[1],ansx=x;for (int i=point[x];i;i=next[i]) pointchange (1,0,n,f[x]+1+g[v[i]],1);      Pointchange (1,0,n,f[x],1); } printf ("%d%d\n", Ansx,ans);}



Bzoj 3832: [poi2014]rally (segment tree + topological sort)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.