Description
Our country is tree-like structure, that is to say that N cities is connected by exactly N - 1 roads.
The old king has two little sons. To make everything fairly, he dicided to divide the country into two parts and each son get one part. Two sons can choose one city as their capitals. For each city, who will be their king is all depend on whose capital is more close to them. If two capitals have the same distance to them, they will choose the elder son as their king.
(The distance is the number of roads between two city)
The old king like his elder son more, so the elder son could choose his capital firstly. Everybody is selfish, the elder son want to own more cities after the little son choose capital while the little son also want to own the cities as much as he can.
If two sons both use optimal strategy, we wonder how many cities will choose elder son as their king.
The old king has two little sons. To make everything fairly, he dicided to divide the country into two parts and each son get one part. Two sons can choose one city as their capitals. For each city, who will be their king is all depend on whose capital is more close to them. If two capitals have the same distance to them, they will choose the elder son as their king.
(The distance is the number of roads between two city)
The old king like his elder son more, so the elder son could choose his capital firstly. Everybody is selfish, the elder son want to own more cities after the little son choose capital while the little son also want to own the cities as much as he can.
If two sons both use optimal strategy, we wonder how many cities will choose elder son as their king.
Input
There are multiple test cases.
The first line contains an integer N (1 ≤ N ≤ 50000).
The next N - 1 lines each line contains two integers a and b indicating there is a road between city a and city b. (1 ≤ a, b ≤ N)
The first line contains an integer N (1 ≤ N ≤ 50000).
The next N - 1 lines each line contains two integers a and b indicating there is a road between city a and city b. (1 ≤ a, b ≤ N)
Output
For each test case, output an integer indicating the number of cities who will choose elder son as their king.
Sample Input
4 1 2 2 3 3 4 4 1 2 1 3 1 4
Sample Output
23
根据题意可以推出,大哥肯定会选择树的重心,所以dfs一遍找出重心即可。
#include<set> #include<map> #include<ctime> #include<cmath> #include<stack> #include<queue> #include<bitset> #include<cstdio> #include<string> #include<cstring> #include<iostream> #include<algorithm> #include<functional> #define rep(i,j,k) for (int i = j; i <= k; i++) #define per(i,j,k) for (int i = j; i >= k; i--) #define loop(i,j,k) for (int i = j;i != -1; i = k[i]) #define lson x << 1, l, mid #define rson x << 1 | 1, mid + 1, r #define fi first #define se second #define mp(i,j) make_pair(i,j) #define pii pair<int,int> using namespace std; typedef long long LL; const int low(int x) { return x&-x; } const double eps = 1e-8; const int INF = 0x7FFFFFFF; const int mod = 1e9 + 7; const int N = 1e5 + 10; const int read() { char ch = getchar(); while (ch<'0' || ch>'9') ch = getchar(); int x = ch - '0'; while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0'; return x; } int T, n, x, y; int ft[N], nt[N], u[N], ct[N], mx[N], sz; void clear(int n) { mx[sz = 0] = INF; rep(i, 1, n) ft[i] = -1; } void AddEdge(int x, int y) { u[sz] = y; nt[sz] = ft[x]; ft[x] = sz++; u[sz] = x; nt[sz] = ft[y]; ft[y] = sz++; } int dfs(int x, int fa, int sum) { int y = mx[x] = (ct[x] = 1) ^ 1; loop(i, ft[x], nt) { if (u[i] == fa) continue; int z = dfs(u[i], x, sum); ct[x] += ct[u[i]]; mx[x] = max(mx[x], ct[u[i]]); y = mx[y] < mx[z] ? y : z; } mx[x] = max(mx[x], sum - ct[x]); return mx[x] < mx[y] ? x : y; } int main() { while (scanf("%d",&n)!=EOF) { clear(n); rep(i, 1, n - 1) scanf("%d%d", &x, &y), AddEdge(x, y); x = dfs(1, 0, n); printf("%d\n", n - mx[x]); } return 0; }