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 ...
随机推荐
- Kettle 连接 oracle
问题1:加载不了驱动 Kettle 不包含jdbc的驱程 所以需要把Ojdbc6.jar 放到 Kettle目录下 4.X: \libext\JDBC 5.x: \lib 问题2:没打开监听 症状 ...
- cocos2dx-lua之断点调试支持
cocos2dx 3.2版对cocos code ide支持已经相当棒了,不过话说,编辑器用起来感觉没有sublime顺手 支持cocos code ide已经支持创建lua项目了,可是默认创建的项目 ...
- EXT中的iconCls 图标加载
刚刚遇到了个奇怪的问题. 我用 在主页面用TAB autoLoad:{url:link, nocache: true, scripts:true} 加载页面Student.jsp, 郁闷,FF可以正常 ...
- 8款唯美设计的HTML5/CSS3应用
1.CSS3漂亮的自定义Checkbox复选框 9款迷人样式 今天我们来分享一款9款样式迷人的CSS3漂亮的自定义Checkbox复选框.这几款复选框样式很丰富,使用起来也比较方便. 在线演示 源码下 ...
- corosync+pacemaker实现高可用(HA)集群
corosync+pacemaker实现高可用(HA)集群(一) 重要概念 在准备部署HA集群前,需要对其涉及的大量的概念有一个初步的了解,这样在实际部署配置时,才不至于不知所云 资源.服务与 ...
- Linux时间相关函数
相关文件: /etc/localtime 本地时间二级制文件 /etc/sysconfig/clock 时区配置文件 /usr/share/zoneinfo 存储各个时区的二进制文件 时间修改方 ...
- 【Qt】Qt之自定义界面(实现无边框、可移动)【转】
简述 UI设计是指对软件的人机交互.操作逻辑.界面美观的整体设计.好的UI设计不仅是让软件变得有个性.有品位,还要让软件的操作变得舒适简单.自由,充分体现软件的定位和特点. 爱美之心人皆有之.其实软件 ...
- php 文件上传简单类---限制仅上传jpg文件
php 文件上传代码,限制只能上传jpg格式文件,也可以自行添加其它扩展名的文件. <?php /* * 图片上传类 仅限JPG格式图片 * edit by www.jbxue.com at 2 ...
- rsync参数详解、利用ssh、rsync 实现数据的定时同步
rsync 简介 rsync(remote synchronize)是一个远程数据同步工具,可通过 LAN/WAN 快速同步多台主机之间的文 件.也可以使用 rsync 同步本
- 每日一“酷”之string
介绍:string模块可以追溯到最早的Python版本中.现在很多的被移植为str和unicode对象的方法,在python3.0中会被完全去除.string模块中,有很多有用的常量和累,用来处理st ...