• [BZOJ]3522: [Poi2014]Hotel


      题解:  $ n<=5000 $  那我们考虑暴力枚举根 

      $ dp[x] $表示任意两个属于不同根儿子子树且深度为x个数的乘积和

      $ sum[x] $表示不同根儿子子树 深度为x的个数和

      然后统计方案就行了

    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <cmath>
    #include <set>
    #include <map>
    #define mp make_pair
    #define pb push_back
    #define pii pair<int,int>
    #define link(x) for(edge *j=h[x];j;j=j->next)
    #define inc(i,l,r) for(int i=l;i<=r;i++)
    #define dec(i,r,l) for(int i=r;i>=l;i--)
    const int MAXN=5e3+10;
    const double eps=1e-8;
    #define ll long long
    using namespace std;
    struct edge{int t;edge*next;}e[MAXN<<1],*h[MAXN],*o=e;
    void add(int x,int y){o->t=y;o->next=h[x];h[x]=o++;}
    ll read(){
        ll x=0,f=1;char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
        while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
        return x*f;
    }
    
    
    int n;
    ll dp[MAXN],sum[MAXN],num[MAXN],ans;
    int maxx;
    bool flag;
    
    void dfs(int x,int pre,int deep){
        maxx=max(maxx,deep+1);
        num[deep+1]++;
        if(flag)ans+=dp[deep+1];
        link(x){
    	if(j->t==pre)continue;
    	dfs(j->t,x,deep+1);
        }
    }
    
    int main(){
        n=read();
        int x,y;
        inc(i,2,n)x=read(),y=read(),add(x,y),add(y,x);
        inc(i,1,n){
    	int cnt=0;
    	link(i){
    	    cnt++;
    	    if(cnt>=3)flag=1;
    	    maxx=0;
    	    dfs(j->t,i,1);
    	    inc(j,1,maxx)dp[j]+=sum[j]*num[j],sum[j]+=num[j],num[j]=0;
    	}
    	inc(j,1,n)dp[j]=sum[j]=num[j]=0;
        }
        printf("%lld
    ",ans);
    }
    

      

    3522: [Poi2014]Hotel

    Time Limit: 20 Sec  Memory Limit: 128 MB
    Submit: 1250  Solved: 690
    [Submit][Status][Discuss]

    Description

    有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达。吉丽要给他的三个妹子各开(一个)房(间)。三个妹子住的房间要互不相同(否则要打起来了),为了让吉丽满意,你需要让三个房间两两距离相同。
    有多少种方案能让吉丽满意?

    Input

    第一行一个数n。
    接下来n-1行,每行两个数x,y,表示x和y之间有一条边相连。

    Output

    让吉丽满意的方案数。

    Sample Input

    7
    1 2
    5 7
    2 5
    2 3
    5 6
    4 5

    Sample Output

    5
  • 相关阅读:
    KindEditor简单的Demo使用
    动态从数据库读取菜单(ASP.NET版)
    完全卸载sql2005
    关于在xp(sp3 专业版)下安装sql2005开发版图解
    新手上路Tomcat 7.x和JDK的配置
    关于IE6幽灵字体
    【译】写个好的 CLI 程序
    【译】通过 Rust 学习解析器组合器 — Part 1
    【译】PHP 内核 — zval 基础结构
    【译】PHP 内核 — 字符串管理
  • 原文地址:https://www.cnblogs.com/wang9897/p/10465615.html
Copyright © 2020-2023  润新知