Byteland is a beautiful land known because of its beautiful trees.

Misha has found a binary tree with nn vertices, numbered from 11 to nn. A binary tree is an acyclic connected bidirectional graph containing nn vertices and n−1n−1 edges. Each vertex has a degree at most 33, whereas the root is the vertex with the number 11 and it has a degree at most 22.

Unfortunately, the root got infected.

The following process happens nn times:

  • Misha either chooses a non-infected (and not deleted) vertex and deletes it with all edges which have an end in this vertex or just does nothing.
  • Then, the infection spreads to each vertex that is connected by an edge to an already infected vertex (all already infected vertices remain infected).

As Misha does not have much time to think, please tell him what is the maximum number of vertices he can save from the infection (note that deleted vertices are not counted as saved).

Input

There are several test cases in the input data. The first line contains a single integer tt (1≤t≤50001≤t≤5000) — the number of test cases. This is followed by the test cases description.

The first line of each test case contains one integer nn (2≤n≤3⋅1052≤n≤3⋅105) — the number of vertices of the tree.

The ii-th of the following n−1n−1 lines in the test case contains two positive integers uiui and vivi (1≤ui,vi≤n1≤ui,vi≤n), meaning that there exists an edge between them in the graph.

It is guaranteed that the graph is a binary tree rooted at 11. It is also guaranteed that the sum of nn over all test cases won't exceed 3⋅1053⋅105.

Output

For each test case, output the maximum number of vertices Misha can save.

Example

input

4
2
1 2
4
1 2
2 3
2 4
7
1 2
1 5
2 3
2 4
5 6
5 7
15
1 2
2 3
3 4
4 5
4 6
3 7
2 8
1 9
9 10
9 11
10 12
10 13
11 14
11 15

output

0
2
2
10

Note

In the first test case, the only possible action is to delete vertex 22, after which we save 00 vertices in total.

In the second test case, if we delete vertex 22, we can save vertices 33 and 44.

题意: 给出一棵含有n个结点的二叉树,初始根节点感染了病毒,接下来会执行若干次操作,每次操作删除一个未被感染的点及其连边,然后病毒向下扩散一层,当病毒无法蔓延时就停止操作,求最终未被感染且未被删除的最大结点个数。

分析: 先说一下一开始想到的错误思路,先求出每个结点的子结点数量,对于每个结点当要删除时优先删子结点个数多的那个结点,这样能保证本次操作是最优的,但是局部最优并不能推出全局最优,并且当两结点的子结点个数相同时该删哪个也是个问题,实际上病毒蔓延的路径就是一条dfs的路径,通过删除结点我们是可以控制蔓延路径的,显然我们希望这条路径越短越好,所以应该找到距离根最近的叶子结点,因为病毒蔓延终止于叶子,但是还需要注意的是病毒蔓延还会终止于单分支结点,当走到一个单分支结点时可以通过删除其子结点来终止蔓延,所以还要找到距离根最近的单分支结点,那么具体要引到单分支结点还是叶子结点呢?对于两种情况都求一下,然后取一个最大值就好了。设最近的叶子结点距离根为d1,那么过程中会有2*d1-1个结点被感染或删除,若换成单分支结点,那就会有2*d2个结点被感染或删除,这点画图就能看出来,所以最终答案就是max(n-(2*d1-1), n-2*d2)。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#define inf 0x3f3f3f3f
using namespace std;vector<int> g[300005];
int d0, d1, ans; void dfs1(int now, int fa, int step){if(g[now].size() == 2 && fa != 0)d1 = min(d1, step);else if(g[now].size() == 1 && fa == 0)d1 = min(d1, step);if(g[now].size() == 1 && fa != 0)d0 = min(d0, step);else if(g[now].size() == 0 && fa == 0)d0 = min(d0, step);for(int i = 0; i < g[now].size(); i++){int to = g[now][i];if(to == fa) continue;dfs1(to, now, step+1);}
}signed main()
{int T;cin >> T;while(T--){int n;scanf("%d", &n);for(int i = 1; i <= n; i++)g[i].clear();for(int i = 1; i < n; i++){int u, v;scanf("%d%d", &u, &v);g[u].push_back(v);g[v].push_back(u);}d0 = d1 = inf;dfs1(1, 0, 1);int ans = max(n-(2*d0-1), n-2*d1);    printf("%d\n", ans);}return 0;
}

[dfs][思维]Infected Tree CF1689C相关推荐

  1. Infected Tree(递归/树形)

    Infected Tree 传送门 Problem - C - Codeforces 思路 一开始第一眼以为是树形dp,就先把每个节点下面的节点个数(不包括自己)全求出来了,然后后面发现没用- 然后又 ...

  2. Codeforces Round #798 C. Infected Tree

    Byteland is a beautiful land known because of its beautiful trees. Misha has found a binary tree wit ...

  3. C. Infected Tree(思维/dfs/树形dp)

    题目 题意: 给定一颗树 ,根节点为2,每个结点的子节点数不超过2(也就是说这是一个二叉树).现在根节点被感染了,我们需要拯救这棵树. 有n次过程,每次过程如下: 我们可以选择砍掉一个非感染点,使得该 ...

  4. (Codeforces798Div2)C. Infected Tree(思维)

    题目链接:Problem - C - Codeforces 样例输入: 4 2 1 2 4 1 2 2 3 2 4 7 1 2 1 5 2 3 2 4 5 6 5 7 15 1 2 2 3 3 4 4 ...

  5. CF982 C Cut 'em all!【树/DFS/思维】

    [链接]:CF982C [题意]:有一颗树,你需要切掉一些边,使这颗树分拆成若干个节点为偶数的联通分量,最多能切掉几条边.若不能切,输出-1. [分析]: 1.若点数n为奇数,因为奇数不可能分为偶数, ...

  6. Monopole Magnets CodeForces - 1345D(dfs+思维)

    A monopole magnet is a magnet that only has one pole, either north or south. They don't actually exi ...

  7. [蓝桥杯2017初赛]方格分割-dfs+思维

    解题思路: 这是大佬的思路: 这道题可能上来会想到搜格子,但搜格子意味着更高的复杂度以及判连通的需要,本题似乎搜索要切开的边更优.由题意,这一条切割线必定经过图的中心点,那么我们一旦确定了半条到达边界 ...

  8. Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4) dfs + 思维

    传送门 文章目录 题意: 思路: 题意: 给一张图,求必须经过aaa点和bbb点的路径条数. 思路: 通过观察我们发现,这个路径无非就是x−>a−>b−>yx->a->b ...

  9. AtCoder2362 - Splatter Painting - DFS+思维

    1.题目描述: B - Splatter Painting Time limit : 2sec / Memory limit : 256MB Score : 700 points Problem St ...

最新文章

  1. 干货!用大白话告诉你什么是Mock测试
  2. 同步和异步的区别和联系以及一般在什么情况下使用它们
  3. 进程内COM与进程外COM
  4. LVS负载均衡:三种工作模式、十种调度算法
  5. 20145326蔡馨熠《信息安全系统设计》第2周学习总结
  6. iphone7测试软件,iPhone7如何测试网速 ping命令测试网速方法介绍
  7. ubuntu 12.04 server + OPENACS(TR069)安装配置日记
  8. 博文搬家到公众号了~~~
  9. 经典图像分割方法总结
  10. UE4像素流送PixelStreaming
  11. centerOS 安装redis
  12. 快速解决cmd命令行乱码问题
  13. 阿里P9技术:我来聊聊百万年薪
  14. linux 搭建离线下载文件,不经意间用wget给自己搭建了一个离线下载服务器
  15. JAVA毕业设计冠军体育用品购物网站计算机源码+lw文档+系统+调试部署+数据库
  16. 51单片机的定时器/计数器及编程
  17. 小程序逆向——某书小程序反编译(一)
  18. Python: Basemap进行数据可视化分析------专题图绘制
  19. python win32com、docx 操控word
  20. java贪吃蛇(障碍物*咬尾巴)

热门文章

  1. 在Ubuntu系统使用Nginx搭建RTMP服务器
  2. python期末复习指南
  3. 服务器的规格标准1U~7U
  4. 这这这。。。有神么问题
  5. 软考高级-信息系统管理师之风险管理(最新版)
  6. C语言 用malloc开辟二维数组
  7. 记录一下MateBook打开华为分享找不到设备
  8. 什么是全文检索?及全文检索的实现思路
  9. 1013 数素数(C语言)
  10. 基于微信小程序云开发的医院体检预约小程序源码,医院体检预约小程序源码,实现体检预约管理、体检预约凭证、预约数据查看导出 版权申诉