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 ...
随机推荐
- C#统计目录中文件MD5值
1. [代码]统计目录中文件MD5值 using System.IO;using System.Security.Cryptography;using System.Collections;using ...
- asp.net ajax实现md5加密
1. [图片] asp.net ajax 效果截图.png 2. [代码]前端代码HTML/Javascript/jQuery <!DOCTYPE html PUBLIC "-//W3 ...
- Selenium-多窗口处理
弹出新的窗口,该如何处理 1.获取当前窗口句柄 2.元素的操作,打开新的窗口 3.获取所有窗口句柄 4.for循环遍历所有窗口,定位到需要操作的窗口上 和你当前句柄不一样的就说明是新的,通过打印tit ...
- 通知消息与ON_NOTIFY
1.通知消息一般是由子控件发出,由父窗口响应,因此响应函数的位置在父窗口内. 2.通知消息发送给父窗口的是通知码,即WM_NOTIFY消息(但为了区分方便不同的消息有不同的名称,但都是以WM_NOTI ...
- Python语言及其应用 第2章
- UML Design Via Visual Studio-Sequence Diagram
本文主要介绍在Visual Studio中设计时序图,内容如下: 何时使用时序图 时序图元素介绍 条件.循环在时序图中的使用 直接通过代码生成时序图 一.何时使用时序图 当要查看单个用例内若干对象的行 ...
- bzoj 3924 幻想乡战略游戏 —— 动态点分治
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3924 参考了博客:https://blog.csdn.net/qq_34564984/art ...
- bzoj 4372 烁烁的游戏 —— 动态点分治+树状数组
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4372 本以为和 bzoj3730 一样,可以直接双倍经验了: 但要注意一下,树状数组不能查询 ...
- spring IOC 注解@Resource
1.@Resource(重要)a)加入 :j2ee/common-annotations.jar b)默认按名称,名称找不到,按类型 默认按照名称setName1到xml中找和id相同的,没有的话再找 ...
- JAVAmap容器基本使用
import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.Scanner ...