HDU 4095 Y (树上计数问题)

题目链接:传送门

题意:

对于给定的一颗树,求树上有多少个三元组(A,B,C)使得包括A,B,C的路径不是一条简单路径。

简单路径:在一条路径中,若没有重复相同的顶点,该路径称为简单路径。

分析:从反面考虑这个问题,等于所有的三元组-所有由简单路径组成的链的数目。

代码如下:

#pragma comment(linker, "/STACK:16777216")
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 1e5+10;

typedef long long LL;

LL dp[maxn];

int head[maxn],ip;

struct node {
    int to,next;
} edge[maxn*2];

void add(int u,int v) {
    edge[ip].to=v;
    edge[ip].next=head[u];
    head[u]=ip++;
}

void init() {
    memset(head,-1,sizeof(head));
    ip=0;
}

LL ans,n;

void dfs(int u,int pre) {
    LL sum=0;
    dp[u]=1;
    for(int i=head[u]; i!=-1; i=edge[i].next) {
        int v = edge[i].to;
        if(v!=pre) {
            dfs(v,u);
            dp[u]+=dp[v];
            ans-=sum*dp[v];
            sum+=dp[v];
        }
    }
    ans-=sum*(n-dp[u]);
}

int main() {
    while(~scanf("%d",&n)) {
        init();
        for(int i=0; i<n-1; i++) {
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
        }
        ans = (n*(n-1)*(n-2)/6);
        dfs(1,0);
        printf("%I64d\n",ans);
    }
    return 0;
}
/****
4
1 2
1 3
1 4
***/

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-05 23:18:42

HDU 4095 Y (树上计数问题)的相关文章

hdu 5297 Y sequence(容斥)

题目链接:hdu 5297 Y sequence 考虑62以内的指数,x为奇数个质数因子,就减掉,偶数个加上.计算x为指数的不满足数直接pow(n,1/x)即可. #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <algorithm> using namespace std; type

hdu 2841 Visible Trees(计数问题)

题目链接:hdu 2841 Visible Trees 题目大意:一个n?m的矩阵,每个整数点上有树,人站在(0,0)点,问可以看见多少棵树. 解题思路:和uva1393是一道相同类型的题目,只不过这道题目的n比较大,不能预处理.必须用另外一种方法. 将矩阵按照(0,0)和(n,m)两天连成的直线分成两部分,分别计算,但是(n,m)这条线被计算了两次,于是减掉1. dp[i]表示这一列上有多少个点是可以被看见的,因为矩阵被对半分了,所以点的个数为m?in,同时还要计算每次看见的点会把后面的给挡住

HDU 2196 求树上所有点能到达的最远距离

其实我不是想做这道题的...只是今天考试考了一道类似的题...然后我挂了... 但是乱搞一下还是有80分....可惜没想到正解啊! 所以今天的考试题是: 巡访 (path.pas/c/cpp) Chanxer终于当上了“中华农民联盟”的盟主,他举目四望,决定四处走走,巡视自己的农土. “中华农民联盟”的成员有个村庄,在“村村通”计划中,村庄们被条道路联通了起来,Chanxer计划从某个村庄出发,访问所有的村庄. 可是Chanxer出行有一个特殊的要求,那就是必须以农车代步,现在我们知道哪些村庄配

[2015hdu多校联赛补题]hdu 5297 Y sequence

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5297 题意:给你一个所有正整数的序列,然后去掉满足x^(2~r)的所有数(x为所有正整数,r>=2题目给出),问你现在这个序列的第n个数是什么 解:首先想到写一个函数func(y),它可以计算出给定数字y是序列中第几个数,这样我们大概可以二分答案~(事实上会TLE,得用迭代法,当然迭代的话也是用这个函数) 那么如何实现func: 首先想去掉满足x^2的所有数,我们可以用pow(y, 1/2)计算出y

HDU 5297 Y sequence 容斥/迭代

Y sequence Problem Description Yellowstar likes integers so much that he listed all positive integers in ascending order,but he hates those numbers which can be written as a^b (a, b are positive integers,2<=b<=r),so he removed them all.Yellowstar ca

HDU 5297 Y sequence

Y sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 658    Accepted Submission(s): 145 Problem Description Yellowstar likes integers so much that he listed all positive integers in ascen

HDU 5297(Y sequence-Mobius函数容斥+迭代)

Y sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1192    Accepted Submission(s): 265 Problem Description Yellowstar likes integers so much that he listed all positive integers in asce

HDU 5297 Y sequence Y数列

题意:给定正整数n和r.定义Y数列为从正整数序列中删除全部能表示成a^b(2 ≤ b ≤ r)的数后的数列,求Y数列的第n个数是多少. 比如n = 10. r = 3,则Y数列为2 3 5 6 7 10 11 12 13 14,第10个数是14. 非常有趣的一道数论题.题目给出的范围是long long范围的. 所以显然不用去枚举每一个数了,更不用说推断每一个数是不是某个数的某次方.那么这个题怎么下手呢.首先我们能够非常直观的想到,被删去的数显然是非常分散的.由于能表示成某个数的幂这种形式的数非

HDU 4705 - Y

Y Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2575    Accepted Submission(s): 723 Problem Description Sample Input 4 1 2 1 3 1 4 Sample Output 1 题意: 给你一棵树,求三个点不连续的个数 思路: 总的减去,3个点连续的 AC代码: