。。。 一开始吓着了 结果不就是找度为1的节点么。。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<string> 6 #include<queue> 7 #include<algorithm> 8 #include<map> 9 #include<iomanip> 10 #include<climits> 11 #include<string.h> 12 #include<cmath> 13 #include<stdlib.h> 14 #include<vector> 15 #include<set> 16 #define INF 1e7 17 #define MAXN 100010 18 #define maxn 1000010 19 #define Mod 1000007 20 #define N 1010 21 using namespace std; 22 typedef long long LL; 23 24 int deg[10010]; 25 int x, y; 26 int n; 27 28 void run() 29 { 30 for (int i = 0; i <= n; ++i) { 31 deg[i] = 0; 32 } 33 for (int i = 1; i < n; ++i) { 34 scanf("%d%d",&x,&y); 35 deg[x]++, deg[y]++; 36 } 37 int num = 0; 38 for (int i = 1; i <= n; ++i) 39 if (deg[i] == 1) 40 num++; 41 42 printf("%d ",(num+1)/2); 43 } 44 45 int main() 46 { 47 while (cin >> n) 48 run(); 49 //system("pause"); 50 return 0; 51 }