BZOJ2815:[ZJOI2012]灾难
Description
阿米巴是小强的好朋友。
阿米巴和小强在草原上捉蚂蚱。小强突然想,如果蚂蚱被他们捉灭绝了,那么吃蚂蚱的小鸟就会饿死,而捕食小鸟的猛禽也会跟着灭绝,从而引发一系列生态灾难。
学过生物的阿米巴告诉小强,草原是一个极其稳定的生态系统。如果蚂蚱灭绝了,小鸟照样可以吃别的虫子,所以一个物种的灭绝并不一定会引发重大的灾难。
我们现在从专业一点的角度来看这个问题。我们用一种叫做食物网的有向图来描述生物之间的关系:
一个食物网有 N个点,代表 N 种生物,如果生物 x 可以吃生物 y,那么从 y向 x 连一个有向边。这个图没有环。
图中有一些点没有连出边,这些点代表的生物都是生产者,可以通过光合作用来生存;而有连出边的点代表的都是消费者,它们必须通过吃其他生物来生存。
如果某个消费者的所有食物都灭绝了,它会跟着灭绝。
我们定义一个生物在食物网中的“灾难值”为,如果它突然灭绝,那么会跟着一起灭绝的生物的种数。
举个例子:在一个草场上,生物之间的关系是:
如果小强和阿米巴把草原上所有的羊都给吓死了,那么狼会因为没有食物而灭绝,而小强和阿米巴可以通过吃牛、牛可以通过吃草来生存下去。所以,羊的灾难值是 1。
但是,如果草突然灭绝,那么整个草原上的 5 种生物都无法幸免,所以,草的灾难值是 4。
给定一个食物网,你要求出每个生物的灾难值。
Input
输入文件 catas.in 的第一行是一个正整数 N,表示生物的种数。生物从 1 标号到 N。
接下来 N 行,每行描述了一个生物可以吃的其他生物的列表,格式为用空格隔开的若干个数字,每个数字表示一种生物的标号,最后一个数字是 0 表示列表的结束。
Output
Sample Input
0
1 0
1 0
2 3 0
2 0
Sample Output
1
0
0
0
HINT
对 50%的数据,N ≤ 10000。
对 100%的数据,1 ≤ N ≤ 65534。
输入文件的大小不超过 1M。保证输入的食物网没有环。
题解:
网上的做法大多是灭绝树。
我的做法是按照这样一个规律求拓扑序:
一种生物的特征值tz[i]指的是其食物中最小的拓扑序,当有多个入度为0的候选点时,优先让特征值大的点先排入拓扑序。
按照这种规律,受生物i影响的生物的拓扑序必定是这种生物i的拓扑序后连续的一段,以最后一个特征值大于等于生物i拓扑序的生物结束,这部分可以用队列或倍增(RMQ)实现。
事实上,这个拓扑序就是所谓灭绝树的dfs序。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 //#define begin { 4 //#define end } 5 #define while while( 6 #define if if( 7 #define do ) 8 #define then ) 9 #define for for( 10 #define fillchar(a,b,c) memset(a,c,b) 11 #define writeln printf("\n") 12 #define write printf 13 #define readln readl() 14 #define inc(a) a++ 15 #define dec(a) a-- 16 #define exit(a) return a 17 #define mod % 18 #define div / 19 #define shl << 20 #define shr >> 21 #define ext}ed long double 22 #define longint int 23 #define integer short 24 #define int64 long long 25 template<typename T> inline void read(T& a) 26 { 27 T x=0,f=1; char ch=getchar(); 28 while(ch<'0')or(ch>'9')do 29 { 30 if ch=='-' then f=-1; ch=getchar(); 31 } 32 while(ch>='0')and(ch<='9')do 33 { 34 x=x*10+ch-'0'; ch=getchar(); 35 } 36 a=x*f; 37 } 38 inline void readl() 39 { 40 char ch; ch=getchar(); 41 while ch!='\n' do ch=getchar(); 42 } 43 struct yuansu 44 { 45 longint a,b; 46 bool operator<(const yuansu &thr)const{return b>thr.b;} 47 }; 48 multiset<yuansu> dui; 49 longint i,j,k,l,n,m,tot,ans,tp[100001],bz[100001][21],b[2000001][2],c[100001],rd[100001],tz[100001],lb[100001]; 50 int main() 51 { 52 read(n); tz[0]=-1; 53 for i=1;i<=n;i++ do 54 { 55 int a; read(a); 56 while a>0 do { inc(rd[i]); inc(m); b[m][1]=i; b[m][0]=c[a]; c[a]=m; read(a); } 57 if rd[i]==0 then { inc(rd[i]); inc(m); b[m][1]=i; b[m][0]=c[0]; c[0]=m; } 58 tz[i]=-1; 59 } 60 dui.insert((yuansu){0,-1}); tot=-1; 61 while !dui.empty() do 62 { 63 yuansu e=(*dui.begin()); dui.erase(dui.begin()); 64 inc(tot); lb[tot]=e.a; tp[e.a]=tot; 65 for int i=c[e.a];i;i=b[i][0] do 66 { 67 dec(rd[b[i][1]]); if tz[b[i][1]]==-1 then tz[b[i][1]]=tot; 68 if rd[b[i][1]]==0 then dui.insert((yuansu){b[i][1],tz[b[i][1]]}); 69 } 70 } 71 for i=0;i<=n;i++ do bz[i][0]=tz[lb[i]]; 72 for j=1;j<=20;j++ do 73 for i=0;i<=n;i++ do 74 { 75 bz[i][j]=bz[i][j-1]; 76 if i+(1 shl(j-1))<=n then 77 bz[i][j]=min(bz[i][j],bz[i+(1 shl(j-1))][j-1]); 78 } 79 for i=1;i<=n;i++ do 80 { 81 int ii=tp[i]+1; ans=0; 82 for j=20;j>=0;j-- do 83 { 84 if ii+(1 shl j)>n+1 then continue; 85 if bz[ii][j]>=tp[i] then { ans=ans+(1 shl j); ii=ii+(1 shl j); } 86 } 87 write("%d",ans); writeln; 88 } 89 }