题目链接:

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. Part 2 How are the URL's mapped to Controller Action Methods?

    Part 2 How are the URL's mapped to Controller Action Methods? The answer is ASP.NET Routing.Notice t ...

  2. SQL里IN的用法以及优化

    1.in后条件不多,可以考虑主表建索引,或用union all 代替 2. in 和 exists的区别: 如果子查询得出的结果集记录较少,主查询中的表较大且又有索引时应该用in, 反之如果外层的主查 ...

  3. jQuer中 height scrollTop

    jQuery(window).height()代表了当前可见区域的大小,而jQuery(document).height()则代表了整个文档的高度,可视具体情况使用. 注意当浏览器窗口大小改变时(如最 ...

  4. JQuery之append和appendTo的区别,还有js中的appendChild用法

    JQuery之append和appendTo的区别 append()前面是要选择的对象,后面是要在对象内插入的元素内容 appendTo()前面是要插入的元素内容且为Jquery对象,而后面是要选择的 ...

  5. 【学习笔记】【C语言】第一个C程序

    1.新建C语言程序 *打开Xcode  *新建文件  *选择C语言程序  *输入程序名称  *选择存放路径  *创建完成后  2.也可以用终端来创建  通过指令来创建.c文件   3.编写代码  4. ...

  6. Mongo客户端MongoVUE的基本使用

    这里没有涉及到服务器以及客户端的安装,文章主要介绍mongo客户端mongoVUE的使用 一.数据库连接 点击绿色加号添加一个连接,输入name.server.port,点击save,点击connec ...

  7. 12.iscsi-target

    server: 环境:rhel7.2  软件包:targetcli-2.1.fb41-3.el7.noarch,selinux-policy-targeted-3.13.1-60.el7.noarch ...

  8. Hibernate中的对象状态,及自动更新原因

    Hibernate的对象有三种状态,分别为:瞬时状态 (Transient). 持久化状态(Persistent).游离状态(Detached).对它的深入理解,才能更好的理解hibernate的运行 ...

  9. 万能的SQLHelper帮助类

    /// <summary> /// 数据库帮助类 /// </summary> public class SQLHelper { private static string c ...

  10. ERP基本功——物料的四个量

    ERP基本功——物料的四个量 在分析制造业管理问题的时候,如果能借用一些ERP里才会用到的概念,就会非常的简单,并且条理清晰. 我觉得柳中冈先生一再强调的一个观点非常有用,大意是这样的:任何人依据现有 ...