zoukankan      html  css  js  c++  java
  • 1023: [SHOI2008]cactus仙人掌图

    Description

    如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人图(cactus)。所谓简单回路就是指在图上不重复经过任何一个顶点的回路。 举例来说,上面的第一个例子是一张仙人图,而第二个不是——注意到它有三条简单回路:(4,3,2,1,6,5,4)、(7,8,9,10,2,3,7)以及(4,3,7,8,9,10,2,1,6,5,4),而(2,3)同时出现在前两个的简单回路里。另外,第三张图也不是仙人图,因为它并不是连通图。显然,仙人图上的每条边,或者是这张仙人图的桥(bridge),或者在且仅在一个简单回路里,两者必居其一。定义在图上两点之间的距离为这两点之间最短路径的距离。定义一个图的直径为这张图相距最远的两个点的距离。现在我们假定仙人图的每条边的权值都是1,你的任务是求出给定的仙人图的直径。
    Input

    输入的第一行包括两个整数n和m(1≤n≤50000以及0≤m≤10000)。其中n代表顶点个数,我们约定图中的顶点将从1到n编号。接下来一共有m行。代表m条路径。每行的开始有一个整数k(2≤k≤1000),代表在这条路径上的顶点个数。接下来是k个1到n之间的整数,分别对应了一个顶点,相邻的顶点表示存在一条连接这两个顶点的边。一条路径上可能通过一个顶点好几次,比如对于第一个样例,第一条路径从3经过8,又从8返回到了3,但是我们保证所有的边都会出现在某条路径上,而且不会重复出现在两条路径上,或者在一条路径上出现两次。
    Output

    只需输出一个数,这个数表示仙人图的直径长度。
    Sample Input
    15 3
    9 1 2 3 4 5 6 7 8 3
    7 2 9 10 11 12 13 10
    5 2 14 9 15 10 8
    10 1
    10 1 2 3 4 5 6 7 8 9 10
    Sample Output
    9
    HINT

    对第一个样例的说明:如图,6号点和12号点的最短路径长度为8,所以这张图的直径为8。 【注意】使用Pascal语言的选手请注意:你的程序在处理大数据的时候可能会出现栈溢出。如果需要调整栈空间的大小,可以在程序的开头填加一句:{$M 5000000},其中5000000即指代栈空间的大小,请根据自己的程序选择适当的数值。

    做出了这道题,我感觉十分傲(zi)娇(bei),因为我是靠自(ti)己(jie)做出来的

    先贴两个大牛的题解

    http://z55250825.blog.163.com/blog/static/150230809201412793151890/

    http://ydcydcy1.blog.163.com/blog/static/21608904020131493113160/

    先考虑没有环的情况,我们可以直接树dp求出最长连

    然后有环了,因为是仙人掌图,所以环与环是独立的

    先随便dfs出一颗树,然后做树dp

    想一想我们一开始是怎么做树dp的,我们用一个f[i]表示从i点往下延伸的距离的最大值,加了环之后,直接做就可能是错的,因为环可能会把距离变小

    为了使f[i]仍然有效,我们要对每一个环单独处理

    tarjan的时候把环上所有的f[i]求出来(先无视所有环上的边),然后对环进行dp,选两个点用f[i]+f[j]+dis[i,j]更新ans,dis就是环上最短距离

    最后用f[i]+dis[i,root]更新f[root],root是这个环最高的点,其他的点不用更新因为已经遍历完了,他们的f就没用了,f[root]的值还要上传所以要更新

    环上的dp可以用单调队列维护

    就这些了

    具体操作可以看一下代码

      1 {M$ 5000000}
      2 const
      3     maxn=50010;
      4 var
      5     n,m,num,ans,tot:longint;
      6     f,fa,sum,dfn,low,first:array[0..maxn]of longint;
      7     a,q:array[0..maxn*2]of longint;
      8     next,last:array[0..maxn*100]of longint;
      9 
     10 procedure insert(x,y:longint);
     11 begin
     12     inc(tot);
     13     last[tot]:=y;
     14     next[tot]:=first[x];
     15     first[x]:=tot;
     16 end;
     17 
     18 procedure init;
     19 var
     20     i,j,k,x,y:longint;
     21 begin
     22     read(n,m);
     23     for i:=1 to m do
     24       begin
     25         read(k);
     26         read(x);
     27         for j:=1 to k-1 do
     28           begin
     29             read(y);
     30             insert(x,y);
     31             insert(y,x);
     32             x:=y;
     33           end;
     34       end;
     35 end;
     36 
     37 function min(x,y:longint):longint;
     38 begin
     39     if x<y then exit(x);
     40     exit(y);
     41 end;
     42 
     43 function max(x,y:longint):longint;
     44 begin
     45     if x>y then exit(x);
     46     exit(y);
     47 end;
     48 
     49 procedure dp(root,p:longint);
     50 var
     51     n,front,rear,i:longint;
     52 begin
     53     n:=sum[p]-sum[root]+1;
     54     front:=1;
     55     rear:=1;
     56     i:=p;
     57     while i<>root do
     58       begin
     59         a[n]:=f[i];
     60         dec(n);
     61         i:=fa[i];
     62       end;
     63     a[n]:=f[root];
     64     n:=sum[p]-sum[root]+1;
     65     for i:=1 to n do
     66       a[i+n]:=a[i];
     67     q[front]:=1;
     68     for i:=2 to n+n>>1 do
     69       begin
     70         while (front<=rear) and (q[front]<i-n>>1) do
     71           inc(front);
     72         ans:=max(ans,a[q[front]]+a[i]+i-q[front]);
     73         while (front<=rear) and (a[q[rear]]-q[rear]<=a[i]-i) do
     74           dec(rear);
     75         inc(rear);
     76         q[rear]:=i;
     77       end;
     78     for i:=2 to n do
     79       f[root]:=max(f[root],a[i]+min(i-1,n-i+1));
     80 end;
     81 
     82 procedure tarjan(p:longint);
     83 var
     84     i,q:longint;
     85 begin
     86     inc(num);
     87     dfn[p]:=num;
     88     low[p]:=num;
     89     i:=first[p];
     90     while i<>0 do
     91       if last[i]<>fa[p] then
     92       begin
     93         q:=last[i];
     94         if dfn[q]=0 then
     95         begin
     96           fa[q]:=p;
     97           sum[q]:=sum[p]+1;
     98           tarjan(q);
     99         end;
    100         low[p]:=min(low[p],low[q]);
    101         if dfn[p]<low[q] then
    102         begin
    103           ans:=max(ans,f[p]+f[q]+1);
    104           f[p]:=max(f[p],f[q]+1);
    105         end;
    106         i:=next[i];
    107       end
    108       else i:=next[i];
    109     i:=first[p];
    110     while i<>0 do
    111       begin
    112         if (fa[last[i]]<>p) and (dfn[p]<dfn[last[i]]) then dp(p,last[i]);
    113         i:=next[i];
    114       end;
    115 end;
    116 
    117 begin
    118     init;
    119     tarjan(1);
    120     write(ans);
    121 end.
    View Code
  • 相关阅读:
    tomcat createSecureRandom 花费了将近10分钟
    tcpdump取数据保存
    Linux TOP命令 按内存占用排序和按CPU占用排序
    在四合院里写code是什么体验(非拉仇恨)
    严重的抑郁与焦虑症
    CodeIgniter 下引入ORM Doctrine
    linux下解压
    ssh 使用密钥与登录进行远程cp
    mac os 下的sublime --- 快捷键
    pixelmator处理png图片,处理掉过白的留白。
  • 原文地址:https://www.cnblogs.com/Randolph87/p/3627744.html
Copyright © 2011-2022 走看看