传送门:https://codeforces.com/contest/110/problem/E

题意:给你一颗树,节点与节点之间的边有一个边权,定义只由4和7组成的数字是幸运数字,现在要你求一共有多少条路径,使得节点u->v之间至少存在一条边为幸运数字

题解:树形dp+容斥,我们要找有多少条路径上至少存在一条幸运边,那么我们就可以找到所有的不含幸运路径的边然后用所有路径和减去不含幸运路径的边即可

   1,每次输入边权的时候处理边权是否为幸运数字,如果是,那么为1,否则为0

   2,dfs处理,如果边权为0,那么不含幸运数字的路径+1;

   3,容斥,因为是一个三元组,我们先从n个点选一个点,再从n-1个点选一个点,再从n-2个点选一个点,那么一共有n*(n-1)*(n-2)种选取三元组的方法。

      如果这个第i个节点存在没有幸运边的情况,那么就从这些边中选3条,方法数是tmp*(tmp-1)*(tmp-2),这是i->j,i->k都没有幸运数的情况

      另外一种是i->j没有幸运数但i->k有幸运数,或者i->j有幸运数,但i->k没有幸运数的情况为2*tmp*(tmp-1)*(n-tmp)

代码如下:

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define fuck(x) cout<<"["<<x<<"]";
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 1e5+;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+;
struct node{
int v,next;
LL w;
}edge[maxn<<];
int head[maxn],tot;
LL dp[maxn];
void add(int u,int v,int w){
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
void dfs(int u,int fa){
dp[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v;
if(v==fa) continue;
dfs(v,u);
if(edge[i].w==){
dp[u]+=dp[v]+;
dp[v]=;
}
}
} int main(){
#ifndef ONLINE_JUDGE
FIN
#endif
int u,v;
LL n,w,f;
tot=;
memset(head,-,sizeof(head));
scanf("%lld",&n);
for(int i=;i<n;i++){
scanf("%d%d%lld",&u,&v,&w);
f=;
while(w){
if(w%!=&&w%!=) f=;
w/=;
}
add(u,v,f);
add(v,u,f);
}
if(n<=){
printf("0\n");
return ;
}
dfs(,);
LL ans=(LL)n*(n-)*(n-);
for(int i=;i<=n;i++){
if(dp[i]){
LL tmp=dp[i]+;
ans-=tmp*(tmp-)*(tmp-);
ans-=tmp*(tmp-)*((LL)n-tmp)*;
}
}
cout<<ans<<endl; }

codeforces 110E Lucky Tree的更多相关文章

  1. Problem - D - Codeforces Fix a Tree

    Problem - D - Codeforces  Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...

  2. CodeForces 109C 树形DP Lucky Tree

    赶脚官方题解写得挺清楚的说,=_= 注意数据范围用long long,否则会溢出. #include <iostream> #include <cstdio> #include ...

  3. CodeForces 146A Lucky Ticket

    Lucky Ticket Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submi ...

  4. CF109 C. Lucky Tree 并查集

    Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...

  5. Codeforces 765 E. Tree Folding

    题目链接:http://codeforces.com/problemset/problem/765/E $DFS子$树进行$DP$ 大概分以下几种情况: 1.为叶子,直接返回. 2.长度不同的路径长度 ...

  6. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  7. Codeforces 121A Lucky Sum

    Lucky Sum Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origi ...

  8. CodeForces 383C Propagating tree

    Propagating tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...

  9. 【19.77%】【codeforces 570D】Tree Requests

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. 常用数字信号的产生(C实现)-ARMA模型数据生成

    ARMA模型属于信号现代谱估计的范畴,AR模型常用于信号的线性预测.AR模型最后归结为线性方程,MA最后为非线性方程,因此,AR模型使用较多. AR模型最后归结为解Yule-Walker方程,对应矩阵 ...

  2. red hat 7 启动过程(EFI)

    不同版本的Linux系统的启动过程在某些地方是不一样的,现在先来介绍一下red hat 7 的启动过程(EFI). (加电→图形登录界面) 接通电源 按下电源键 EFI固件启动 初始化硬件 从EFI启 ...

  3. js数组长度

    js数组长度,一般使用length 属性即可获取,但这个数组是个对象则只能使用以下方式 var t=typeof o; var length=0; if(t=='string'){ length=o. ...

  4. OpenCV代码提取:flip函数的实现

    OpenCV中实现图像翻转的函数flip,公式为: 目前fbc_cv库中也实现了flip函数,支持多通道,uchar和float两种数据类型,经测试,与OpenCV3.1结果完全一致. 实现代码fli ...

  5. 13 ThreadLocal

    ThreadLocal 在多线程环境下,每个线程都有自己的数据.一个线程使用自己的局部变量比使用全局变量好,因为局部变量只有线程自己能看见,不会影响其他线程,而全局变量的修改必须加锁. 1. 使用函数 ...

  6. 3124: [Sdoi2013]直径

    3124: [Sdoi2013]直径 https://www.lydsy.com/JudgeOnline/problem.php?id=3124 分析: 所有直径都经过的边,一定都是连续的一段.(画个 ...

  7. Java面试题集合

    1.Java的HashMap是如何工作的? HashMap是一个针对数据结构的键值,每个键都会有相应的值,关键是识别这样的值. HashMap 基于 hashing 原理,我们通过 put ()和 g ...

  8. C#读写txt文件的两种方法介绍 v

    C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...

  9. mysql_connect(): [2002] No such file or directory

    在mac中搭建php的开发环境 1. apach ---- 推荐用MAMP.你只要把你的php文件/项目放入到htdocs(/Applications/MAMP/htdocs)目录下,启动mamp,输 ...

  10. 事务消息中心-TMC

    此文已由作者杨凯明授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 背景 为什么要做事务消息中心 原有kqueue的方式缺点: 降低业务库性能 占用业务库磁盘 历史数据管理成本 ...