CF1695D2. Tree Queries (Hard Version) (树形DP, 思维)
https://codeforces.com/contest/1695/problem/D2
题意:
思路:
- 找任意度数大于3的点u做根,u有c个子树,则c-1个子树都需要有询问点
- 这是因为,度数大于3的点至少有两棵子树有询问点,这样对于一个子树,子树外有询问点,只需关注子树内
树上问题以一个子树为一个阶段进行分析
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false) ,cin.tie(0), cout.tie(0);
//#pragma GCC optimize(3,"Ofast","inline")
#define ll long long
#define PII pair<int, char>
//#define int long long
const int N = 2e5 + 5;
const int M = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const ll LNF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double PI = acos(-1.0);
vector<int> E[N];
bool f[N];
int dfs ( int u, int fa ) {
f[u] = 0; int res = 0; int cnt = 0;
for ( auto v : E[u] ) {
if( v == fa ) continue;
res += dfs(v, u);
f[u] |= f[v];
cnt += !f[v];
}
if( cnt >= 2 ) res += cnt - 1, f[u] = 1;
return res;
}
void solve() {
int n; cin >> n;
if( n == 1 ) {
cout << 0 << '\n'; return;
}
for ( int i = 1; i <= n; ++ i ) E[i].clear();
for ( int i = 1; i <= n - 1; ++ i ) {
int u,v; cin >> u >> v;
E[u].push_back(v), E[v].push_back(u);
}
int root = 0;
for ( int i = 1; i <= n; ++ i ) {
if(E[i].size() >= 3) {
root = i; break;
}
}
if( !root ) {
cout << 1 << '\n'; return;
}
cout << dfs(root, -1) << '\n';
}
int main () {
IOS
int t = 1; cin >> t;
while ( t -- ) solve();
return 0;
}