forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpt07x.cpp
58 lines (58 loc) · 817 Bytes
/
pt07x.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 2009-04-05
#include <iostream>
using namespace std;
static int neighbours[222222]={0};
static int Link[222222]={0};
static int first[222222]={0};
static int last[222222]={0};
int top=1;
void f(int parent,int node,int& i,int& n)
{
i=1;
n=0;
for (int I=first[node]; I; I=Link[I])
{
int x,y;
if (neighbours[I]!=parent)
{
f(node,neighbours[I],x,y);
i+=min(x,y);
n+=x;
}
}
}
int in()
{
char c;
int x=0;
for(;;)
{
c=getchar_unlocked();
if (c<=32) return x;
x=(x<<1)+(x<<3)+c-'0';
}
}
void add(int s,int t)
{
if (!first[s])
first[s]=last[s]=top;
else
last[s]=Link[last[s]]=top;
neighbours[top++]=t;
}
int main()
{
int N,i,u,v;
N=in();
for (i=0; i<N-1; i++)
{
u=in()-1; v=in()-1;
add(u,v);
add(v,u);
}
int x;
int y;
f(-1,0,x,y);
printf("%d\n",min(x,y));
return 0;
}