5177. 【NOIP2017提高组模拟6.28】TRAVEL (Standard IO)
Description
Input
Output
Solution
有大佬说:可以用LCT做。(会吗?不会)
对于蒟蒻的我,只好用水法(3s,不虚)。
首先选出的泡泡怪一定是连续的一段 L,
R 然后 L 一定属于虫洞左边界中的某一个 R 也同样是这样的 这样就可以枚举 L 和 R,
O(N)判断是否可行,可用并查集, 总复杂度 O(NM^2)。
代码
1 type 2 arr=record 3 x,y,l,r:longint; 4 end; 5 var 6 n,m,tk,ans,ll:longint; 7 a:array [0..3001] of arr; 8 fa:array [0..1001] of longint; 9 function max(o,p:longint):longint; 10 begin 11 if o>p then exit(o); 12 exit(p); 13 end; 14 15 procedure init; 16 var 17 i:longint; 18 begin 19 readln(n,m); 20 tk:=0; 21 for i:=1 to m do 22 begin 23 readln(a[i].x,a[i].y,a[i].l,a[i].r); 24 tk:=max(tk,a[i].r); 25 end; 26 end; 27 28 procedure qsort(l,r:longint); 29 var 30 i,j,mid:longint; 31 t:arr; 32 begin 33 if l>r then exit; 34 i:=l; j:=r; 35 mid:=a[(l+r) div 2].r; 36 repeat 37 while a[i].r<mid do inc(i); 38 while a[j].r>mid do dec(j); 39 if i<=j then 40 begin 41 t:=a[i]; a[i]:=a[j]; a[j]:=t; 42 inc(i); dec(j); 43 end; 44 until i>j; 45 qsort(i,r); 46 qsort(l,j); 47 end; 48 49 function get(x:longint):longint; 50 begin 51 if (fa[x]=0) or (fa[x]=x) then exit(x); 52 fa[x]:=get(fa[x]); 53 exit(fa[x]); 54 end; 55 56 function fd(l,r:longint):boolean; 57 var 58 i,x,y:longint; 59 begin 60 fillchar(fa,sizeof(fa),0); 61 for i:=1 to m do 62 if (a[i].l<=l) and (a[i].r>=r) then 63 begin 64 x:=get(a[i].x); y:=get(a[i].y); 65 fa[x]:=y; 66 end; 67 if get(1)=get(n) then exit(true); 68 exit(false); 69 end; 70 71 procedure main; 72 var 73 i,r,l,mid,t:longint; 74 begin 75 ans:=0; 76 for i:=1 to m do 77 begin 78 l:=1; r:=tk; 79 while l+1<r do 80 begin 81 mid:=(l+r) div 2; 82 if fd(a[i].l,mid) then l:=mid 83 else r:=mid; 84 end; 85 if not fd(a[i].l,r) then r:=l; 86 t:=r-a[i].l+1; 87 if (t>ans) or (t=ans) and (a[i].l<ll) then 88 begin 89 ans:=t; ll:=a[i].l; 90 end; 91 end; 92 end; 93 94 procedure print; 95 var 96 i,rr:longint; 97 begin 98 writeln(ans); 99 rr:=ans+ll-1; 100 for i:=ll to rr do 101 write(i,' '); 102 end; 103 104 begin 105 init; 106 qsort(1,m); 107 main; 108 print; 109 end.