题意:一个n个点的数, m个人住在其中的某些点上, 每个人的标号1-m, 询问u-v 路径上标号前a个人,并输出标号,a < 10。

作法, 利用倍增, ID[j][i] 表示i到i的第2^j个祖先上前10个人, 那么每次询问直接维护就好了,细节好多, 刚开始不知道怎么求ID[j][i]。

这里把2^j分成两部分, 前2^(j-1)和 后2^(j-1)个, 然后递推的维护。

感觉树链剖分也可以做, 不知道会不会TLE, 树链剖分的话 线段树的每个点维护10个值, 每次合并就行了。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 1e5 + ;
  4. const int maxdep = ;
  5. int par[maxdep][maxn], dep[maxn], n;
  6. vector <int> ID[maxdep][maxn], cit[maxn];
  7. vector <int> G[maxn];
  8. void init() {
  9. for (int i = ; i < maxn; i++) {
  10. G[i].clear();
  11. cit[i].clear();
  12. for (int j = ; j < maxdep; j++) {
  13. ID[j][i].clear();
  14. }
  15. }
  16. memset(par, -, sizeof (par));
  17. }
  18. void update(vector <int> &v1, vector <int> &v2) {
  19. for (int x: v2) {
  20. v1.push_back(x);
  21. }
  22. sort (v1.begin(), v1.end());
  23. v1.erase(unique(v1.begin(), v1.end()), v1.end());
  24. while (v1.size() > ) {
  25. v1.pop_back();
  26. }
  27. }
  28. void dfs(int u, int father) {
  29. par[][u] = father;
  30. dep[u] = dep[father] + ;
  31. for (int i = u; i <= u; i++) {
  32. update(ID[][i], cit[par[][i]]);
  33. update(ID[][i], cit[i]);
  34. for (int j = ; j + < maxdep; j++) {
  35. if (~par[j][i]) {
  36. par[j+][i] = par[j][par[j][i]];
  37. update(ID[j+][i], cit[i]);
  38. update(ID[j+][i], ID[j][i]);
  39. update(ID[j+][i], ID[j][par[j][i]]);
  40. } else {
  41. par[j+][i] = -;
  42. }
  43. }
  44. }
  45. for (int v: G[u]) {
  46. if (v != father) {
  47. dfs(v, u);
  48. }
  49. }
  50.  
  51. }
  52. int lca(int u, int v) {
  53. if (dep[u] > dep[v]) {
  54. swap(u, v);
  55. }
  56. for (int k = ; k < maxdep; k++) {
  57. if ((dep[v] - dep[u]) >> k & ) {
  58. v = par[k][v];
  59. }
  60. }
  61. if (u == v) {
  62. return u;
  63. }
  64. for (int i = maxdep-; i >= ; i--) {
  65. if (par[i][u] != par[i][v]) {
  66. u = par[i][u];
  67. v = par[i][v];
  68. }
  69. }
  70. return par[][u];
  71. }
  72. int anc;
  73. vector <int> solve(int u, int v) {
  74. vector <int> res;
  75. if (u == v) {
  76. update(res, cit[u]);
  77. }
  78. for (int i = maxdep-; i >= ; i--) {
  79. if (~par[i][u] && dep[par[i][u]] >= dep[anc]) {
  80. update(res, ID[i][u]);
  81. u = par[i][u];
  82. }
  83. }
  84. for (int i = maxdep-; i >= ; i--) {
  85. if (~par[i][v] && dep[par[i][v]] >= dep[anc]) {
  86. update(res, ID[i][v]);
  87. v = par[i][v];
  88. }
  89. }
  90. vector<int> emp;
  91. update(res, emp);
  92. return res;
  93. }
  94. int main() {
  95. #ifndef ONLINE_JUDGE
  96. freopen("in.txt","r",stdin);
  97. #endif
  98. int m, q;
  99. while (~scanf ("%d%d%d", &n, &m, &q)) {
  100. init();
  101. for (int i = ; i < n-; i++) {
  102. int u, v;
  103. scanf ("%d%d", &u, &v);
  104. G[u].push_back(v);
  105. G[v].push_back(u);
  106. }
  107. for (int i = ; i < m; i++) {
  108. int c;
  109. scanf ("%d", &c);
  110. if (cit[c].size() < ) {
  111. cit[c].push_back(i+);
  112. ID[][c].push_back(i+);
  113. }
  114. }
  115. dfs(, );
  116. while(q--) {
  117. int u, v, a;
  118. scanf ("%d%d%d", &u, &v, &a);
  119. anc = lca(u, v);
  120. auto res = solve(u, v);
  121. int tot = min((int)res.size(), a);
  122. printf("%d%c", tot, " \n"[!tot]);
  123. for (int i = ; i < min((int)res.size(), a); i++) {
  124. printf("%d%c", res[i], " \n"[i+==min((int)res.size(), a)]);
  125. }
  126. }
  127. }
  128. return ;
  129. }

Codeforces Round #326 (Div. 1) - C. Duff in the Army 树上倍增算法的更多相关文章

  1. Codeforces Round #326 Div.1 C.Duff in the Army 树上倍增

    题意概述: 给出一棵N个结点的树,然后有M个居民分散在这棵树的结点上(允许某个结点没有居民).现在给出一些询问形如u,v,a,定义k=min(x,a),其中x表示的是u->v路径上的居民数量.将 ...

  2. Codeforces Round #326 (Div. 2) D. Duff in Beach dp

    D. Duff in Beach Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/588/probl ...

  3. Codeforces Round #326 (Div. 2) C. Duff and Weight Lifting 水题

    C. Duff and Weight Lifting Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  4. Codeforces Round #326 (Div. 2) B. Duff in Love 分解质因数

    B. Duff in Love Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/588/proble ...

  5. Codeforces Round #326 (Div. 2) A. Duff and Meat 水题

    A. Duff and Meat Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/588/probl ...

  6. Codeforces Round #326 (Div. 2) B Duff in Love 简单数论 姿势涨

    B. Duff in Love time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  7. Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想

    题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...

  8. 【LCA】CodeForce #326 Div.2 E:Duff in the Army

    C. Duff in the Army Recently Duff has been a soldier in the army. Malek is her commander. Their coun ...

  9. Codeforces Round #326 (Div. 2) B. Pasha and Phone C. Duff and Weight Lifting

    B. Pasha and PhonePasha has recently bought a new phone jPager and started adding his friends' phone ...

随机推荐

  1. HTML5 FileReader读取Blob对象API详解

    使用FileReader对象,web应用程序可以异步的读取存储在用户计算机上的文件(或者原始数据缓冲)内容,可以使用File对象或者Blob对象来指定所要读取的文件或数据.其中File对象可以是来自用 ...

  2. Android之提交数据到服务端方法简单封装

    在Android应用中,除了单机版的应用,其余的应用免不了需要频繁地与服务端进行数据交互,如果每一种方法都独立写一段代码,那会造成代码大量重复,冗余,这不是我们所希望的,所以我们可以对其进行一些封装, ...

  3. cas sso单点登录系列8_抛弃Https让Cas以Http协议提供单点登录服务

    转:http://blog.csdn.net/ycyk_168/article/details/18668951 本文环境: 1.apache-tomcat-7.0.50-windows-x86 2. ...

  4. 利用CMake自己创建OpenCV静态链接库

    1.准备工作: 1)完成Visual Studio2012安装: 2)下载并解压CMake3.5.0: 3)下载并解压OpenCV2.4.12: 4)下载并解压TBB44_20160128oss. 2 ...

  5. bootstrap学习--什么是bootstrap

    2011年,twitter的“一小撮”工程师为了提高他们内部的分析和管理能力,用业余时间为他们的产品构建了一套易用.优雅.灵活.可扩展的前端工具集--BootStrap.Bootstrap由MARK ...

  6. windows2008 R2 安装wampserver

    1. 在官网http://www.wampserver.com/下载,wampserver2.5; 2. 安装时候会缺少msvcr110.dll文件,所以先要安装这个文件: 3. 先从微软下载Visu ...

  7. java-web-j2e学习建议路线

      JAVA学习之路(2)  首先要明白Java体系设计到得三个方面:J2SE,J2EE,J2ME(KJAVA).J2SE,Java 2 Platform Standard Edition,我们经常说 ...

  8. 使用Echarts的五个步骤

     _liuz 2015-07-22 09:35:53 参考网址:http://echarts.baidu.com/doc/start.html 一.制作一个图表容器<div id="m ...

  9. ios 排序汇总

    ios 排序汇总  IOS几种简单有效的数组排序方法 //第一种,利用数组的sortedArrayUsingComparator调用 NSComparator ,obj1和obj2指的数组中的对象 N ...

  10. Unity各平台路径总结

    路径是Unity开发中令人头疼的一个问题,根据我的开发经验,现将开发中遇到的路径问题总结如下: 1. 如何读取Application.streamingAssetsPath下的文件? Edit.iOS ...