题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=170
思路:统计每个节点的度,将度为1的节点消去所需要的最少的边即为答案。
代码:
#include "stdio.h" //nyoj 170 网络的可靠性 #include "string.h" #define N 10005 int du[N]; int main() { int n; int i; int x,y; while(scanf("%d",&n)!=EOF) { if(n==1 || n==2) {printf("0 "); continue; } memset(du,0,sizeof(du)); for(i=1; i<n; ++i) { scanf("%d %d",&x,&y); du[x]++; du[y]++; } int num=0; for(i=1; i<=n; ++i) { if(du[i]==1) num++; } if(num%2==0) num = num/2; else num = num/2+1; printf("%d ",num); } return 0; }