旅游
1000ms
65536KB
64-bit integer IO format:
%lld Java class name:
Main
Font Size:
某L是一个旅游爱好者,并且伴有轻微强迫症。不管去什么地方旅游,他总是要走遍每一个景点,以求不留遗憾。
前段时间,他又去了一个名叫YNBHX的景区,这个景区非常特别,它一共有N个景点和N-1条小道,每条小道连接这N个景点中的某两个,并保证从景区内每一个景点都能通过若干条小道到达另一个景点。
由于景区人数微多,景区规定,每人每天可以选择任意一个景点出发到达另一个景点,但途中不能走回头路(即每一条小道在同一天之内只能经过一次),现在某L想知道,对于这个神奇的景区,他要想游览完所有的景区至少需要几天?=,=
Input
第一行一个整数N,表示共有N个景点。(N<=1000)
接下来N-1行,每行两个整数A、B,表示景点A和景点B之间有一条小道。
Output
一个整数K,表示某L要想游览完所有景点至少需要K天。
Sample Input
Sample Input1 3 1 2 2 3 Sample Input2 4 1 2 1 3 1 4
Sample Output
Sample Output1 1 Sample Output2 2水题:只用判断无向图的度,度大于2的把大于2的部分加起来,最后ans=(sum+1)/2+1;<pre class="cpp" name="code">#include <stdio.h> #include <string.h> int in[2000]={0}; int main() { int n; while(~scanf("%d",&n)) { int x,y; for(int i=1;i<n;i++) { scanf("%d%d",&x,&y); in[x]++;in[y]++; } int ans=0; for(int i=0;i<1001;i++) { if(in[i]>2) { ans+=(in[i]-2); } } printf("%d\n",(ans+1)/2+1); } return 0; }