链接

这题可以算树形DP吧 树上的递推

对于树上的某个节点 反着算比较好做 就是算有多少有simple路径的

固定某个节点u 另两个节点 有两种取法

1.从不同子树里各选一个

2.从所有子树里选一个 再从以u为跟的子树 外面选一个

求总和

 #pragma comment(linker, "/STACK:16777216")
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define N 100010
#define LL __int64
struct node
{
int u,v,next;
}ed[N<<];
int head[N],t;
LL sum[N],ans,n;
void init()
{
t = ;
memset(head,-,sizeof(head));
}
void add(int u,int v)
{
ed[t].u = u;
ed[t].v = v;
ed[t].next = head[u];
head[u] = t++;
}
LL dfs(int pre,int u)
{
int i;
LL s=,ss;
sum[u] = ;
for(i = head[u]; i!=- ; i = ed[i].next)
{
int v = ed[i].v;
if(v==pre) continue;
ss = dfs(u,v);
ans+=s*ss;
s+=ss;
sum[u]+=ss;
}
ans+=s*(n-sum[u]);
return sum[u];
}
int main()
{
int i,u,v;
while(scanf("%I64d",&n)!=EOF)
{
init();ans=;
memset(sum,,sizeof(sum));
for(i = ; i < n ; i++)
{
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(-,);
LL oo = (n-)*n/*(n-)/;
printf("%I64d\n",oo-ans);
}
return ;
}

hdu4705Y的更多相关文章

随机推荐

  1. GCD 多线程

    Grand Central Dispatch (GCD)是Apple开发的一个多核编程的较新的解决方法.它主要用于优化应用程序以支持多核处理器以及其他对称多处理系统.它是一个在线程池模式的基础上执行的 ...

  2. 九度OJ 1351 数组中只出现一次的数字

    题目地址:http://ac.jobdu.com/problem.php?pid=1351 题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 输 ...

  3. 24种设计模式--访问者模式【Visitor Pattern】

    今天天气不错,绝对是晴空万里,骄阳似火呀,好,我们今天来讲访问者模式,我们在前面讲了组合模式和迭代器模式,通过组合模式我们能够把一个公司的人员组织机构树搭建起来,给管理带来非常大的便利,通过迭代器模式 ...

  4. PHP学习笔记——PHP脚本和JAVA连接mysql数据库

    环境 开发包:appserv-win32-2.5.10 服务器:Apache2.2 数据库:phpMyAdmin 语言:php5,java 平台:windows 10 java驱动:mysql-con ...

  5. redis 入门笔记(一)

    redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的web应用程序的完美解决方案 三个主要特点:      1,Redis数据库完全在内存中,使用磁盘仅用于持久性       ...

  6. C++带参数默认值的函数

    定义形式: void fun(int a = 1 ,int b = 2 ,int c = 3 ,int d = 4){ //函数定义 cout<<"a="<< ...

  7. Beyond Compare 使用介绍

    Beyond Compare 背景 平时工作中对于源代码都是使用SVN来管理,在线状态下工作的很好,但是有时候离线状态下,对于多个版本之间的代码合并就比较麻烦.尤其是涉及到多人协作时更是如此. 所以找 ...

  8. 遍历 TextBox控件

    foreach (System.Windows.Forms.Control control in this.Controls) { if (control is System.Windows.Form ...

  9. php socket connect permission denied

    Linux在php socket连接时报错:permission denied 解决办法: # setsebool httpd_can_network_connect=1 参考来源: http://w ...

  10. Python学习_Python 3.X版本导入httplib模块报ImportError解决方案

    之前用Python 2.7版本的httplib做接口测试时,运行代码都是正常的, 最近开始用Python 3.3之后,再去看以前的代码,发现import httplib出现错误:Unresolved ...