Codeforces Bubble Cup 8 - Finals [Online Mirror] B. Bribes lca
题目链接:
http://codeforces.com/contest/575/problem/B
题解:
把链u,v拆成u,lca(u,v)和v,lca(u,v)(v,lca(u,v)是倒过来的)。这样就只要考虑自下而上的线性结构了,可以用前缀和的思想来做成段更新。
代码:
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; const int maxn = 1e5 + ;
const int DEG = ;
const int mod = 1e9 + ;
typedef __int64 LL; struct Edge {
int v, type;
Edge() {}
Edge(int v, int type) :v(v), type(type) {}
}; int n,q;
vector<Edge> egs;
vector<int> G[maxn]; int dep[maxn], anc[maxn][DEG],up[maxn];
void dfs(int u,int fa) {
dep[u] = dep[fa] + ;
anc[u][] = fa;
for (int i = ; i < DEG; i++) {
int f = anc[u][i - ];
anc[u][i] = anc[f][i - ];
}
for (int i = ; i < G[u].size(); i++) {
Edge& e = egs[G[u][i]];
if (e.v == fa) continue;
up[e.v] = -e.type;
dfs(e.v, u);
}
} int cnt[][maxn];
int Lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int i = DEG - ; i >= ; i--) {
if (dep[anc[u][i]]>=dep[v]) {
u = anc[u][i];
}
}
if (u == v) return u;
for (int i = DEG - ; i >= ; i--) {
if (anc[u][i] != anc[v][i]) {
u = anc[u][i];
v = anc[v][i];
}
}
//printf("u:%d,v:%d\n", u, v);
return anc[u][];
} void dfs2(int u, int fa) {
for (int i = ; i < G[u].size(); i++) {
Edge& e = egs[G[u][i]];
if (e.v == fa) continue;
dfs2(e.v, u);
cnt[][u] += cnt[][e.v];
cnt[][u] += cnt[][e.v];
}
} void addEdge(int u, int v, int type) {
egs.push_back(Edge(v, type));
G[u].push_back(egs.size() - );
} LL fast_pow(int n) {
LL ret = ,x=;
while (n) {
if (n & ) ret *= x, ret %= mod;
x *= x, x %= mod;
n /= ;
}
return ret;
} void init() {
for (int i = ; i < maxn; i++) G[i].clear();
egs.clear();
memset(anc, , sizeof(anc));
memset(dep, , sizeof(dep));
memset(up, , sizeof(up));
memset(cnt, , sizeof(cnt));
} int main() {
scanf("%d", &n);
init();
for (int i = ; i < n - ; i++) {
int u, v, type;
scanf("%d%d%d", &u, &v, &type);
if (type == ) {
addEdge(u, v, );
addEdge(v, u, );
}
else {
addEdge(u, v, );
addEdge(v, u, -);
}
}
dfs(, );
//for (int i = 1; i <= n; i++) {
// printf("%d:(%d)",i,dep[i]);
// for (int j = 0; j < 20; j++) {
// printf("%d ", anc[i][j]);
// }
// printf("\n");
//}
scanf("%d", &q);
int s = , t;
while (q--) {
//scanf("%d%d", &s, &t);
//printf("(%d,%d):%d\n", s, t, Lca(s, t));
scanf("%d", &t);
int lca = Lca(s, t);
//printf("(%d,%d):%d\n", s, t, lca);
cnt[][s]++;
cnt[][lca]--;
cnt[][t]++;
cnt[][lca]--;
s = t;
}
dfs2(, );
LL ans = ;
for (int i = ; i <= n; i++) {
if (up[i] == -) {
ans += fast_pow(cnt[][i]) - ;
ans = (ans + mod) % mod;
}
else if (up[i] == ) {
ans += fast_pow(cnt[][i]) - ;
ans = (ans + mod) % mod;
}
}
printf("%I64d\n", ans);
return ;
}
/*
7
1 2 0
1 3 0
2 4 0
2 5 0
3 6 0
3 7 0 */
Codeforces Bubble Cup 8 - Finals [Online Mirror] B. Bribes lca的更多相关文章
- Codeforces Bubble Cup 8 - Finals [Online Mirror]H. Bots 数学
H. Bots Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/575/problem/H Desc ...
- Codeforces Bubble Cup 8 - Finals [Online Mirror] D. Tablecity 数学题
D. Tablecity Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/575/problem/D ...
- Codeforces Bubble Cup 8 - Finals [Online Mirror] F. Bulbo DP
F. Bulbo Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/575/problem/F Des ...
- Bubble Cup 12 - Finals Online Mirror, unrated, Div. 1
Bubble Cup 12 - Finals Online Mirror, unrated, Div. 1 C. Jumping Transformers 我会状压 DP! 用 \(dp[x][y][ ...
- Bubble Cup 11 - Finals [Online Mirror, Div. 1]题解 【待补】
Bubble Cup 11 - Finals [Online Mirror, Div. 1] 一场很好玩的题啊! I. Palindrome Pairs 枚举哪种字符出现奇数次. G. AI robo ...
- 【简单dfs】Bubble Cup 14 - Finals Online Mirror (Unrated, ICPC Rules, Teams Preferred, Div. 2), problem: (J) Robot Factory,
传送门 Problem - 1600J - Codeforces 题目 题意 给定n行m列, 求每个连通块由多少格子组成,并将格子数从大到小排序输出 对于每个格子都有一个数(0~15),将其转化 ...
- Bubble Cup X - Finals [Online Mirror]
来自FallDream的博客,未经允许,请勿转载,谢谢. 组了个菜鸡队打cf上的ACM比赛 比较快做完了8题但是菜的抠脚罚时巨多,所以最后被顶到了19名(居然没出首页) 自己的号自从上次疯狂掉分就没动 ...
- Bubble Cup X - Finals [Online Mirror] B. Neural Network country 矩阵快速幂加速转移
B. Neural Network country time limit per test 2 seconds memory limit per test 256 megabytes Due to t ...
- Bubble Cup 12 - Finals [Online Mirror, unrated, Div. 1] E. Product Tuples
题意略,题解生成函数练习题,1+(q-ai)x卷积即可,线段树优化(类似分治思想) //#pragma GCC optimize(2) //#pragma GCC optimize(3) //#pra ...
随机推荐
- DWZ LookUp Suggest 教程
单个查找带回 jsp 代码 lookup.jsp <%@ page language="java" contentType="text/html; charset= ...
- 行列转换 pivot
select * from ( select isnull(c.type,'其他') type,d from ( select ID,Record_code,code,day(thedate) d f ...
- PHP static静态局部变量和静态全局变量总结
1.不会随着函数的调用和退出而发生变化,不过,尽管该变量还继续存在,但不能使用它.倘若再次调用定义它的函数时,它又可继续使用,而且保存了前次被调用后留下的值 2.静态局部变量只会初始化一次 3.静态属 ...
- SQL语句新建表,同时添加主键、索引、约束
SQL语句新建数据表 主键,索引,约束 CREATE TABLE [dbo].[T_SendInsideMessageRec]( [SendInsideMID] [uniqueidentifier ...
- 【Unity3D实战】摇摆直升机开发实战(一)
[Unity3D实战]摇摆直升机开发实战(一) 1.点击[Assets],创建[Sprites]和[Resources]文件夹,然后将所需要的素材导入[Sprites]文件夹中. 2.找到[Sprit ...
- 利用Nutch和Tomcat构建搜索引擎
利用Nutch和Tomcat构建搜索引擎 1.安装环境及软件版本介绍 本教程是在Linux Ubuntu 12.04 desktop i386操作系统上搭建,结合使用了Nutch-1.2和Apache ...
- python——类
类的定义格式: class CLASSNAME(父类1,父类2,父类3): __A = 0 ##私有属性,两个_开头,只有在该类中的函数才能访问 B = 0 ##公共属性 def 函数名(s ...
- float,double和decimal的精度问题
先标注一个音标,因为我老是读错:decimal ['desɪml] 精度对比: 类型 CTS 类型 描述 有效数字 范围 float System.Single 32-bit single-preci ...
- boost:program_options
由于系统库getopt和getopt_long用起来不够直观,仔细看了下boost发现Boost.Program_options可以满足我的需求,它和getopt系列函数一样,可以抓起命令行参数,这里 ...
- js实现模拟自动点击按钮,并且在10秒倒计时之后疯狂点击
需求来自于csdn问答,可以利用这个原理做秒杀抢单外挂. 代码示例如下: <html> <head> <meta charset="utf-8"/&g ...