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).然后 ...
随机推荐
- 五 Servlet 技术
一 Servlet 基础 1. 定义相关: a) 是运行在 Web 服务器上得 Java 小程序 b) 只将处理结果返回给客户 c) 是实现接口 Servlet 的 java 类,能被服务器调用. d ...
- 如何重启Activity
有的时候,我们只是想重启某个Activity,但是不重启整个App. 一种做法是: Intent intent = getIntent(); overridePendingTransition(0, ...
- 说说Audition消除歌曲中的人声
今天再说说消除人声,音乐中人声的消除有多种方法,但都很难完全消除,而且效果越好越难消除,一首音效极佳的立体声歌曲是无法完全消除人声的.本人常用的是以下三种方法.第1种如下图,此方法简单,但是完成后变成 ...
- Javascript模块化编程(三):require.js的用法【转】
作者: 阮一峰 日期: 2012年11月 7日 这个系列的第一部分和第二部分,介绍了Javascript模块原型和理论概念,今天介绍如何将它们用于实战. 我采用的是一个非常流行的库require.js ...
- ehcache 缓存
一:详细配置步骤 1,添加ehcache.xml文件 将ehcache.xml文件添加到src路径下面.ehcache.xml文件内容如下 <ehcache> <diskStore ...
- c#部分---一维数组、冒泡排序、foreach的用法
一维数组:2016-10-14 定义方式:{定义的时候,需要数据类型.长度!} 1.int []aa=new int [5]; 表示数组里面有5个字符: 2.int []aa=new int []{ ...
- PHP强制清除缓存
在页面最顶端加上 <?phpheader("Expires: Mon, 26 Jul 1997 05:00:00 GMT");header("Last-Modifi ...
- android中的DatePicker与TimePicker
1.布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...
- Android—菜单
layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- 【BZOJ1007】【HNOI2008】水平可见直线
依旧看黄学长代码,不过这回是看完后自己写的 原题: 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则Li为被覆盖的.例 ...