[Poi2014]Hotel

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
n≤5000

这题\(O(n^2)\)即可,但是我TM开始想了个巨麻烦的做法。。。直接设\(f[i][j]\)表示深度为\(i\)可以凑出的\(j\)元组的个数,枚举根之后跑3次dfs即可(加强版需要用长链剖分+dp,我不会)

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
    static char buf[1000000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
    int x=0,f=1;char ch=gc();
    for (;ch<'0'||ch>'9';ch=gc())   if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<1)+(x<<3)+ch-'0';
    return x*f;
}
inline int read(){
    int x=0,f=1;char ch=getchar();
    for (;ch<'0'||ch>'9';ch=getchar())  if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=getchar())    x=(x<<1)+(x<<3)+ch-'0';
    return x*f;
}
inline void print(int x){
    if (x<0)    putchar('-'),x=-x;
    if (x>9)    print(x/10);
    putchar(x%10+'0');
}
const int N=5e3;
int pre[(N<<1)+10],now[N+10],child[(N<<1)+10];
ll f[N+10][4],Ans;
int n,tot;
void join(int x,int y){pre[++tot]=now[x],now[x]=tot,child[tot]=y;}
void insert(int x,int y){join(x,y),join(y,x);}
void dfs(int x,int fa,int deep,int T){
    f[deep][T]+=f[deep][T-1];
    for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
        if (son==fa)    continue;
        dfs(son,x,deep+1,T);
    }
}
void work(int x){
    memset(f,0,sizeof(f));
    for (int i=1;i<=n;i++)  f[i][0]=1;
    for (int p=now[x],son=child[p];p;p=pre[p],son=child[p])
        dfs(son,x,1,3),dfs(son,x,1,2),dfs(son,x,1,1);
    for (int i=1;i<=n;i++)  Ans+=f[i][3];
}
int main(){
    n=read();
    for (int i=1;i<n;i++){
        int x=read(),y=read();
        insert(x,y);
    }
    for (int i=1;i<=n;i++)  work(i);
    printf("%lld\n",Ans);
    return 0;
}

原文地址:https://www.cnblogs.com/Wolfycz/p/9949255.html

时间: 2024-08-29 22:12:10

[Poi2014]Hotel的相关文章

3522: [Poi2014]Hotel( 树形dp )

枚举中点x( 即选出的三个点 a , b , c 满足 dist( x , a ) = dist( x , b ) = dist( x , c ) ) , 然后以 x 为 root 做 dfs , 显然两个位于 x 的同一颗子树内的点是不可能被同时选到的 . 我们对 x 的每一颗子树进行 dfs , 记录下当前子树中的点到 x 距离为 d ( 1 <= d <= n ) 有多少个 , 记为 cnt[ 0 ][ i ] . 然后 cnt[ 1 ][ i ] 记录之前 dfs 过的子树的 cnt[

3522: [Poi2014]Hotel

3522: [Poi2014]Hotel Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 253  Solved: 117[Submit][Status][Discuss] Description 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房(间).三个妹子住的房间要互不相同(否则要打起来了),为了让吉丽满意,你需要让三个房间两两距离相同.有多少种方案能让吉丽满意?

bzoj 3522: [Poi2014]Hotel

呵呵,一开始天真的我以为求个 西格玛 C(??,3)就好了.. (题解:比枚举2个数的再多一个,,一样搞) 1 #include <bits/stdc++.h> 2 #define LL long long 3 #define lowbit(x) x&(-x) 4 #define inf 1e15 5 using namespace std; 6 inline int ra() 7 { 8 int x=0,f=1; char ch=getchar(); 9 while (ch<'

bzoj3522 [Poi2014]Hotel

题目链接 在黄学长博客上居然分类树形DP? 暴力,以每个点为根计算子树内答案,推推式子就维护两个S就可以啦 1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<string> 7 #include<cmath> 8 #include<ctime&

BZOJ3522——[Poi2014]Hotel

1.题意:http://www.lydsy.com/JudgeOnline/problem.php?id=3522 2.分析:这道题有两种情况,第一个是三个点在同一个链上,这显然不可能了,因为树上的路径是唯一的,第二个情况是三个点距离某个中心的距离相等 那么我们只需枚举中间点,然后在不同子树中dfs一下即可求出答案 #include <queue> #include <cstdio> #include <cstring> #include <cstdlib>

【bzoj3522】[Poi2014]Hotel 树形dp

题目描述 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房(间).三个妹子住的房间要互不相同(否则要打起来了),为了让吉丽满意,你需要让三个房间两两距离相同.有多少种方案能让吉丽满意? 输入 第一行一个数n.接下来n-1行,每行两个数x,y,表示x和y之间有一条边相连. 输出 让吉丽满意的方案数. 样例输入 7 1 2 5 7 2 5 2 3 5 6 4 5 样例输出 5 题解 树形dp 如果树上三个点之间两两距离相同

BZOJ 3522 [Poi2014] Hotel 题解

题意:求一棵边权全都是1的树上,集合大小为3,且集合内点两两距离相等的集合个数. NOIP2014 D1T2加强版.. 通过分析发现,满足这样的点对一定是在有根树中深度相同,且不再同一棵以根节点儿子为根的子树中. 于是我们枚举根.. 三个点的集合个数是由2个点的集合个数转移过来的..2个又是由一个转移过来的.用两个数组保存一下即可. 1 #include<cstdio> 2 #include<cstring> 3 #include<vector> 4 #include&

@bzoj - [email&#160;protected] [POI2014]Hotel加强版

目录 @[email protected] @[email protected] @part - [email protected] @part - [email protected] @accepted [email protected] @[email protected] @[email protected] 给定一棵树,求无序三元组 (a, b, c) 的个数,使得 dis(a, b) = dis(b, c) = dis(c, a),且 a ≠ b, b ≠ c, c ≠ a. inpu

[Poi2014][BZOJ3522] Hotel

3522: [Poi2014]Hotel Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 278  Solved: 136[Submit][Status][Discuss] Description 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房(间).三个妹子住的房间要互不相同(否则要打起来了),为了让吉丽满意,你需要让三个房间两两距离相同.有多少种方案能让吉丽满意?