1. 程式人生 > >bzoj 1023[SHOI2008]cactus仙人掌圖

bzoj 1023[SHOI2008]cactus仙人掌圖

spl head 有一個 efi mage sim 兩個 turn log

1023: [SHOI2008]cactus仙人掌圖

Time Limit: 1 Sec Memory Limit: 162 MB

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

8
9

HINT

對第一個樣例的說明:如圖,6號點和12號點的最短路徑長度為8,所以這張圖的直徑為8。


技術分享圖片

毒瘤仙人掌

關於仙人掌的學習可以參考WC2017 cjk 大神的課件

http://immortalco.blog.uoj.ac/blog/1955

技術分享圖片
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4
#include <algorithm> 5 #include <vector> 6 #define LL long long 7 8 using namespace std; 9 10 const int MAXN = 1e6 + 10; 11 int N, M; 12 int u, v, w; 13 int ans = 0; 14 int cnt = 0; 15 int num = 0; 16 int hd = 0, tail = 0; 17 int head[MAXN], head2[MAXN]; 18 int from[MAXN]; 19 int len[MAXN]; 20 int q[MAXN]; 21 int fa[MAXN]; 22 int dfn[MAXN], low[MAXN]; 23 int dis[MAXN]; 24 int flag[MAXN]; 25 int dp[MAXN]; 26 inline LL read() 27 { 28 LL x = 0, w = 1; char ch = 0; 29 while(ch < 0 || ch > 9) { 30 if(ch == -) { 31 w = -1; 32 } 33 ch = getchar(); 34 } 35 while(ch >= 0 && ch <= 9) { 36 x = x * 10 + ch - 0; 37 ch = getchar(); 38 } 39 return x * w; 40 } 41 struct edge { 42 int v; 43 int next; 44 int w; 45 } g[MAXN], gg[MAXN]; 46 47 void addedge(int u, int v, int w) 48 { 49 g[++cnt].v = v; 50 g[cnt].w = w; 51 g[cnt].next = head[u]; 52 head[u] = cnt; 53 } 54 55 void addedge2(int u, int v, int w) 56 { 57 gg[++cnt].v = v; 58 gg[cnt].w = w; 59 gg[cnt].next = head2[u]; 60 head2[u] = cnt; 61 } 62 void tarjan(int x) 63 { 64 // cout<<x<<endl; 65 dfn[x] = ++cnt; 66 for(int j = head[x]; j; j = g[j].next) { 67 int to = g[j].v; 68 if(fa[x] != to) { 69 if(dfn[to] == 0) { 70 fa[to] = x; 71 dis[to] = dis[x] + 1; 72 tarjan(to); 73 } else if(dfn[to] < dfn[x]){ 74 N++; 75 flag[N] = 1; 76 from[N] = ++num; 77 addedge2(to, N, 0); 78 int tmp = x; 79 len[num] = dis[x] - dis[to] + 1; 80 int tot = 0; 81 while(tmp != to) { 82 tot++; 83 addedge2(N, tmp, min(dis[tmp] - dis[to], len[num] - (dis[tmp] - dis[to]))); 84 from[tmp] = num; 85 tmp = fa[tmp]; 86 } 87 } 88 } 89 } 90 if(from[x] == 0) { 91 addedge2(fa[x], x, 1); 92 } 93 } 94 void DFS(int x) 95 { 96 int fir = 0, sec = 0; 97 for(int j = head2[x]; j; j = gg[j].next) { 98 int to = gg[j].v; 99 DFS(to); 100 if(dp[to] + gg[j].w > sec) { 101 sec = dp[to] + gg[j].w; 102 } 103 if(sec > fir) { 104 swap(sec, fir); 105 } 106 } 107 dp[x] = fir; 108 if(!flag[x]) { 109 ans = max(ans, fir + sec); 110 } else { 111 hd = 0, tail = 0; 112 int b = from[x], mx = -1e9; 113 for(int j = head2[x]; j; j = gg[j].next) { 114 int to = gg[j].v; 115 while(hd < tail && dis[to] - dis[q[hd]] > len[b] / 2) { 116 mx = max(mx, dp[q[hd]] + len[b] + dis[q[hd]]); 117 hd++; 118 } 119 ans = max(ans, mx + dp[to] - dis[to]); 120 if(hd < tail) { 121 ans = max(ans, dp[q[hd]] - dis[q[hd]] + dp[to] + dis[to]); 122 } 123 while(tail > hd && dp[to] - dis[to] > dp[q[tail - 1]] - dis[q[tail - 1]]) { 124 tail--; 125 } 126 q[tail++] = to; 127 } 128 } 129 } 130 int main() 131 { 132 N = read(), M = read(); 133 for(int i = 1; i <= M; i++) { 134 int k = read(); 135 u = read(); 136 for(int j = 1; j < k; j++) { 137 v = read(); 138 addedge(u, v, 1); 139 addedge(v, u, 1); 140 u = v; 141 } 142 } 143 cnt = 0; 144 tarjan(1); 145 DFS(1); 146 /*for(int i = 1; i <= N; i++) { 147 cout<<i<<" "<<dp[i]<<endl; 148 } 149 cout<<endl;*/ 150 printf("%d\n", ans); 151 return 0; 152 } 153 154 /* 155 15 3 156 157 9 1 2 3 4 5 6 7 8 3 158 159 7 2 9 10 11 12 13 10 160 161 5 2 14 9 15 10 162 163 */
View Code

bzoj 1023[SHOI2008]cactus仙人掌圖