Educational Codeforces Round 64 (Rated for Div. 2)D(并查集,图)
#include<bits/stdc++.h>
using namespace std;
int f[2][200007],s[2][200007];//并查集,相邻点
int find_(int *f,int x){
return f[x]==x?x:f[x]=find_(f,f[x]);
}
void add(int *f,int *s,int x,int y){
x=find_(f,x);
y=find_(f,y);
if(x!=y)//不在一个联通块,将x并入y所在联通块中,将块内的相邻类型点数量更新
f[x]=y,s[y]+=s[x];
}
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i){
f[0][i]=f[1][i]=i;
s[0][i]=s[1][i]=1;
}
int x,y,z;
for(int i=1;i<n;++i){
scanf("%d%d%d",&x,&y,&z);
add(f[z],s[z],x,y);
}
long long ans=0;
for(int i=1;i<=n;++i)
ans+=1ll*s[0][find_(f[0],i)]*s[1][find_(f[1],i)]-1;
//从i点出发,终点为0或1结点,共有x-1+y-1,从0结点出发经过i终点为1结点,共有(x-1)*(y-1),合计x*y-1
printf("%lld",ans);
return 0;
}
Educational Codeforces Round 64 (Rated for Div. 2)D(并查集,图)的更多相关文章
- Educational Codeforces Round 64 (Rated for Div. 2)题解
Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...
- Educational Codeforces Round 64 (Rated for Div. 2) A,B,C,D,E,F
比赛链接: https://codeforces.com/contest/1156 A. Inscribed Figures 题意: 给出$n(2\leq n\leq 100)$个数,只含有1,2,3 ...
- Educational Codeforces Round 64 (Rated for Div. 2) (线段树二分)
题目:http://codeforces.com/contest/1156/problem/E 题意:给你1-n n个数,然后求有多少个区间[l,r] 满足 a[l]+a[r]=max([l, ...
- Educational Codeforces Round 65 (Rated for Div. 2)题解
Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
随机推荐
- Java集合类--->入门下篇
HashSet集合 在上篇大概了解了什么是集合类,知道它可以存储任意类型的对象,并且比数组灵活,集合类的长度可以变化.这里将接着介绍一下,Set接口的实现类之一,HashSet集合,Set集合:元素不 ...
- Java_异常_01_org.apache.commons.lang.exception.NestableRuntimeException
异常信息: The type org.apache.commons.lang.exception.NestableRuntimeException cannot be resolved. It is ...
- BEC listen and translation exercise 45
So the Counselling Services we offer deal with any problems arising from your studies or in your lif ...
- 关于C++多态的理解
多态,即多种形态.对于具有继承关系的一类对象,子类表现出了父类的某些特性,但是表现的不一样,这就是多态的现实体现.例如动物可以发声,但是狗是旺旺,狗是动物的一种,但是表现了不同的叫的特点,这就是多态. ...
- FFMPEG内存操作(二)从内存中读取数及数据格式的转换
相关博客列表: FFMPEG内存操作(一) avio_reading.c 回调读取数据到内存解析 FFMPEG内存操作(二)从内存中读取数及数据格式的转换 FFmpeg内存操作(三)内存转码器 在雷神 ...
- [CJOJ2425][SYZOI Round1]滑稽的树
cjoj sol 子树转化成dfs序上的区间. 所以就变成了:区间Kth,区间内[a,b]范围内的数有多少个,单点修改 裸的树套树啊. code #include<cstdio> #inc ...
- 11g 如何添加,替换,移除,迁移 OCR ?
一: 增加 裸设备上,创建至少280MB的裸设备,权限是640,属主是root:oinstall共享文件系统 Or NFS,创建空文件,权限是640,属主是root:oinstall root用户执行 ...
- POJ2559:Largest Rectangle in a Histogram
浅谈栈:https://www.cnblogs.com/AKMer/p/10278222.html 题目传送门:http://poj.org/problem?id=2559 贪心的想,最大的子矩阵顶部 ...
- HDU1257(简单DP)
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- POJ3087(模拟)
#include"iostream" #include"string" #include"map" using namespace std; ...