Codeforces Round #326 (Div. 1) - C. Duff in the Army 树上倍增算法
题意:一个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个值, 每次合并就行了。
- #include <bits/stdc++.h>
- using namespace std;
- const int maxn = 1e5 + ;
- const int maxdep = ;
- int par[maxdep][maxn], dep[maxn], n;
- vector <int> ID[maxdep][maxn], cit[maxn];
- vector <int> G[maxn];
- void init() {
- for (int i = ; i < maxn; i++) {
- G[i].clear();
- cit[i].clear();
- for (int j = ; j < maxdep; j++) {
- ID[j][i].clear();
- }
- }
- memset(par, -, sizeof (par));
- }
- void update(vector <int> &v1, vector <int> &v2) {
- for (int x: v2) {
- v1.push_back(x);
- }
- sort (v1.begin(), v1.end());
- v1.erase(unique(v1.begin(), v1.end()), v1.end());
- while (v1.size() > ) {
- v1.pop_back();
- }
- }
- void dfs(int u, int father) {
- par[][u] = father;
- dep[u] = dep[father] + ;
- for (int i = u; i <= u; i++) {
- update(ID[][i], cit[par[][i]]);
- update(ID[][i], cit[i]);
- for (int j = ; j + < maxdep; j++) {
- if (~par[j][i]) {
- par[j+][i] = par[j][par[j][i]];
- update(ID[j+][i], cit[i]);
- update(ID[j+][i], ID[j][i]);
- update(ID[j+][i], ID[j][par[j][i]]);
- } else {
- par[j+][i] = -;
- }
- }
- }
- for (int v: G[u]) {
- if (v != father) {
- dfs(v, u);
- }
- }
- }
- int lca(int u, int v) {
- if (dep[u] > dep[v]) {
- swap(u, v);
- }
- for (int k = ; k < maxdep; k++) {
- if ((dep[v] - dep[u]) >> k & ) {
- v = par[k][v];
- }
- }
- if (u == v) {
- return u;
- }
- for (int i = maxdep-; i >= ; i--) {
- if (par[i][u] != par[i][v]) {
- u = par[i][u];
- v = par[i][v];
- }
- }
- return par[][u];
- }
- int anc;
- vector <int> solve(int u, int v) {
- vector <int> res;
- if (u == v) {
- update(res, cit[u]);
- }
- for (int i = maxdep-; i >= ; i--) {
- if (~par[i][u] && dep[par[i][u]] >= dep[anc]) {
- update(res, ID[i][u]);
- u = par[i][u];
- }
- }
- for (int i = maxdep-; i >= ; i--) {
- if (~par[i][v] && dep[par[i][v]] >= dep[anc]) {
- update(res, ID[i][v]);
- v = par[i][v];
- }
- }
- vector<int> emp;
- update(res, emp);
- return res;
- }
- int main() {
- #ifndef ONLINE_JUDGE
- freopen("in.txt","r",stdin);
- #endif
- int m, q;
- while (~scanf ("%d%d%d", &n, &m, &q)) {
- init();
- for (int i = ; i < n-; i++) {
- int u, v;
- scanf ("%d%d", &u, &v);
- G[u].push_back(v);
- G[v].push_back(u);
- }
- for (int i = ; i < m; i++) {
- int c;
- scanf ("%d", &c);
- if (cit[c].size() < ) {
- cit[c].push_back(i+);
- ID[][c].push_back(i+);
- }
- }
- dfs(, );
- while(q--) {
- int u, v, a;
- scanf ("%d%d%d", &u, &v, &a);
- anc = lca(u, v);
- auto res = solve(u, v);
- int tot = min((int)res.size(), a);
- printf("%d%c", tot, " \n"[!tot]);
- for (int i = ; i < min((int)res.size(), a); i++) {
- printf("%d%c", res[i], " \n"[i+==min((int)res.size(), a)]);
- }
- }
- }
- return ;
- }
Codeforces Round #326 (Div. 1) - C. Duff in the Army 树上倍增算法的更多相关文章
- Codeforces Round #326 Div.1 C.Duff in the Army 树上倍增
题意概述: 给出一棵N个结点的树,然后有M个居民分散在这棵树的结点上(允许某个结点没有居民).现在给出一些询问形如u,v,a,定义k=min(x,a),其中x表示的是u->v路径上的居民数量.将 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【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 ...
- 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 ...
随机推荐
- HTML5 FileReader读取Blob对象API详解
使用FileReader对象,web应用程序可以异步的读取存储在用户计算机上的文件(或者原始数据缓冲)内容,可以使用File对象或者Blob对象来指定所要读取的文件或数据.其中File对象可以是来自用 ...
- Android之提交数据到服务端方法简单封装
在Android应用中,除了单机版的应用,其余的应用免不了需要频繁地与服务端进行数据交互,如果每一种方法都独立写一段代码,那会造成代码大量重复,冗余,这不是我们所希望的,所以我们可以对其进行一些封装, ...
- cas sso单点登录系列8_抛弃Https让Cas以Http协议提供单点登录服务
转:http://blog.csdn.net/ycyk_168/article/details/18668951 本文环境: 1.apache-tomcat-7.0.50-windows-x86 2. ...
- 利用CMake自己创建OpenCV静态链接库
1.准备工作: 1)完成Visual Studio2012安装: 2)下载并解压CMake3.5.0: 3)下载并解压OpenCV2.4.12: 4)下载并解压TBB44_20160128oss. 2 ...
- bootstrap学习--什么是bootstrap
2011年,twitter的“一小撮”工程师为了提高他们内部的分析和管理能力,用业余时间为他们的产品构建了一套易用.优雅.灵活.可扩展的前端工具集--BootStrap.Bootstrap由MARK ...
- windows2008 R2 安装wampserver
1. 在官网http://www.wampserver.com/下载,wampserver2.5; 2. 安装时候会缺少msvcr110.dll文件,所以先要安装这个文件: 3. 先从微软下载Visu ...
- java-web-j2e学习建议路线
JAVA学习之路(2) 首先要明白Java体系设计到得三个方面:J2SE,J2EE,J2ME(KJAVA).J2SE,Java 2 Platform Standard Edition,我们经常说 ...
- 使用Echarts的五个步骤
_liuz 2015-07-22 09:35:53 参考网址:http://echarts.baidu.com/doc/start.html 一.制作一个图表容器<div id="m ...
- ios 排序汇总
ios 排序汇总 IOS几种简单有效的数组排序方法 //第一种,利用数组的sortedArrayUsingComparator调用 NSComparator ,obj1和obj2指的数组中的对象 N ...
- Unity各平台路径总结
路径是Unity开发中令人头疼的一个问题,根据我的开发经验,现将开发中遇到的路径问题总结如下: 1. 如何读取Application.streamingAssetsPath下的文件? Edit.iOS ...