Codeforces 109C Lucky Tree 组合计数+dfs
来源:互联网 发布:网络摄像机安装图解 编辑:程序博客网 时间:2024/05/22 01:34
题目链接:点击打开链接
题意:
给定n个点的树,有边权。
定义lucky number:数字只有4或7组成
对于一个三元组(i, j, k)
若path(i,j) 路径上的数字存在lucky number && path(i,k) 路径上的数字存在lucky number
则三元组合法。
问有多少个合法的三元组。
( (i,j,k) != (i,k,j) )
用全集-补集。dfs出每个只由 非lucky number 构成的联通块 的点数 x 。
然后方法数就是 x*(x-1)*(x-2) + x*(x-1) * (n-x) * 2
#include <cstdio>#include <iostream>#include <algorithm>#include <string.h>#include <math.h>#include <vector>#include <map>using namespace std;#define ll long long#define N 100100bool fix(int x){if(x == 0)return false;while(x){int y = x%10;if(y!=4 && y!=7)return false;x /= 10;}return true;}struct Edge{int from, to, val, nex;}edge[N<<2];int head[N], edgenum;void add(int u, int v, int val){Edge E = {u, v, val, head[u]};edge[edgenum] = E;head[u] = edgenum++;}ll dp[N];ll dfs(int u, int fa){dp[u] = 0;for(int i = head[u]; ~i; i = edge[i].nex){int v = edge[i].to;if(v == fa)continue;dfs(v, u);if(edge[i].val == 0){dp[u] += dp[v]+1;dp[v] = 0;}}}void init(){memset(head, -1, sizeof head); edgenum = 0;}int n;void solve(){int i, j, u, v, val;init();for(i = 1; i < n; i++) {scanf("%d %d %d",&u,&v,&val);add(u, v, val);add(v, u, val);}if(n<=2){puts("0");return ;}for(i = 0; i < edgenum; i+=2)edge[i].val = edge[i^1].val = fix(edge[i].val);ll all = (ll)n;all = all * (all-1) *(all-2);dfs(1, 1);for(i = 1; i <= n; i++) if(dp[i]){ll ans = dp[i] +1;all -= ans * (ans-1) * (ans-2);all -= ans * (ans-1) * ((ll)n - ans) * 2LL;}cout<<all<<endl;}int main(){while(~scanf("%d",&n)){solve();}return 0;}
0 0
- Codeforces 109C Lucky Tree 组合计数+dfs
- Codeforces 109 C. Lucky Tree
- Codeforces 145D Lucky Pair (组合计数)
- CodeForces - 630C Lucky Numbers【计数】
- CF 109C: Lucky Tree
- CodeForces 430C Xor-tree(DFS)
- Codeforces 430C Xor-tree【Dfs+思维】
- Codeforces 430C Xor-Tree DFS,奇偶性
- CodeForces-630C. Lucky Numbers
- Codeforces 630C Lucky Numbers
- CodeForces-630C. Lucky Numbers
- CodeForces 630C:Lucky Numbers
- CodeForces - 630C Lucky Numbers
- 【CodeForces】[630C]Lucky Numbers
- Codeforces 630C Lucky Numbers
- codeforces 43C Lucky Tickets
- codeforces 287 C. Lucky Permutation
- Codeforces 682C C. Alyona and the Tree (DFS)
- 关于系统调用和内核函数
- jquery ajax post json data(int,string,array,object(own an array property member) to SpringMVC Server
- oracle中长用到的单行函数 (从实践中学习Oracle SQL-第四章)
- HDU--1200:To and Fro (字符串)
- Android中TextView如何实现水平和垂直滚动
- Codeforces 109C Lucky Tree 组合计数+dfs
- Linux的cached和pagecache清除 解决缓存、内存不足、测试结果影响问题
- 设计模式一:单例模式(续)
- spring3.0详解
- 撒空间的好记撒还记得会
- iOS_28仿QQ空间_控制器的切换_自定义segmentCtrol
- 实践活动速回复光度法回到家
- opencv 播放视频
- eclipse页面内出现奇怪符号