题目链接:

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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][ ...

  5. Bubble Cup 11 - Finals [Online Mirror, Div. 1]题解 【待补】

    Bubble Cup 11 - Finals [Online Mirror, Div. 1] 一场很好玩的题啊! I. Palindrome Pairs 枚举哪种字符出现奇数次. G. AI robo ...

  6. 【简单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),将其转化 ...

  7. Bubble Cup X - Finals [Online Mirror]

    来自FallDream的博客,未经允许,请勿转载,谢谢. 组了个菜鸡队打cf上的ACM比赛 比较快做完了8题但是菜的抠脚罚时巨多,所以最后被顶到了19名(居然没出首页) 自己的号自从上次疯狂掉分就没动 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. ServiceStack简介

    原文:http://bbs.csdn.net/topics/390911450?page=1#post-398388262 在帖子中看到,大致了解了下,非常好的框架,留着学习 https://gith ...

  2. OpenGl从零开始之坐标变换

    http://www.tuicool.com/articles/uiayYrI OpenGL学习脚印: 坐标变换过程(vertex transformation) http://blog.csdn.n ...

  3. Cisco基本命令配置

    实验一 路由器的基本命令操作 1 实验目标 ü 熟悉路由器的命令行操作 ü 能够使用命令行帮助 ü 能够查看路由器接口信息 ü 能够产看路由器配置信息 ü 能够配置以太网接口 ü 能够配置广域网接口 ...

  4. ios固定高度禁止惯性滚动

    最近测试pad改H5的项目时,固定高度的div,超出部分滚动,但在ios下滑动特别卡顿,安卓上没问题.搜索找到解决办法 固定高度的div设置超出页面滚动,ios会出现卡顿,非常不爽.通过下面css就可 ...

  5. 《HTML5与CSS3基础教程》学习笔记 ——Four Day

    第十六章 1.    输入和元素 电子邮件框 <input type="email"> 搜索框 <input type="search"> ...

  6. 【Qt】Qt环境搭建(Qt Creator)【转】

    简述 上一节中介绍了如何进行Qt和Visual Studio的下载安装,随后演示了如何将Qt集成到Visual Studio中,并完成了我们第一个Qt小程序-Hello World.下面主要讲解如何利 ...

  7. JS重写alert,保证弹窗错误的友好性

    // ------------------------------------------------------------- // 重写alert,保证弹窗错误的友好性 var j_oldAler ...

  8. 注意:php5.4删除了session_unregister函数

    在php5.4版本中,应经删除了session_unregister该函数.朋友们注意一下 前几天安装了dedecms系统,当在后台安全退出的时候,后台出现空白,先前只分析其他功能去了,也没太注意安全 ...

  9. Oracle 表的连接方式(2)-----HASH JOIN的基本机制1

    我们对hash join的常见误解,一般包括两个: 第一个误解:是我们经常以为hash join需要对两个做join的表都做全表扫描 第二个误解:是经常以为hash join会选择比较小的表做buil ...

  10. [转]init.d解析

    本文为转载,放在这里以备忘. init.d指的是/etc/rc.d/init.d目录.本文包括3部分内容1. Linux的引导过程2. 运行级别3. /etc/rc.d/ 与/etc/rc.d/ini ...