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 ...
随机推荐
- mysql的SQL_CALC_FOUND_ROWS 使用 类似count(*) 使用性能更高
mysql的SQL_CALC_FOUND_ROWS 使用 类似count(*) 使用性能更高 在很多分页的程序中都这样写: SELECT COUNT(*) from `table` WHERE ... ...
- 分享知识-快乐自己:Linux下安装 erlang 及 RabbitmMQ
Linux下安装 erlang 及 RabbitmMQ: 下载地址一 下载地址二 下载地址三 安装依赖: yum install ncurses-devel 安装 erlang: 1):下载Erla ...
- 如何理解Box-sizing模型?
CSS3 box-sizing 属性 http://www.w3school.com.cn/tiy/t.asp?f=css3_box-sizing <style> div.containe ...
- 基于FTP服务、JAVA实现文件同步操作
package lixj.ftp; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStre ...
- 「LOJ#10050」「一本通 2.3 例 2」The XOR Largest Pair (Trie
题目描述 在给定的 $N$ 个整数 $A_1,A_2,A_3...A_n$ 中选出两个进行异或运算,得到的结果最大是多少? 输入格式 第一行一个整数$N$. 第二行$N$个整数$A_i$. 输出格式 ...
- ACM学习历程—HDU 5443 The Water Problem(RMQ)(2015长春网赛1007题)
Problem Description In Land waterless, water is a very limited resource. People always fight for the ...
- 第k大区间(平均数)--LH
老师自己出的题,由于没有评测的地方, 我就只说做法啦.(其实是懒得写)(逃ヽ(゚∀゚*)ノ━━━ゥ♪ 以下是个人的见解,如果错了告诉我哦⊙0⊙? 最近特别喜欢用画图写字,,☆⌒(*^-゜)v!
- 服务端返回可执行js格式要求
服务端返回的数据,如果有直接执行的代码,那返回的头部格式中的"Content-Type",不能为"text/plain",不然,浏览器是不会执行返回数据的. 返 ...
- phpMailer在CentOS 6.5下无法发送邮件的解决办法
作者:ffsystem 网站在Windows平台上开发测试,完成了后同步到CentOS6.5 Nigix运行.发现phpMailer组件无法与SMTP服务器建立连接,导致无法发送邮件. 错误代码: p ...
- USB插拔检测程序
一.手动添加ON_WM_DEVICECHANGE()消息 二.添加头文件#include <Dbt.h> 三.定义设备的GUID static const GUID GUID_DEVINT ...