3391: [usaco 128 DEC] tree cutting network failure time limit: 1 sec memory limit: MB
Submit: 47 solved: 37
[Submit] [Status] Description John was aware that Bessie had spent a huge amount of money on building the network and dismissed her. bessie was angry and plans to retaliate. she plans to destroy John's network. john's network is tree-like and connects N (1 ≤ n ≤ 10000) cowshed. she planned to cut off the power of a cowshed and cut off all cables connected to the cowshed. then there will be several sub-networks. to ensure that the damage is large enough, the number of Cowshed in each subnet cannot exceed half of the total number of cowshed, which cowshed is worth the damage? Row 1st of input: an integer n. rows 2nd to n + 1: Enter two integers in each line to indicate the two endpoints of a cable. output outputs all the burstable worthy of destruction in ascending order. if there is no damage, "NONE" is output ". sample input10
1 2
2 3
3 4
4 5
6 7
7 8
8 9
9 10
3 8 sample output3
8
If shed 3 or shed 8 is damaged, the number of remaining three subnet nodes will be 5, 2, 2, not more than 5.
Source Information
Hint Source
Silver
Question:
Similar to finding the center of gravity, you just need to go through DFS again.
Code:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #include<string>12 #define inf 100000000013 #define maxn 10000+1014 #define maxm 500+10015 #define eps 1e-1016 #define ll long long17 #define pa pair<int,int>18 #define for0(i,n) for(int i=0;i<=(n);i++)19 #define for1(i,n) for(int i=1;i<=(n);i++)20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)22 #define mod 100000000723 using namespace std;24 inline int read()25 {26 int x=0,f=1;char ch=getchar();27 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}28 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}29 return x*f;30 }31 int n,tot,head[maxn],a[maxn],f[maxn],s[maxn];32 bool v[maxn];33 struct edge{int go,next;}e[2*maxn];34 inline void insert(int x,int y)35 {36 e[++tot].go=y;e[tot].next=head[x];head[x]=tot;37 e[++tot].go=x;e[tot].next=head[y];head[y]=tot;38 }39 void dfs(int x)40 {41 v[x]=1;f[x]=0;s[x]=1;42 for(int i=head[x],y;i;i=e[i].next)43 if(!v[y=e[i].go])44 {45 dfs(y);46 s[x]+=s[y];47 f[x]=max(f[x],s[y]);48 }49 f[x]=max(f[x],n-s[x]);50 if(f[x]<=n/2)a[++a[0]]=x;51 }52 int main()53 {54 freopen("input.txt","r",stdin);55 freopen("output.txt","w",stdout);56 n=read();57 for1(i,n-1)insert(read(),read());58 dfs(1);59 if(!a[0]){printf("NONE");return 0;}60 sort(a+1,a+a[0]+1);61 for1(i,a[0])printf("%d\n",a[i]);62 return 0; 63 }View code
Bzoj3391: [usaco DEC] tree cutting network failure