hdu 4705 排列组合
思路:枚举能是A,B,C在一条简单路径上的中点。 计算多少个几何能满足。在用总数减去
#pragma comment(linker, "/STACK:16777216")
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Maxn 200010
#define Maxm 300010
#define LL __int64
#define Abs(x) ((x)>0?(x):(-x))
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define inf 0x7fffffff
#define Mod 1000000007
using namespace std;
LL num[Maxn];
LL snum[Maxn],vi[Maxn];
vector<LL> son[Maxn];
vector<int> head[Maxn];
void dfs(int u)
{
int i,v,sz;
vi[u]=;
snum[u]=;
sz=head[u].size();
for(i=;i<sz;i++){
v=head[u][i];
if(vi[v]) continue;
dfs(v);
snum[u]+=snum[v];
son[u].push_back(snum[v]);
}
}
void predfs(int u,LL sum)
{
int i,v,sz;
vi[u]=;
if(sum)
son[u].push_back(sum);
sz=head[u].size();
for(i=;i<sz;i++){
v=head[u][i];
if(vi[v]) continue;
predfs(v,sum+snum[u]-snum[v]);
}
}
int main()
{
int i,j,u,v;
LL n;
while(scanf("%I64d",&n)!=EOF)
{
memset(snum,,sizeof(snum));
memset(vi,,sizeof(vi));
memset(num,,sizeof(num));
for(i=;i<=n;i++){
son[i].clear();
head[i].clear();
}
for(i=;i<n;i++){
scanf("%d%d",&u,&v);
head[u].push_back(v);
head[v].push_back(u);
}
dfs();
memset(vi,,sizeof(vi));
predfs(,);
LL ans=n*(n-)*(n-)/;
//cout<<ans<<endl;
LL sz;
LL sum=;
for(i=;i<=n;i++){
sz=son[i].size();
sum=;
for(j=;j<sz;j++)
sum+=son[i][j];
for(j=;j<sz-;j++)
ans-=(son[i][j]*(sum-=son[i][j]));
}
printf("%I64d\n",ans);
}
return ;
}
hdu 4705 排列组合的更多相关文章
- HDU 1521 排列组合 指数型母函数
排列组合 Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status D ...
- Hdu 1521 排列组合
a1 n1 a2 n2 ... ak nkn=n1+n2+...+nk从n个数中选r个排列(不是组合噢)// 指数型母函数// 模板#include <iostream> #include ...
- HDU 1521 排列组合 (母函数)
题目链接 Problem Description 有n种物品,并且知道每种物品的数量.要求从中选出m件物品的排列数.例如有两种物品A,B,并且数量都是1,从中选2件物品,则排列有"AB&qu ...
- hdu 4535(排列组合之错排公式)
吉哥系列故事——礼尚往来 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- hdu 4497(排列组合+LCM和GCD)
GCD and LCM Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total ...
- hdu 1521 排列组合 —— 指数型生成函数
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1521 标准的指数型生成函数: WA了好几遍,原来是多组数据啊囧: 注意精度,直接强制转换(int)是舍去小 ...
- hdu 1521 排列组合【指数型生成函数】
根据套路列出式子:\( \prod_{i=1}^{n}\sum_{j=0}^{c[i]}\frac{x^j}{j!} \),然后暴力展开即可 #include<iostream> #inc ...
- 排列组合+组合数取模 HDU 5894
// 排列组合+组合数取模 HDU 5894 // 题意:n个座位不同,m个人去坐(人是一样的),每个人之间至少相隔k个座位问方案数 // 思路: // 定好m个人 相邻人之间k个座位 剩下就剩n-( ...
- HDU 4497 GCD and LCM(分解质因子+排列组合)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 题意:已知GCD(x, y, z) = G,LCM(x, y, z) = L.告诉你G.L,求满 ...
随机推荐
- Could not load file or assembly 'MagickNet.dll'
1 确定项目中bin目录下存在该DLL文件 2 安装 VC++发布组件_缩略图用_x86(1).exe
- OC: 类的扩展、类的延展、协议、 NSDate
NSDateFormatter 指定⽇日期格式: NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter ...
- PLSQL中便捷的输入
我们可以利用PLSQL工具中的替换功能进行sql语句的输入,具体做法如下: 工具---首选项---用户界面---编辑器 1. 然后点击“edit”按钮: 2. . 在文本框中输入你想要替换的字符,等号 ...
- erlang web socket参考。
出自: http://blog.sina.com.cn/s/blog_96b8a15401010g71.html
- Windows Server 2012远程刷新客户端组策略,IE代理设置
Windows Server 2012远程刷新客户端组策略: 1.PowerShell命令对单台计算机进行刷新: Invoke-GPUpdate -RandomDelayInMinutes 0 -Co ...
- Java练习之最大相同子串
package string.demo; /* 需求:找到两个字符串的最长共同子串 * 思路: * 1.先看短的那个字符串是否在长的那个字符串中,如果存在,短的那个字符串就是最大共同子串 * 2.如果 ...
- Codeforces Round #337 (Div. 2) A. Pasha and Stick 数学
A. Pasha and Stick 题目连接: http://www.codeforces.com/contest/610/problem/A Description Pasha has a woo ...
- Visual Studio原生开发的10个调试技巧(一)
最近碰巧读了Ivan Shcherbakov写的一篇文章,<11个强大的Visual Studio调试小技巧>.这篇文章只介绍了一些有关Visual Studio的基本调试技巧,但是还有其 ...
- [Java] 识别图片验证码
现在大多数网站都采用了验证码来防止暴力破解或恶意提交.但验证码真的就很安全吗?真的就不能被机器识别?? 我先讲讲我是怎么实现站外提交留言到一个网站的程序. 这个网站的留言版大致如下: 我一看这种简单的 ...
- Python 读写文件和file对象(转)
1.open使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('thefile.txt ...