• 「CF1042F」Leaf Sets


    传送门
    Luogu

    解题思路

    比较显然的一种做法:
    我们把一个点的子树高度抠出来并排序记为 (L_i),找到最大的 (i) 使得 (L_{i-1}+L_ile K)
    于是我们把前 (i) 个对应的子树中的叶子合并为一个集合,之后的单独为一个集合,从非叶子跑一遍 ( ext{DFS}) 就可以了,最后输出答案即可。

    细节注意事项

    • 咕咕咕

    参考代码

    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <cctype>
    #include <cmath>
    #include <ctime>
    #include <vector>
    #define rg register
    using namespace std;
    template < typename T > inline void read(T& s) {
     	s = 0; int f = 0; char c = getchar();
     	while (!isdigit(c)) f |= (c == '-'), c = getchar();
     	while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
     	s = f ? -s : s;
    }
    
    const int _ = 1000010;
    const int __ = 2000010;
    
    int tot, head[_], nxt[__], ver[__];
    inline void Add_edge(int u, int v)
    { nxt[++tot] = head[u], head[u] = tot, ver[tot] = v; }
    
    int n, k, dgr[_], res;
    
    inline int dfs(int u, int f) {
    	if (dgr[u] == 1) return 0;
    	vector < int > t;
    	for (rg int i = head[u]; i; i = nxt[i])
    		if (ver[i] != f) t.push_back(dfs(ver[i], u) + 1);
    	sort(t.begin(), t.end());
    	int len = t.size() - 1;
    	for (; len > 0; --len)
    		if (t[len] + t[len - 1] <= k) break; else ++res;
    	return t[len];
    }
    
    int main() {
    #ifndef ONLINE_JUDGE
    	freopen("in.in", "r", stdin);
    #endif
    	read(n), read(k);
    	for (rg int u, v, i = 1; i < n; ++i)
    		read(u), read(v), Add_edge(u, v), Add_edge(v, u), ++dgr[u], ++dgr[v];
    	int rt = 1;
    	for (rg int i = 1; i <= n; ++i)
    		if (dgr[i] > 1) { rt = i; break; }
    	dfs(rt, 0);
    	printf("%d
    ", res + 1);
    	return 0;
    }
    

    完结撒花 (qwq)

  • 相关阅读:
    ORM&MySQL
    Python的内存管理机制
    Docker
    MySQL数据库优化
    Django&Flask区别
    Nginx
    JWT
    云接入特别说明
    gogs私有代码库上传项目
    Authentication failed (rejected by the remote node), please check the Erlang cookie
  • 原文地址:https://www.cnblogs.com/zsbzsb/p/11745884.html
Copyright © 2020-2023  润新知