题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1253

所有的三元组的可能情况数有ans0=C(n,3)。然后减去不符合条件的情况数。

假设被黑边相连的点形成一个特殊的连通块,在一个大小为x的连通块形成的过程中,ans减去cal(x)=C(x,3)+(n-x)*C(x,2)

代码如下

#include<bits/stdc++.h>
using namespace std;
typedef long long LL; const int N=5e4+;
const int mod=;
int fa[N];
LL d[N];
LL ans,n; int Find(int x)
{
return x==fa[x]? x:fa[x]=Find(fa[x]);
}
int Union(int x,int y)
{
x=Find(x);y=Find(y);
fa[x]=y;
d[y]+=d[x];
}
void init()
{
for(int i=;i<=n;i++)
fa[i]=i,d[i]=;
} LL cal(LL x)
{
return x*(x-)*(x-)/+(n-x)*(x-)*x/;
} int main()
{
while(cin>>n)
{
init();
for(int i=;i<n;i++)
{
int a,b;
char ch;
cin>>a>>b>>ch;
if(ch=='b') Union(a,b);
}
ans=n*(n-)*(n-)/; //ans0=C(n,3)
for(int i=;i<=n;i++)
if(i==fa[i]) ans-=cal(d[i]);
cout<<ans%mod<<endl;
}
}

51nod 1253:Kundu and Tree(组合数学)的更多相关文章

  1. 51nod_1253:Kundu and Tree(组合数学)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1253 全为红边的情况下,ans=C(n,3).假设被黑边相连 ...

  2. 51Nod 1016 水仙花数 V2(组合数学,枚举打表法)

    1016 水仙花数 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 160         难度:6级算法题                水仙花数是指一个 n 位数 ( n≥3 ) ...

  3. HackerRank "Kundu and Tree" !!

    Learnt from here: http://www.cnblogs.com/lautsie/p/3798165.html Idea is: we union all pure black edg ...

  4. 51nod1253 Kundu and Tree

    树包含N个点和N-1条边.树的边有2中颜色红色('r')和黑色('b').给出这N-1条边的颜色,求有多少节点的三元组(a,b,c)满足:节点a到节点b.节点b到节点c.节点c到节点a的路径上,每条路 ...

  5. 51Nod1253 Kundu and Tree 容斥原理

    原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1253.html 题目传送门 - 51Nod1253 题意 树包含 N 个点和 N-1 条边.树的边有 ...

  6. 51Nod 1486 大大走格子 —— 组合数学

    题目链接:https://vjudge.net/problem/51Nod-1486 1486 大大走格子 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: ...

  7. 51nod-1253: Kundu and Tree

    [传送门:51nod-1253] 简要题意: 给出一棵n个点的树,树上的边要么为黑,要么为红 求出所有的三元组(a,b,c)的数量,满足a到b,b到c,c到a三条路径上分别有至少一条红边 题解: 显然 ...

  8. 【51nod1253】Kundu and Tree(容斥+并查集)

    点此看题面 大致题意: 给你一棵树,每条边为黑色或红色, 求有多少个三元组\((x,y,z)\),使得路径\((x,y),(x,z),(y,z)\)上都存在至少一条红色边. 容斥 我们可以借助容斥思想 ...

  9. NOIP前做题记录

    鉴于某些原因(主要是懒)就不一题一题写了,代码直接去\(OJ\)上看吧 CodeChef Making Change 传送门 完全没看懂题解在讲什么(一定是因为题解公式打崩的原因才不是曲明英语太差呢- ...

随机推荐

  1. 29 August

    P1352 Bosses' Masquerade 树形DP模板. #include <cstdio> #include <algorithm> using namespace ...

  2. python cv2在验证码识别中的使用

    使用函数cv2.imread(filepath,flags)读入一副图片 filepath:要读入图片的完整路径 flags:读入图片的标志 cv2.IMREAD_COLOR:默认参数,读入一副彩色图 ...

  3. (转)基于TLS证书手动部署kubernetes集群(上)

    转:https://www.cnblogs.com/wdliu/archive/2018/06/06/9147346.html 一.简介 Kubernetes是Google在2014年6月开源的一个容 ...

  4. p5471 [NOI2019]弹跳

    分析 代码 #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define ...

  5. DB-MD:MD/主数据

    ylbtech-DB-MD:MD/主数据 主数据(MD Master Data)指系统间共享数据(例如,客户.供应商.账户和组织部门相关数据).与记录业务活动,波动较大的交易数据相比,主数据(也称基准 ...

  6. Mybatis入门之MyBatis项目案例

    一.项目案例演示 后台管理系统用户数据维护平台 所有用户数据查询 单个用户数据查询 用户数据修改(完善资料) 锁定用户账号 删除用户账号 彻底删除用户账号 二.数据库数据准备工作 数据库:mysql ...

  7. poj2431Expedition

    A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poo ...

  8. ruby基本语法(1)

    一些学习资源 http://www.codecademy.com/zh/courses/ruby-beginner-en-d1Ylq/0/5?curriculum_id=5059f8619189a50 ...

  9. html5_websql

    var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);  var msg;  db.transaction(function ...

  10. ubuntu18.04 安装 jdk

    1.当前路径(如果不想下载到当前路径,可以先cd到指定路径,再开始下载),官网下载JDK文件jdk-8u121-linux-x64.tar.gz $ wget https://download.ora ...