#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(并查集,图)的更多相关文章

  1. Educational Codeforces Round 64 (Rated for Div. 2)题解

    Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...

  2. 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 ...

  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, ...

  4. Educational Codeforces Round 65 (Rated for Div. 2)题解

    Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...

  5. 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 ...

  6. 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 ...

  7. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  8. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  9. 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 ...

随机推荐

  1. DIV CSS 笔记

    /*针对谷歌浏览器内核支持的CSS样式*/ <style type="text/css"> @media screen and (-webkit-min-device- ...

  2. 机器学习(十九)— xgboost初试kaggle

     1.官网下载kaggle数据集Homesite Competition数据集,文件结构大致如下: 2.代码实战 #Parameter grid search with xgboost #featur ...

  3. 计算机_网络_01_配置IE代理

    一.配置代理 1.打开代理设置 打开chrome浏览器设置->高级设置->系统->打开代理设置 2.打开局域网设置 Internet属性->连接->局域网设置 3.配置代 ...

  4. 09 - Django应用第六步

    1 自定义页面 在项目中, 肯定不会只是显示单纯的HTML文件, 我们可能还需要JS, CSS, 图片等 这些文件就被称为静态文件(starIc files) 在小的项目中, 可能静态文件比较少, 但 ...

  5. Qt Create 4.6.2无法自动生成Android Kit

    开发环境: OS,Microsoft Windows [Version 10.0.17134.523] Qt,5.11.1 Qt Creator,4.6.2 JDK,1.8.0_181 Android ...

  6. HihoCoder1663双阶乘的末尾数字([Offer收割]编程练习赛40)(暴力||数学)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定正整数x和k,判断是否存在正整数1 ≤ y ≤ x使得x与y同奇偶且(x!!)/(y!!)的个位数字为k. 其中x!! ...

  7. vijos1782:借教室

    描述 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借教室的信息,我们自然希望 ...

  8. http请求中的get和post的区别

    1.标准答案 GET在浏览器回退时是无害的,而POST会再次提交请求. GET产生的URL地址可以被Bookmark,而POST不可以. GET请求会被浏览器主动cache,而POST不会,除非手动设 ...

  9. python中报中文编码异常,Non-ASCII ,but no encoding declared

    异常信息: SyntaxError: Non-ASCII character '\xe5' in file a.py on line 9, but no encoding declared; see ...

  10. DevExpress 显示进度条

    1.使用了DevExpress的WaitDialogForm WaitDialogForm waitDialogForm = null; new Thread((ThreadStart)delegat ...