CF109 C. Lucky Tree 并查集

Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

One day Petya encountered a tree with n vertexes. Besides, the tree was weighted, i. e. each edge of the tree has weight (a positive integer). An edge is lucky if its weight is a lucky number. Note that a tree with n vertexes is an undirected connected graph that has exactly n - 1 edges.

Petya wondered how many vertex triples (i, j, k) exists that on the way from i to j, as well as on the way from i to k there must be at least one lucky edge (all three vertexes are pairwise distinct). The order of numbers in the triple matters, that is, the triple (1, 2, 3) is not equal to the triple (2, 1, 3) and is not equal to the triple (1, 3, 2).

Find how many such triples of vertexes exist.

Input

The first line contains the single integer n (1 ≤ n ≤ 105) — the number of tree vertexes. Next n - 1 lines contain three integers each: ui vi wi (1 ≤ ui, vi ≤ n, 1 ≤ wi ≤ 109) — the pair of vertexes connected by the edge and the edge‘s weight.

Output

On the single line print the single number — the answer.

Please do not use the %lld specificator to read or write 64-bit numbers in С++. It is recommended to use the cin, cout streams or the %I64d specificator.

Sample test(s)

input

41 2 43 1 21 4 7

output

16

input

41 2 41 3 471 4 7447

output

24

Note

The 16 triples of vertexes from the first sample are:(1, 2, 4), (1, 4, 2), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 2, 4), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2).

In the second sample all the triples should be counted: 4·3·2 = 24.

题意:

一条边,如果这一条边的权值只由数字4,7组成,我们就说这条边是一条幸运边,否则不是

现在有一棵树,边有权值,问:

这棵树有多少点对(i,j,k)满足:

i!=j!=k,

并且路径i到j至少经过一条幸运边,路径i到k也至少经过一条幸运边

注意:

点对(i,j,k)和(j,i,k)和(i,k,j)算是不同的点对

这道题,直接算满足的点对好像有点麻烦,正难则反,我们先算出不符合的点对

符合的点对=总点对-不符合的点对

总点对=n*(n-1)*(n-2)

现在考虑不符合的点对

不符合的点对分2种:

1.i到j和i到k2条路径都不包含幸运边

2.2条路径,其中一条经过了幸运边,另外一条没有经过幸运边

这棵树,根据幸运边我们可以分成若干个联通块,这里可以用并查集来实现

find_fa()函数的同时维护数组sum

sum[i]表示i所在的联通块的节点数

接着我们枚举联通块

对于每一个连通块:

2条路径都没有经过幸运边的数量:sum[i]*(sum[i]-1)*(sum[i]-2)

只有一条经过幸运边的数量:2*sum[i]*(sum[i]-1)*(n-sum[i])

累加就得到所有不符合的点对了

注意:

1.并查集记得路径压缩

2.每一个连通块只算一次,不要重复计算

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>

#define LL long long

using namespace std;

const int maxn=1e5+5;

int fa[maxn];
LL sum[maxn];

void solve();

int main()
{
    solve();
    return 0;
}

//初始化
void init(int n)
{
    for(int i=1;i<=n;i++){
        fa[i]=i;
        sum[i]=1;
    }
}

//判断是不是幸运边
bool ok(int w)
{
    while(w){
        int cur=w%10;
        if(cur!=4 && cur!=7)
            return false;
        w/=10;
    }
    return true;
}

//并查集,注意要路径压缩,注意sum数组的更新
int find_fa(int x)
{
    if(fa[x]==x)
        return x;
    int cur=find_fa(fa[x]);
    sum[cur]+=sum[x];
    sum[x]=0;
    fa[x]=cur;
    return cur;
}

void solve()
{
    int n;
    scanf("%d",&n);
    init(n);
    for(int i=1;i<n;i++){
        int u,v,w;
        scanf("%d %d %d",&u,&v,&w);
        if(!ok(w)){
            int fau=find_fa(u);
            int fav=find_fa(v);
            if(fau!=fav){
                fa[fau]=fav;
                sum[fav]+=sum[fau];
                sum[fau]=0;
            }
        }
    }
    if(n<3){
        printf("0\n");
        return ;
    }
    LL ans=0;
    for(int i=1;i<=n;i++){
        if(find_fa(i)==i){
            if(sum[i]>2)
                ans+=(sum[i]*(sum[i]-1)*(sum[i]-2));
            ans+=2LL*sum[i]*(sum[i]-1)*(n-sum[i]);
        }
    }
    ans=n*(n-1LL)*(n-2LL)-ans;

    printf("%I64d\n",ans);
    return ;
}
时间: 2024-10-13 16:21:45

CF109 C. Lucky Tree 并查集的相关文章

HDU 1325 Is It A Tree? 并查集

判断是否为树 森林不是树 空树也是树 成环不是树 数据: 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 0 0 1 2 2 3 4 5 0 0 2 5 0 0 ans: no no yes #include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <malloc.h> #include <ctype

HDU1325 Is It A Tree? 并查集

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1325 这题与HDU1272 小希的迷宫 (并查集) 非常像,不过细细看,还是有一点区别的.就是这题的路径是单向的,每次只能由起点指向终点,在连接之前终点必须是根节点. 注意的问题: 1.不能成环,即每次输入的两个数的根节点不能相同: 2.最终根节点数目为一 3.注意当只输入"0 0" 时要输出"Case %d is a tree." 4.路径是单向的,即每次只能由起点指

HDU 5606 tree 并查集

tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ans?i??=size[findset(i)],size表示每个并查集根的size Ans_i=size[findset(i)],sizeAns?i??=size[findset(i)],size表示每个并查集根的sizesize. #include<cstdio> #include<cstring> #include<algorithm>

HDU 1325 POJ 1308 Is It A Tree? (并查集)

这道题就是裸并查集,关键在于对不是树几种的判断 1. 空树是树 2. 森林不是树 3. 无环 或者从入度来看:1,无环:2,除了根,所有的入度为1,根入度为0:3,这个结构只有一个根,不然是森林了. 这道题本来暑假做的POJ 1308 但是HDU没有过.在于空树没有考虑. 用并查集判断有多少个森林注意编号是随机的,不是次序.... /* input: 0 0 1 1 0 0 1 2 1 2 0 0 1 2 2 3 4 5 0 0 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9

hdu5606 tree (并查集)

tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 823    Accepted Submission(s): 394 Problem Description There is a tree(the tree is a connected graph which contains n points and n−1 edges),t

swust oj 856--Huge Tree(并查集)

题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 There are N trees in a forest. At first, each tree contains only one node as its root. And each node is marked with a number. You're asked to do the following two

[POJ 1308]Is It A Tree?(并查集判断图是否为一棵有根树)

Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties. There is exactly one node, called the root, t

Is It A Tree?(并查集)(dfs也可以解决)

Is It A Tree? Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by direct

树上统计treecnt(dsu on tree 并查集 正难则反)

题目链接 \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\(L\sim R\)号点两两连通,最少需要选择的边的数量. 求\[\sum_{l=1}^n\sum_{r=l}^nTree[l,r]\] \(Solution\) 枚举每条边,计算它的贡献. 那么我们要判断有多少连续区间的点跨过这条边,并不好算,反过来去求在这条边的两侧分别有多少个连续区间. 那么显然有\(O(n^2)\)的做法,即对每条边DFS它的两侧,枚