HackerRank "Kundu and Tree" !!
Learnt from here: http://www.cnblogs.com/lautsie/p/3798165.html
Idea is: we union all pure black edges so we get 1+ pure black edge groups. Then we can simply pick only 1 vertex from each pure-black group to form a triangle. Then it will be all combination problem.
Only with some data type tuning to make it pass all tests:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <unordered_map>
using namespace std; unordered_map<int, int> parent;
unordered_map<int, long long> num; #define M 1000000007 int find(int i)
{
while(i != parent[i]) i = parent[i];
return i;
}
void merge(int i, int j) // merge j to i
{
int pi = find(i);
int pj = find(j);
if (pi == pj) return; num[pi] += num[pj];
parent[pj] = pi;
} int main()
{
// Get input
int n; cin >> n;
for (int i = ; i <= n; i++)
{
parent[i] = i;
num[i] = ;
}
// union all black edges
for (int i = ; i < (n-); i++)
{
int a, b;
cin >> a >> b;
char c;
cin >> c;
if (c == 'b')
{
merge(a, b);
}
} // Now we have grouped all connected pure black edges // Idea is, we pick 1 from 1 black set to make sure there's no pure
// black edges in one triangle. and we pick three
long long p1 = ;
long long p2 = ;
long long p3 = ;
for(int i = ; i <= n; i ++)
{
if(i == parent[i])
{
long long x = num[i]; p1 += x;
p2 += (x * x);
p3 += (x * x * x);
}
} long long ret = p1 * p1 * p1 - * p2 * p1 + * p3;
cout << ((ret / ) % M) << endl; return ;
}
HackerRank "Kundu and Tree" !!的更多相关文章
- HackerRank "Self Balancing Tree"
Something to learn: Rotation ops in AVL tree does not require recursion. https://github.com/andreima ...
- *[hackerrank]Cut the tree
https://www.hackerrank.com/contests/w2/challenges/cut-the-tree 树分成两部分,求两部分差最小.一开始想多了,后来其实求一下总和,求一下分部 ...
- 【HackerRank】Utopian tree
The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occu ...
- 51nod1253 Kundu and Tree
树包含N个点和N-1条边.树的边有2中颜色红色('r')和黑色('b').给出这N-1条边的颜色,求有多少节点的三元组(a,b,c)满足:节点a到节点b.节点b到节点c.节点c到节点a的路径上,每条路 ...
- 51nod_1253:Kundu and Tree(组合数学)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1253 全为红边的情况下,ans=C(n,3).假设被黑边相连 ...
- 51Nod1253 Kundu and Tree 容斥原理
原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1253.html 题目传送门 - 51Nod1253 题意 树包含 N 个点和 N-1 条边.树的边有 ...
- 51nod-1253: Kundu and Tree
[传送门:51nod-1253] 简要题意: 给出一棵n个点的树,树上的边要么为黑,要么为红 求出所有的三元组(a,b,c)的数量,满足a到b,b到c,c到a三条路径上分别有至少一条红边 题解: 显然 ...
- 【51nod1253】Kundu and Tree(容斥+并查集)
点此看题面 大致题意: 给你一棵树,每条边为黑色或红色, 求有多少个三元组\((x,y,z)\),使得路径\((x,y),(x,z),(y,z)\)上都存在至少一条红色边. 容斥 我们可以借助容斥思想 ...
- 51nod 1253:Kundu and Tree(组合数学)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1253 所有的三元组的可能情况数有ans0=C(n,3).然后 ...
随机推荐
- 用python处理数学问题
一, 计算对数: >>> import math #导入数学模块>>> math.log(8,2) #计算以2为底 8的对数3.0>&g ...
- (实用篇)php 文件夹删除,清除缓存程序
<?php header('content-type:text/html;charset=utf-8'); function delFile($fpath) { $filesize = arra ...
- Asp.net 执行回调操作后 无法更新ViewState的问题
源码 待续...
- [转载]oracle 11g不能导出空表的多种解决方法
原文地址:11g不能导出空表的多种解决方法">oracle 11g不能导出空表的多种解决方法作者:Anlin_Yang ORACLE 11g 用exp命令导出库文件备份时,发现只能导出 ...
- 357. Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- 转 用 AXIOM 促进 XML 处理
转自:http://www.ibm.com/developerworks/cn/xml/x-axiom/ AXIOM 还不是另一种对象模型.它有着明确的设计目标:大幅提升 Apache 下一代 SOA ...
- JAG Practice Contest for ACM-ICPC Asia Regional 2016
2016弱校联盟十一专场10.3 传送门 B. Help the Princess! 计算皇后和士兵谁先到达出口即可. C. We don't wanna work! 两个优先队列维护工作中积极性最小 ...
- 1-2 ISO/OSI七层模型简介
相关名词解释: ISO:国际标准化组织 OSI:开放系统互联模型 IOS:苹果操作系统, 但是在计算机网络中,IOS是互联网操作系统,是思科公司为其网络设备开发的操作维护系统 <1>OSI ...
- 如何在远程计算机上运行PowerShell
问题: 不能在WORKGROUP里面的远程计算机里运行PowerShell指令,报错为用户名密码错 解决方法: 把两台机器上(远程计算机其和本机)都加入到trustedhosts 具体请参考 http ...
- 黑马程序员——JAVA基础之final this.和super.的区别
------- android培训.java培训.期待与您交流! ---------- final关键字: final可以修饰类,方法,变量. final修饰的类不可以被继承. final修饰的方法 ...