题目链接: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. (转)字符编码笔记:ASCII,Unicode 和 UTF-8

    转:http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html 今天中午,我突然想搞清楚 Unicode 和 UTF-8 之 ...

  2. 10 Advanced Bing Search Tricks You Should Know

    Exclude Websites From Bing Search: wikipedia -wikipedia.org Excluding Keywords From Bing Search: fac ...

  3. day25—JavaScript实现文件拖拽上传案例实践

    转行学开发,代码100天——2018-04-10 今天记录一个利用JavaScript实现文件拖拽上传到浏览器,后天将文件打开的小案例. 基本功能:1点击添加文件 2 文件拖拽添加 html: < ...

  4. Selenium WebDriver高级应用

    WebDriver高级应用 public class Demo4 { WebDriver driver; // @BeforeMethod:在每个测试方法开始运行前执行 @BeforeMethod p ...

  5. 爬虫(六)——存储库(一)MongoDB存储库

    目录 存储库--MongoDB 一.安装MongoDB 4.0 1.安装 2.修改数据库文件和日志保存位置设置(可不改) 3.账户管理.远程服务 三.MongoDB数据库操作 1.对库操作 2.对集合 ...

  6. maven配置本地仓库、maven配置阿里中央仓库、eclipse配置maven

    一.maven配置本地仓库路径 1.打开下载好的maven目录 (若没安装,可以看我写的安装步骤https://www.cnblogs.com/xjd-6/p/11344719.html) 2.进入c ...

  7. Linux系统的镜像文件iso下载地址

    CentOS-6.1-x86_64-bin-DVD1.iso 官方网址:http://archive.kernel.org/centos-vault/6.1/isos/x86_64/ 下载链接地址:h ...

  8. resultType和resultMap一对一查询小结

    resultType和resultMap一对一查询小结 SELECT orders.*, USER .username,USER.birthday,USER.sex,USER.address FROM ...

  9. avaScript —— 常用正则表达式

    用户名 /^[a-z0-9_-]{3,16}$/ 密码 /^[a-z0-9_-]{6,18}$/ 十六进制值 /^#?([a-f0-9]{6}|[a-f0-9]{3})$/ 电子邮箱 /^([a-z0 ...

  10. 数组Array的方法调用

    <script language="JavaScript" type="text/javascript"> var arr = ["11& ...