传送门: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. SPFA算法(2) POJ 1511 Invitation Cards

    原题: Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 31230   Accepted: ...

  2. python2.X与python3.X爬虫常用的模块变化对应

    python2 python3 import urllib2 import urllib.request,urllib.error import urllib.request,urllib.error ...

  3. PIC32MZ 通过USB在线升级 -- USB CDC bootloader

    了解bootloader 的实现,请加QQ: 1273623966 (验证填 bootloader):欢迎咨询或定制bootloader:我的博客主页www.cnblogs.com/geekygeek ...

  4. Django-Content-type用法

    from django.db import models from django.contrib.contenttypes.models import ContentType from django. ...

  5. linux里面的fork函数创建一个新进程

    由fork创建的新进程成为子进程,fork函数被调用一次,但是返回两次,

  6. guacamole实现虚拟键盘

    要做的事情比较简单,就是先实例化一个虚拟键盘,然后监听事件即可. js代码 //虚拟键盘数据 var a = {"language":"en_US"," ...

  7. Java Algorithm Problems

    Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...

  8. 3,SQL语句及数据库优化

       1,统一SQL语句的写法 对于以下两句SQL语句,程序员认为是相同的,数据库查询优化器认为是不同的. 所以封装成复用方法,用标准模板来控制. select*from dual select*Fr ...

  9. (原)MongoDB在系统中的使用

    序)Nosql并不是要取代原有的数据产品,而是为不同的应用场景提供更多的选择. 一)结构类型 传统数据库的领域在于结构化文档,对于非结构化文档和半结构化文档,它能处理,但是有一定的缺陷,那么什么又是结 ...

  10. tomcat8编码设置和gc异常解决

    用startup.bat启动 编码解决: 用编辑器打开catalina.bat文件找到set "JAVA_OPTS=%JAVA_OPTS% %JSSE_OPTS% " 更改为 se ...