• [Poi2014][BZOJ3522] Hotel


    3522: [Poi2014]Hotel

    Time Limit: 20 Sec  Memory Limit: 128 MB
    Submit: 278  Solved: 136
    [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

    HINT

    【样例解释】

    {1,3,5},{2,4,6},{2,4,7},{2,6,7},{4,6,7}




    【数据范围】

    n≤5000

    Source

    暴力枚举中点,乘法原理。t1,t2过渡一下。
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<vector>
    #include<queue>
    #define ll long long
    using namespace std;
    int next[10005],list[10005],head[5005],num[5005];
    ll sum,t1[5005],t2[5005];
    int tot,n,x,y,mx;
    void insert(int x,int y)
    {
        next[++tot]=head[x];
        head[x]=tot;
        list[tot]=y;
    }
    void dfs(int x,int fa,int deep)
    {
        mx=max(mx,deep);
        num[deep]++;
        for (int i=head[x];i;i=next[i])
            if (list[i]!=fa) dfs(list[i],x,deep+1);
    }
    
    int main()
    {
        scanf("%d",&n);
        for (int i=1;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            insert(x,y);
            insert(y,x);
        }
        for (int i=1;i<=n;i++)
        {
            memset(t1,0,sizeof(t1));
            memset(t2,0,sizeof(t2));
            for (int j=head[i];j;j=next[j])
            {
                memset(num,0,sizeof(num));
                dfs(list[j],i,1);
                for (int k=1;k<=mx;k++)
                {
                    sum+=t2[k]*num[k];
                    t2[k]+=num[k]*t1[k];
                    t1[k]+=num[k];
                }
            }
        }
        printf("%lld",sum);
        return 0;
    }
     
  • 相关阅读:
    Guns 01 项目基本运行
    个人 比较好用的软件
    个人 软件开发 综合技能提升
    开源框架 综合知识
    开源框架 工作流框架
    开源框架 Java 开发框架 1
    开源框架 Java 管理系统
    开源框架 C#
    scp(安全拷贝)和rsync(增量复制)
    完全分布式集群的准备工作
  • 原文地址:https://www.cnblogs.com/ws-fqk/p/4664845.html
Copyright © 2020-2023  润新知