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

相关文章:

  • 2021-08-12
  • 2021-08-14
  • 2021-12-18
  • 2021-08-21
  • 2022-12-23
  • 2022-12-23
  • 2021-11-07
  • 2021-07-21
猜你喜欢
  • 2021-09-04
  • 2022-12-23
  • 2021-07-22
  • 2021-12-29
  • 2022-12-23
  • 2021-11-19
  • 2021-12-10
相关资源
相似解决方案