• [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
  • 相关阅读:
    linux设备驱动学习笔记(1)
    linux 设备驱动程序中的一些关联性思考
    linux——(2)文件权限与目录配置
    linux——(1)初识linux
    设计模式-状态模式(State Pattern)
    设计模式-组合模式(Composite Pattern)
    设计模式-迭代器模式(Iterator Pattern)
    设计模式-模板方法模式(the Template Method Pattern)
    设计模式-外观模式(Facade Pattern)
    设计模式-适配器模式(Adapter Pattern)
  • 原文地址:https://www.cnblogs.com/wang9897/p/10465615.html
Copyright © 2020-2023  润新知