Description
After Farmer John realized that Bessie had installed a “tree-shaped” network among his N (1 <= N <= 10,000) barns at an incredible cost, he sued Bessie to mitigate his losses.
Bessie, feeling vindictive, decided to sabotage Farmer John’s network by cutting power to one of the barns (thereby disrupting all the connections involving that barn). When Bessie does this, it breaks the network into smaller pieces, each of which retains full connectivity within itself. In order to be as disruptive as possible, Bessie wants to make sure that each of these pieces connects together no more than half the barns on FJ.
Please help Bessie determine all of the barns that would be suitable to disconnect.
Input
Line 1: A single integer, N. The barns are numbered 1..N.
Lines 2..N: Each line contains two integers X and Y and represents a connection between barns X and Y.
Output
10
1 2
2 3
3 4
4 5
6 7
7 8
8 9
9 10
3 8
Sample Output
3
8
Hint
INPUT DETAILS:
The set of connections in the input describes a “tree”: it connects all the barns together and contains no cycles.
OUTPUT DETAILS:
If barn 3 or barn 8 is removed, then the remaining network will have one piece consisting of 5 barns and two pieces containing 2 barns. If any other barn is removed then at least one of the remaining pieces has size at least 6 (which is more than half of the original number of barns, 5).
#include<cstdio>
#include<algorithm>
#define N 11000
using namespace std;
inline int read(){
int x=0;char ch=getchar();
while (ch<'0'||ch>'9') ch=getchar();
while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
return x;
}
struct node{
int y,next;
}data[N<<1];
bool mark[N];int size[N],dfn[N],low[N],ans,num,n,h[N],fa[N];
void tarjan(int x){
dfn[x]=low[x]=++num;size[x]++;int tmp=0,rest=n-1;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;
if (fa[x]==y) continue;
if (!dfn[y]){
tarjan(y);size[x]+=size[y];low[x]=min(low[x],low[y]);
if (low[y]>=dfn[x]){
tmp=max(tmp,size[y]);rest-=tmp;
}
}else low[x]=min(low[x],dfn[y]);
}
tmp=max(tmp,rest);
if (tmp<=(n>>1)) ans++,mark[x]=true;
}
int main(){
freopen("bzoj3391.in","r",stdin);
n=read();
for (int i=1;i<n;++i){
int x=read(),y=read();
data[++num].y=y;data[num].next=h[x];h[x]=num;
data[++num].y=x;data[num].next=h[y];h[y]=num;
}
for (int i=1;i<=n;++i) if (!dfn[i]) tarjan(i);
if (!ans) printf("NONE");else for (int i=1;i<=n;++i) if(mark[i]) printf("%d\n",i);
return 0;
}
因篇幅问题不能全部显示,请点此查看更多更全内容