传送门: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; }

最新文章

  1. 一枚招聘信息——微信支付web前端开发工程师【已招到】
  2. 算术表达式解析(第二版) C++11版
  3. -bash: /usr/local/bin/react-native: No such file or directory
  4. 复杂sql分组查询 ( pivot)
  5. 在ORACLE触发器里调用JAVA程序
  6. SPOJ375 Query on a tree
  7. VIM自动补全插件 - YouCompleteMe--&quot;大神级vim补全插件&quot;
  8. 由javascript中的this指针所想到的
  9. python 最佳入门实践
  10. BeagleBone Black项目实训手册(大学霸内部资料)
  11. Fragment碎片频繁来回切换的时候报java.lang.IllegalStateException: No activity
  12. 一篇非常经典的springMVC注解实现方式详解
  13. 201521123122 《java程序设计》第八周实验总结
  14. 201521044091 《java程序设计》第八周学习总结
  15. OpenStack--Rabbitmq组件消息队列
  16. .net 信息采集ajax数据
  17. k8s HPA自动收缩
  18. 新课程网上选课系统V1.0—适用于中小学校本课程选课、选修课选课
  19. 【资料搜集】DirectX学习
  20. PAT甲题题解-1070. Mooncake (25)-排序,大水题

热门文章

  1. Educational Codeforces Round 47 (Rated for Div. 2) :E. Intercity Travelling
  2. linux execl()函数
  3. PHP.39-扩展-锁机制解决并发-MySQL锁、PHP文件锁
  4. WPF中的命令与命令绑定(一)
  5. 手把手教你玩转CSS3 3D技术
  6. 通过圆形按钮的绘制熟悉Qt的绘图机制,掌握这种终极方法
  7. C++学习012友元
  8. 《机器学习实战》 in python3.x
  9. 目标检测之Faster-RCNN的pytorch代码详解(数据预处理篇)
  10. mongodb数据库操作之简单查询