Codeforces 109C Lucky Tree 组合计数+dfs

题目链接:点击打开链接

题意:

给定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 100100
bool 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;
}
时间: 2024-07-29 11:52:29

Codeforces 109C Lucky Tree 组合计数+dfs的相关文章

codeforces 110E Lucky Tree

传送门:https://codeforces.com/contest/110/problem/E 题意:给你一颗树,节点与节点之间的边有一个边权,定义只由4和7组成的数字是幸运数字,现在要你求一共有多少条路径,使得节点u->v之间至少存在一条边为幸运数字 题解:树形dp+容斥,我们要找有多少条路径上至少存在一条幸运边,那么我们就可以找到所有的不含幸运路径的边然后用所有路径和减去不含幸运路径的边即可 1,每次输入边权的时候处理边权是否为幸运数字,如果是,那么为1,否则为0 2,dfs处理,如果边权

CodeForces - 383C Propagating tree(dfs + 线段树)

题目大意: 给出一棵树,树上每个节点都有权值,然后有两个操作. 1 x val 在结点x上加上一个值val,x的儿子加上 -val,x的儿子的儿子加上 - (-val),以此类推. 2 x 问x节点的值. 思路分析: 每个节点上加值都是给自己的儿子节点加,而且这个是颗树. 比如样例上的,如果你给node 1加一个值,那么五个节点都加. 再给node 2加个值,2的儿子节点也加了,之前给1加的值也要加到2号节点的儿子. 所以你会发现节点的儿子会存在一个从属的关系. 这样的话,我们可以把所有节点从新

[HDU 3461] Saving Beans &amp; 组合计数Lucas定理模板

Saving Beans Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description Although winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold

[ZJOI2010]排列计数 (组合计数/dp)

[ZJOI2010]排列计数 题目描述 称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Magic的,答案可能很大,只能输出模P以后的值 输入输出格式 输入格式: 输入文件的第一行包含两个整数 n和p,含义如上所述. 输出格式: 输出文件中仅包含一个整数,表示计算1,2,?, 的排列中, Magic排列的个数模 p的值. 输入输出样例 输入样例#1: 20 23 输出样例#1: 16 说明

3.29省选模拟赛 除法与取模 dp+组合计数

LINK:除法与取模 鬼题.不过50分很好写.考虑不带除法的时候 其实是一个dp的组合计数. 考虑带除法的时候需要状压一下除法操作. 因为除法操作是不受x的大小影响的 所以要状压这个除法操作. 直接采用二进制状压是不明智的 2的个数最多为13个 2^13也同样到达了1e4的复杂度. 考虑 hash状压 即 2的个数有x个 那么我们就有状态w表示2还有x个. 这样做的原因是把一些相同的东西给合并起来 而并非分散开来.即有多个2直接记录有多少个即可. 可以发现 这样做不同的除数最多只有5个 状态量较

Codeforces 6D Lizards and Basements 2 dfs+暴力

题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h> #include<queue> #include<string> #include<stdlib.h> #include<a

Yue Fei&#39;s Battle(组合计数递推)

//求一个直径为 k 的树有多少种形态,每个点的度不超过 3 // 非常完美的分析,学到了,就是要细细推,并且写的时候要细心 还有除法取模需要用逆元 #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> using namespace std; #define MOD 1000000007 #define L

CodeForces 440C One-Based Arithmetic(递归,dfs)

A - One-Based Arithmetic Time Limit:500MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2014-08-21) Description Prof. Vasechkin wants to represent positive integer n as a sum of adden

POJ 1496 POJ 1850 组合计数

Code Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8256 Accepted: 3906 Description Transmitting and memorizing information is a task that requires different coding systems for the best use of the available space. A well known system is t