1. /*
  2. HDU 6035 - Colorful Tree [ DFS,分块 ]
  3. 题意:
  4. n个节点的树,每个节点有一种颜色(1~n),一条路径的权值是这条路上不同的颜色的数量,问所有路径(n*(n-1)/2条) 权值之和是多少?
  5. 分析:
  6. 考虑单种颜色,这种颜色的贡献是 至少经过一次这种颜色的路径数 = 总路径数(n*(n-1)/2) - 没有经过这种颜色的路径数
  7. 求没有经过这种颜色的路径数,即这种颜色的点将整棵树分块,每个分块中的总路径数
  8. */
  9. #include <bits/stdc++.h>
  10. using namespace std;
  11. #define LL long long
  12. const int N = 200005;
  13. struct Edge {
  14. int to, next;
  15. }edge[N<<1];
  16. int tot, head[N];
  17. void init() {
  18. memset(head, -1, sizeof(head));
  19. tot = 0;
  20. }
  21. void addedge(int u, int v) {
  22. edge[tot].to = v; edge[tot].next = head[u];
  23. head[u] = tot++;
  24. }
  25. int n;
  26. int c[N], last[N], rem[N], cut[N];
  27. LL ans;
  28. LL sum2(LL x) {
  29. return x*(x-1)/2;
  30. }
  31. int dfs(int u, int pre)
  32. {
  33. int su = 1, fa = last[c[u]];
  34. last[c[u]] = u;
  35. for (int i = head[u]; i != -1; i = edge[i].next)
  36. {
  37. int v = edge[i].to;
  38. if (v == pre) continue;
  39. cut[u] = 0;
  40. int sv = dfs(v, u);
  41. su += sv;
  42. ans -= sum2(sv-cut[u]);
  43. }
  44. (fa ? cut[fa] : rem[c[u]]) += su;
  45. last[c[u]] = fa;
  46. return su;
  47. }
  48. int main()
  49. {
  50. int tt = 0;
  51. while (~scanf("%d", &n))
  52. {
  53. init();
  54. for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
  55. for (int i = 1; i < n; i++)
  56. {
  57. int x, y; scanf("%d%d", &x, &y);
  58. addedge(x, y); addedge(y, x);
  59. }
  60. memset(last, 0, sizeof(last));
  61. memset(cut, 0, sizeof(cut));
  62. memset(rem, 0, sizeof(rem));
  63. ans = n*sum2(n);
  64. dfs(1, 1);
  65. for (int i = 1; i <= n; i++)
  66. ans -= sum2(n-rem[i]);
  67. printf("Case #%d: %lld\n", ++tt, ans);
  68. }
  69. }
  70. //----------------------------------------------------------------------
  71. #include <bits/stdc++.h>
  72. using namespace std;
  73. #define LL long long
  74. const int N = 200005;
  75. vector<int> c[N], G[N];
  76. int n;
  77. int L[N], R[N], s[N], f[N];
  78. void dfs(int u, int pre, int&& ncnt)
  79. {
  80. f[u] = pre;
  81. L[u] = ++ncnt;
  82. s[u] = 1;
  83. for (auto& v : G[u])
  84. {
  85. if (v == pre) continue;
  86. dfs(v, u, move(ncnt));
  87. s[u] += s[v];
  88. }
  89. R[u] = ncnt;
  90. }
  91. bool cmp(int a, int b) {
  92. return L[a] < L[b];
  93. }
  94. int main()
  95. {
  96. int tt = 0;
  97. while (~scanf("%d", &n))
  98. {
  99. for (int i = 0; i <= n; i++) c[i].clear(), G[i].clear();
  100. for (int i = 1; i <= n; i++)
  101. {
  102. int x; scanf("%d", &x);
  103. c[x].push_back(i);
  104. }
  105. for (int i = 1; i < n; i++)
  106. {
  107. int x, y; scanf("%d%d", &x, &y);
  108. G[x].push_back(y);
  109. G[y].push_back(x);
  110. }
  111. G[0].push_back(1);
  112. dfs(0, 0, 0);
  113. LL ans = (LL)n * n * (n-1)/2;
  114. for (int i = 1; i <= n; i++)
  115. {
  116. if (c[i].empty()) {
  117. ans -= (LL)n*(n-1)/2;
  118. continue;
  119. }
  120. c[i].push_back(0);
  121. sort(c[i].begin(), c[i].end(), cmp);
  122. for (auto& x : c[i])
  123. for (auto& y : G[x])
  124. {
  125. if (y == f[x]) continue;
  126. int size = s[y];
  127. int k = L[y];
  128. while (1)
  129. {
  130. L[n+1] = k;
  131. auto it = lower_bound(c[i].begin(), c[i].end(), n+1, cmp);
  132. if (it == c[i].end() || L[*it] > R[y]) break;
  133. size -= s[*it];
  134. k = R[*it]+1;
  135. }
  136. ans -= (LL)size * (size-1)/2;
  137. }
  138. }
  139. printf("Case #%d: %lld\n", ++tt, ans);
  140. }
  141. }

  

HDU 6035 - Colorful Tree | 2017 Multi-University Training Contest 1的更多相关文章

  1. hdu 6035:Colorful Tree (2017 多校第一场 1003) 【树形dp】

    题目链接 单独考虑每一种颜色,答案就是对于每种颜色至少经过一次这种的路径条数之和.反过来思考只需要求有多少条路径没有经过这种颜色即可. 具体实现过程比较复杂,很神奇的一个树形dp,下面给出一个含较详细 ...

  2. 2017 Multi-University Training Contest - Team 1 1003&&HDU 6035 Colorful Tree【树形dp】

    Colorful Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  3. HDU 6035 Colorful Tree(补集思想+树形DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6035 [题目大意] 给出一颗树,一条路径的价值为其上点权的种类数,求路径总价值 [题解] 单独考虑 ...

  4. HDU 6035 Colorful Tree (树形DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6035 [题目大意] 给出一颗树,一条路径的价值为其上点权的种类数,求路径总价值 [题解] 我们计算 ...

  5. 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)

    题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...

  6. HDU 6035 Colorful Tree(dfs)

    题意:一棵有n个点的树,树上每个点都有颜色c[i],定义每条路径的值为这条路径上经过的不同颜色数量和.求所有路径的值的和. 可以把问题转化为对每种颜色有多少条不同的路径至少经过这种颜色的点,然后加和. ...

  7. hdu 6035 Colorful Tree(虚树)

    考虑到树上操作:首先题目要我们求每条路径上出现不同颜色的数量,并把所有加起来得到答案:我们知道俩俩点之间会形成一条路径,所以我们可以知道每个样例的总的路径的数目为:n*(n-1)/2: 这样单单的求, ...

  8. HDU 6170 - Two strings | 2017 ZJUT Multi-University Training 9

    /* HDU 6170 - Two strings [ DP ] | 2017 ZJUT Multi-University Training 9 题意: 定义*可以匹配任意长度,.可以匹配任意字符,问 ...

  9. hdu 6301 Distinct Values (2018 Multi-University Training Contest 1 1004)

    Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

随机推荐

  1. [转帖]docker容器保持运行不退出

    docker容器保持运行不退出 2019年01月20日 23:21:22 chvalrous 阅读数 1511   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...

  2. Javascript - BOM 对象

    浏览器相关的对象.获取浏览器相关的信息,可以设置和修改浏览器属性. 0. web-app 版 TodoList 小程序 用以下内容可以自己手写一个 TodoList 小程序,再添加几行代码就可以用手机 ...

  3. TUM 慕尼黑工业大学 MSEI 课程结构介绍 ws19/20

    本文内容 根据德文 tum 官网介绍:https://www.ei.tum.de/studium/master-ei-msei/ 翻译,提取并且翻译成中文信息. 本文适用于ws19/20届的学生. 概 ...

  4. php面试相关整理

    1.HTTP Keep-Alive的作用 作用:Keep-Alive:使客户端到服务器端的连接持续有效,当出现对服务器的后继请求时,Keep-Alive功能避免了建立或者重新建立连接.Web服务器,基 ...

  5. PHPstorm配置同步服务器文件

    一.配置服务器 1.连接配置 打开菜单栏 Tools -> Deployment -> Configuration 点击 + 选择 SFTP,并填写相关服务器信息: Type:连接类型,这 ...

  6. linux 下用find命令查找文件,rm命令删除文件

    linux 下用find命令查找文件,rm命令删除文件. 删除指定目录下指定文件find 要查找的目录名 -name .svn |xargs rm -rf 删除指定名称的文件或文件夹: find -t ...

  7. HashMap—— values() remove方法 containsKey()方法 containsValue()方法

    values()方法:看下面的实例,就是把所有的value值封装成一个connection型的数组 Map<Integer,Student> students=new HashMap< ...

  8. C#中word文档转html

    var path = Request.Url.Host + ":" + Request.Url.Port + list[i].AnnexPath; //html保存路径 strin ...

  9. 题解luoguP2054 BZOJ1965【[AHOI2005]洗牌】

    题目链接: https://www.luogu.org/problemnew/show/P2054 https://www.lydsy.com/JudgeOnline/problem.php?id=1 ...

  10. STM32F10xxx_异常与中断

    STM32F10xxx_异常与中断 [TOC] 更新记录 version status description date author V1.0 C Create Document 2018.10.2 ...