• Leaf Sets CodeForces


    大意: 给定树, 求叶子的最小划分, 使得每个划分内任意两个叶子距离不超过k.

    任选一个非叶结点, 贪心合并.

    #include <iostream>
    #include <sstream>
    #include <algorithm>
    #include <cstdio>
    #include <math.h>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    #include <string.h>
    #include <bitset>
    #define REP(i,a,n) for(int i=a;i<=n;++i)
    #define PER(i,a,n) for(int i=n;i>=a;--i)
    #define hr putchar(10)
    #define pb push_back
    #define lc (o<<1)
    #define rc (lc|1)
    #define mid ((l+r)>>1)
    #define ls lc,l,mid
    #define rs rc,mid+1,r
    #define x first
    #define y second
    #define io std::ios::sync_with_stdio(false)
    #define endl '
    '
    #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> pii;
    const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
    ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
    ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
    ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
    inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
    //head
    
    
    
    #ifdef ONLINE_JUDGE
    const int N = 1e6+10;
    #else
    const int N = 111;
    #endif
    
    int n, k, ans;
    vector<int> g[N];
    
    int dfs(int x, int f) {
    	if (g[x].size()==1) return 0;
    	vector<int> v;
    	for (int y:g[x]) if (y!=f) v.pb(dfs(y,x)+1);
    	sort(v.begin(),v.end());
    	int sz = v.size()-1;
    	for (; sz>=1; --sz) {
    		if (v[sz]+v[sz-1]<=k) break;
    		++ans;
    	}
    	return v[sz];
    }
    
    int main() {
    	scanf("%d%d", &n, &k);
    	REP(i,2,n) {
    		int u, v;
    		scanf("%d%d", &u, &v);
    		g[u].pb(v),g[v].pb(u);
    	}
    	int rt = 1;
    	PER(i,1,n) if (g[i].size()>1) rt=i;
    	dfs(rt,0);
    	printf("%d
    ",ans+1);
    }
    
  • 相关阅读:
    Using Redis as Django's session store and cache backend
    Celery 和 Redis 入门
    centos 安装 rabbitmq
    CentOS 6 安装 Python3.5以及配置Django
    python metaclass 入门简介
    uWSGI其三:uWSGI搭配Nginx使用
    CentOS 6.5 安装 Nginx 1.7.8 教程
    基于nginx和uWSGI在Ubuntu上部署Djan
    CentOS 6.5 下安装 Redis 2.8.7
    查看Selinux和关闭Selinux
  • 原文地址:https://www.cnblogs.com/uid001/p/10913197.html
Copyright © 2020-2023  润新知