题目链接: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. 移动端调试 — 安卓机+chrome

    移动端开发时,我们常使用chrome自带的模拟器,模拟各种手机设备. 但模拟毕竟是模拟,当开发完毕,使用真机访问页面出现问题时如何调试呢? 下面介绍一种针对Android机的调试方法 1. 在pc和a ...

  2. win2019

    slmgr /upkslmgr /ipk N69G4-B89J2-4G8F4-WWYCC-J464Cslmgr /skms zh.us.toslmgr /ato

  3. centos7中没有service iptables save指令来保存防火墙规则

    1.任意运行一条iptables防火墙规则配置命令: iptables -P OUTPUT ACCEPT 2.对iptables服务进行保存: service iptables save 如果上述命令 ...

  4. http代理工具delphi源码

    http://www.caihongnet.com/content/xingyexinwen/2013/0721/730.html http代理工具delphi源码 以下代码在 DELPHI7+IND ...

  5. PHP安装-centos7

    下载地址:https://www.php.net/downloads.php 1.wget下载php源码至/usr/local/src 下 wget https://www.php.net/distr ...

  6. 夜雨惊风 Linux下卸载openjdk,安装jdk

    1.查看java版本 1 2 3 4 [root@CFDB2 ~]# java -version openjdk version "1.8.0_171" OpenJDK Runti ...

  7. Vue过渡:用Velocity实现JavaScript钩子

    Velocity is an animation engine with a similar API to jQuery's $.animate(). It has no dependencies, ...

  8. phpcms列表分页ajax加载更多

    1.在phpcms\modules\content\index.php文件中添加以下函数: /*列表分页ajax加载更多*/ public function homeajaxlist() {  if( ...

  9. eclipse 从git取项目,导入为maven项目,新加的方法,报加载主类错误

    eclipse 从git取项目,导入为maven项目,新加的方法,报加载主类错误 具体描述: 整体编译能够编译成功,但新加一个java,里面创建一个main方法,运行时,报无法加载主类的错误, 整体编 ...

  10. 【freemarker】渲染列表一系列操作

    数据模型: public class AddressVo implements Serializable { private static final long serialVersionUID = ...