Fork me on GitHub

NYOJ题目170网络的可靠性

-------------------------------

 

无论哪一个坏掉了都能连通意味着不能存在只有一根线(度为1)的基站,所以统计一下度为1的点,然后为了节省将它们两两相连,如果是奇数的话剩下的那个没配对的就随便连连喽~

 

AC代码:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         
 7         Scanner sc=new Scanner(System.in);
 8         while(sc.hasNextInt()){
 9             int n=sc.nextInt();
10             int x[]=new int[n+1];
11             
12             for(int i=0;i<n-1;i++){
13                 int u=sc.nextInt();
14                 int v=sc.nextInt();
15                 x[u]++;
16                 x[v]++;
17             }
18             
19             int ans=0;
20             for(int i=1;i<x.length;i++){
21                 if(x[i]==1) ans++;
22             }
23             
24             
25             System.out.println((ans+1)/2);
26         }
27     }
28     
29 }

 

题目来源: http://acm.nyist.net/JudgeOnline/problem.php?pid=170

posted @ 2016-09-22 00:52  CC11001100  阅读(281)  评论(0编辑  收藏  举报