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 ...
随机推荐
- tkinter窗口系列之一——列表框
以下内容来自https://tkdocs.com/tutorial/morewidgets.html 一个列表框显示由单行文本所组成的一栏条目,通常它很冗长,它允许使用者通过列表浏览其中的内容,选择一 ...
- Luogu P1377 [TJOI2011]树的序:离线nlogn建二叉搜索树
题目链接:https://www.luogu.org/problemnew/show/P1377 题意: 有一棵n个节点的二叉搜索树. 给出它的插入序列,是一个1到n的排列. 问你使得树的形态相同的字 ...
- 十 Django框架,Cookie
注意:获取Cookie是在请求对象里处理,设置Cookie是在响应对象里处理 普通Cookieset_cookie()设置普通cookie 参数: key, 键 value='', 值 max_age ...
- Idea_学习_01_Idea激活
一.激活—激活码 下载地址:官网 1.注册码 http://idea.lanyus.com/ 二.激活—license server 1. (推荐) 弹窗中选择最后一个页面license server ...
- Git_错误_01_failed to push some refs to 'git@github.com
在使用git 对源代码进行push到gitHub时可能会出错,信息如下 此时很多人会尝试下面的命令把当前分支代码上传到master分支上. $ git push -u origin master 但依 ...
- 关于android 图片加载优化
android应用对图片处理算是比较频繁的了,尤其是在程序加载大量图片和高分辨率图片时,最容易产生oom异常,下面是个人平时一些省内存加载方法 方法一: public Bitmap decodeFil ...
- JavaScript下的进制转换
JavaScript下的进制转换 //十进制转其他进制 var num = 99; console.log('十进制: ', num); console.log('八进制:', (num).toStr ...
- C#中如何获取汉字的笔画数和汉字的拼音
以前玩过一个游戏,输入两个人的名字然后点击缘分就能产生一段缘分测试的结果,后来经过分析知道是根据名字笔画数之差来弄的小游戏,于是就在百度上找怎么得到汉字的笔画数,也没找到自己想要的答案,问遍了所有的人 ...
- HDU1257(简单DP)
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- Python:删除字符串中的字符
一.删除字符串两端的一种或多种字符 #strip().lstrip().rstrip()方法:(默认删除空格符) A.list.strip(字符):删除字符串两端的一种或多种字符: #例:删除字符串s ...