【HDU6035】Colorful Tree(dfs,树形dp)
来源:互联网 发布:mac怎么打开mobi文件 编辑:程序博客网 时间:2024/06/05 03:30
记录一个菜逼的成长。。
2017 Multi-University Training Contest - Team 1
题目链接
题目大意:
有一个
求总共
笔记
考虑每个颜色的贡献。
考虑总的答案减去非法值
总的答案
遍历到当前点,往下遍历直到某一子树不包含与当前点颜色相同的点,在这个子树任意选两个点,都不会有当前点颜色的贡献,所以总答案减去子树中的路径数。
#include <bits/stdc++.h>using namespace std;#define fin freopen("D://in.txt","r",stdin)#define fout freopen("D://out.txt","w",stdout)#define rep(i,l,r) for( int i = l; i <= r; i++ )#define rep0(i,l,r) for( int i = l; i < r; i++ )#define ALL(v) (v).begin(),(v).end()#define cl(a,b) memset(a,b,sizeof(a))#define clr clear()#define pb push_back#define mp make_pair#define fi first#define se secondtypedef long long LL;typedef pair<int,int> PII;const int INF = 0x3f3f3f3f;const int maxn = 200000 + 10;int sum[maxn],son[maxn],c[maxn];LL ans ;vector<int>edge[maxn];void dfs(int u,int f){ son[u] = 1; int add = 0,pre = sum[c[u]]; for( int i = 0; i < edge[u].size(); i++ ){ int v = edge[u][i]; if(v == f)continue; dfs(v,u); son[u] += son[v]; int tmp = son[v] - (sum[c[u]] - pre); add += tmp; pre = sum[c[u]]; ans -= (LL)tmp * (tmp - 1) / 2; } sum[c[u]] += add + 1;}int main(){ //fin; int n,cas = 1; while(~scanf("%d",&n)){ rep(i,1,n)scanf("%d",c+i); rep(i,1,n-1){ int u,v; scanf("%d%d",&u,&v); edge[u].pb(v); edge[v].pb(u); } ans = (LL)n * n * (n-1) / 2; dfs(1,0); rep(i,1,n){ int tmp = n - sum[i]; ans -= (LL)tmp * (tmp - 1) / 2; edge[i].clr;sum[i] = 0; } printf("Case #%d: %lld\n",cas++,ans); } return 0;}
阅读全文
0 0
- 【HDU6035】Colorful Tree(dfs,树形dp)
- HDU6035 Colorful Tree(树形dp)
- 【2017多校】HDU6035 Colorful Tree 【听说是树形DP】
- HDU6035-Colorful Tree 补集思想+树形DP
- hdu6035-树形dp-2017多校(2)&难-Colorful Tree
- 【dp】树形dp真好玩,hdu6035多校第一场的 colorful tree
- HDU6035 Colorful Tree
- HDU6035-Colorful Tree
- HDU6035-Colorful Tree
- hdu6035 Colorful Tree
- HDU6035 Colorful Tree
- HUD 6035 Colorful Tree dfs序||树形dp
- HDU 6035 Colorful Tree (树形DP,dfs)
- hdu6035树形dp
- HDU 6035 Colorful Tree [树形dp]
- hdu 6035 Colorful Tree(树形DP)
- HDU 6035 Colorful Tree (树形dp)
- HDU 6035 Colorful Tree(树形DP)
- Android-记账本(六)- ChatActivity
- git 服务器
- Maximum Subarray 动态规划 最大连续子序列和
- JDBC的介绍和数据库的连接
- Java Web入门学习(一) STS与Tomcat配置
- 【HDU6035】Colorful Tree(dfs,树形dp)
- 1.mini.MenuButton 菜单按钮。
- Nginx最大处理连接数
- curl安装——windows
- deque双向队列
- 题目12:喷水装置(二)
- leetcode 230. Kth Smallest Element in a BST
- Java8 之 Lambda
- C++调用构造函数