CF109 C. Lucky Tree 并查集

时间:2022-09-20 19:49:52

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
4
1 2 4
3 1 2
1 4 7
output
16
input
4
1 2 4
1 3 47
1 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+; int fa[maxn];
LL sum[maxn]; void solve(); int main()
{
solve();
return ;
} //初始化
void init(int n)
{
for(int i=;i<=n;i++){
fa[i]=i;
sum[i]=;
}
} //判断是不是幸运边
bool ok(int w)
{
while(w){
int cur=w%;
if(cur!= && cur!=)
return false;
w/=;
}
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]=;
fa[x]=cur;
return cur;
} void solve()
{
int n;
scanf("%d",&n);
init(n);
for(int i=;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]=;
}
}
}
if(n<){
printf("0\n");
return ;
}
LL ans=;
for(int i=;i<=n;i++){
if(find_fa(i)==i){
if(sum[i]>)
ans+=(sum[i]*(sum[i]-)*(sum[i]-));
ans+=2LL*sum[i]*(sum[i]-)*(n-sum[i]);
}
}
ans=n*(n-1LL)*(n-2LL)-ans; printf("%I64d\n",ans);
return ;
}

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

  1. Hdu&period;1325&period;Is It A Tree&quest;&lpar;并查集)

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  2. Is It A Tree&quest;&lpar;并查集&rpar;

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26002   Accepted: 8879 De ...

  3. HDU 5606 tree 并查集

    tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ans​i​​=size[findset(i)],size表示每个并 ...

  4. &lbrack;Swust OJ 856&rsqb;--Huge Tree&lpar;并查集&rpar;

    题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 Description T ...

  5. Codeforces Round &num;363 &lpar;Div&period; 2&rpar;D&period; Fix a Tree&lpar;并查集&rpar;

    D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  6. Is It A Tree&quest;&lpar;并查集&rpar;(dfs也可以解决)

    Is It A Tree? Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submi ...

  7. tree&lpar;并查集&rpar;

    tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  8. 树上统计treecnt&lpar;dsu on tree 并查集 正难则反&rpar;

    题目链接 dalao们怎么都写的线段树合并啊.. dsu跑的好慢. \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\( ...

  9. hdu 1325 Is It A Tree&quest; 并查集

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

随机推荐

  1. phpstudy 80端口被占用,修改端口

    搭建mantis,总会出现80端口被占用的情况.看到别的步骤是:1.cmd 运行netstat -ano查看80端口被什么占用,然后在任务管理器找到对应的结束进程.通常情况下是被System占用,右击 ...

  2. angular的&dollar;resource factory都有啥

    angular的$resource factory都有啥 A factory which creates a resource object that lets you interact with R ...

  3. Convert&period;ToDateTime(值),方法可以把一个值转化成DateTime类型。

    例子:将日历控件的值转化成DateTime类型. DateTime beginDate = Convert.ToDateTime(this.beginCalendar.EditValue);

  4. 遍历std&colon;&colon;list过程中删除元素后继续遍历过程

    std::list::erase Erase elements Removes from the list container either a single element (position) o ...

  5. twitter接口开发

    前一阵子研究了下twitter接口,发现网上的资料不是很多.遂花了些心血,终于有所收获~ 现在有时间赶紧整理出来便于自己以后查阅,也想帮助有困难的同学们.废话不多说,现在就以最简洁的方式开始了.注意: ...

  6. android开发学习——day3

    关于android开发的详细过程了解 Android App程序的四种重要组成类型:1.Activity 2.Service 3.Content Provider 4.Broadcast Receiv ...

  7. vpshere6 ESXI 禁止登陆 &quot&semi;执行此操作的权限被拒绝&quot&semi;

    vCenter在添加ESXI主机时,锁定模式选择“正常”,导致无法直接登陆ESXI宿主机,现象如下: 解决方法:

  8. Python 字典和集合基于哈希表实现

    哈希表作为基础数据结构我不多说,有兴趣的可以百度,或者等我出一篇博客来细谈哈希表.我这里就简单讲讲:哈希表不过就是一个定长数组,元素找位置,遇到哈希冲突则利用 hash 算法解决找另一个位置,如果数组 ...

  9. opencv学习之路(39)、PCA

    一.PCA理论介绍 网上已经有许多介绍pca原理的博客,这里就不重复介绍了.详情可参考 http://blog.csdn.net/zhongkelee/article/details/44064401 ...

  10. 《机器学习实战》2&period;2&period;2分析数据:使用matplotlib创建散点图

    #输出散点图 def f(): datingDataMat,datingLabels = file2matrix("datingTestSet3.txt") fig = plt.f ...