日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

灾难 BZOJ 2815

發布時間:2023/11/29 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 灾难 BZOJ 2815 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

災難

【樣例輸入】

5

0

1 0

1 0

2 3 0

2 0

【樣例輸出】

4

1

0

0

0


題解:

先跑出拓撲序

我們按拓撲序建立一棵“滅絕樹”

滅絕樹含義是當一個點滅絕時,它的子樹將會全部滅絕

所以答案就是點在滅絕樹中的子樹大小

一個點如果滅絕,那么需要所有指向它的點滅絕

由于拓撲序的關系,指向它的點已經加入過了"滅絕樹”中

所以這個點要滅絕,就需要所有指向它的點全部滅絕,即這些點的最近公共祖先

那么直接我們將這個祖先與此點連邊,更新Lca

最后求出子樹大小,即統計答案

1 #include<algorithm> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 inline int Get() 9 { 10 int x = 0; 11 char c = getchar(); 12 while('0' > c || c > '9') c = getchar(); 13 while('0' <= c && c <= '9') 14 { 15 x = (x << 3) + (x << 1) + c - '0'; 16 c = getchar(); 17 } 18 return x; 19 } 20 const int me = 1000233; 21 int n; 22 int head, tail; 23 int in[me]; 24 int ue[me]; 25 int de[me]; 26 int si[me]; 27 int fat[me][21]; 28 int tot, nex[2][me], fir[2][me], to[2][me]; 29 inline void Ins(int x, int y, int z) 30 { 31 nex[z][++tot] = fir[z][x]; 32 fir[z][x] = tot; 33 to[z][tot] = y; 34 } 35 inline void Topo() 36 { 37 head = 0, tail = 0; 38 for(int i = 1; i <= n; ++i) 39 if(!in[i]) 40 ue[++tail] = i; 41 while(head < tail) 42 { 43 int u = ue[++head]; 44 for(int i = fir[0][u]; i; i = nex[0][i]) 45 { 46 int v = to[0][i]; 47 --in[v]; 48 if(!in[v]) ue[++tail] = v; 49 } 50 } 51 } 52 inline int Lca(int x, int y) 53 { 54 if(x < 0) return y; 55 if(de[x] < de[y]) swap(x, y); 56 for(int i = 20; i >= 0; --i) 57 if(de[fat[x][i]] >= de[y]) 58 x = fat[x][i]; 59 for(int i = 20; i >= 0; --i) 60 if(fat[x][i] != fat[y][i]) 61 { 62 x = fat[x][i]; 63 y = fat[y][i]; 64 } 65 if(x == y) return x; 66 return fat[x][0]; 67 } 68 inline void Update(int u, int v) 69 { 70 fat[v][0] = u; 71 de[v] = de[u] + 1; 72 for(int i = 1; i <= 20; ++i) 73 fat[v][i] = fat[fat[v][i - 1]][i - 1]; 74 } 75 inline void Build() 76 { 77 while(tail) 78 { 79 int u = ue[tail]; 80 int lca = -1; 81 for(int i = fir[0][u]; i; i = nex[0][i]) 82 { 83 int v = to[0][i]; 84 lca = Lca(lca, v); 85 } 86 if(lca < 0) lca = 0; 87 Ins(lca, u, 1); 88 Update(lca, u); 89 --tail; 90 } 91 } 92 void Ergo(int u) 93 { 94 si[u] = 1; 95 for(int i = fir[1][u]; i; i = nex[1][i]) 96 { 97 int v = to[1][i]; 98 Ergo(v); 99 si[u] += si[v]; 100 } 101 } 102 int main() 103 { 104 n = Get(); 105 for(int i = 1; i <= n; ++i) 106 { 107 int x = Get(); 108 while(x) 109 { 110 ++in[x]; 111 Ins(i, x, 0); 112 x = Get(); 113 } 114 } 115 Topo(); 116 Build(); 117 Ergo(0); 118 for(int i = 1; i <= n; ++i) 119 printf("%d\n", si[i] - 1); 120 }

?

轉載于:https://www.cnblogs.com/lytccc/p/6253270.html

總結

以上是生活随笔為你收集整理的灾难 BZOJ 2815的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。