codeforces 110E Lucky Tree
传送门: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的更多相关文章
- Problem - D - Codeforces Fix a Tree
Problem - D - Codeforces Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...
- CodeForces 109C 树形DP Lucky Tree
赶脚官方题解写得挺清楚的说,=_= 注意数据范围用long long,否则会溢出. #include <iostream> #include <cstdio> #include ...
- CodeForces 146A Lucky Ticket
Lucky Ticket Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submi ...
- CF109 C. Lucky Tree 并查集
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...
- Codeforces 765 E. Tree Folding
题目链接:http://codeforces.com/problemset/problem/765/E $DFS子$树进行$DP$ 大概分以下几种情况: 1.为叶子,直接返回. 2.长度不同的路径长度 ...
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
- Codeforces 121A Lucky Sum
Lucky Sum Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origi ...
- CodeForces 383C Propagating tree
Propagating tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...
- 【19.77%】【codeforces 570D】Tree Requests
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- loj136 (最小瓶颈路,多次询问)
题目描述 给定一个包含 n nn 个节点和 m mm 条边的图,每条边有一个权值.你的任务是回答 k kk 个询问,每个询问包含两个正整数 s ss 和 t tt 表示起点和终点,要求寻找从 s ss ...
- 开启一个项目如何上传到git
1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库 git init 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点 ...
- ORB-SLAM 代码笔记(五)Frame类
Frame类的成员变量主要包含从摄像头获取的图像的 1. 特征点信息(关键点+描述字) 2. 尺寸不变特征所用金字塔信息,这些都定义在ORBextractor对象中 3. 词袋模型参数,用于跟踪失败情 ...
- 响应式js设置
<script> (function anonymous() { // 声明一个函数,并直接的执行 function computed() { let HTML = document.do ...
- crm踩坑记(一)
目录 antd es6 Object.entries() Object.keys() Object.getOwnPropertyNames() 很神奇的用法!!! eslint 傻逼 其他 参考 an ...
- CentOS 7 安装Nginx并实现域名转发
CentOS 7 条件 教程中的步骤需要root用户权限. 一.添加Nginx到YUM源 添加CentOS 7 Nginx yum资源库,打开终端,使用以下命令: sudo rpm -Uvh http ...
- Android2.2以上的版本HttpURLConnection.getContentLength()获取的size跟下载下来的file的legth不相等
2.2以上的版本下载网络资源不完整无法更新.HttpURLConnection.getContentLength()获取的size跟下载下来的file的legth不等. 原因是:HttpURLConn ...
- 「日常训练」Soldier and Badges (CFR304D2B)
题意 (Codeforces 546B) 问对一个序列最少需要增减几个1能使其彼此不同. 分析 模拟处理.需要注意的是,尽管题目中说了an<=3000,问题是,如果一群a全是3000呢(滑稽), ...
- cocos2d-x 场景切换
场景切换的方法 场景切换是通过导演类director实现的,其中的相关方法如下: director.run(new_scene).该方法可以运行场景,只能在启动第一个场景时调用该方法.如果已运行场景, ...
- Spring实战第四章学习笔记————面向切面的Spring
Spring实战第四章学习笔记----面向切面的Spring 什么是面向切面的编程 我们把影响应用多处的功能描述为横切关注点.比如安全就是一个横切关注点,应用中许多方法都会涉及安全规则.而切面可以帮我 ...