题目链接:

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. C# 序列化(二)二进制序列化的案例

    这篇是针对上一篇讲序列化的文章的一个实际案例,WinForm程序的主界面如下:

  2. Professional iOS Network Programming Connecting the Enterprise to the iPhone and iPad

    Book Description Learn to develop iPhone and iPad applications for networked enterprise environments ...

  3. NSString字符操作

    1.常用创建初始化方法 1.NSString *string0 = @"string"; 2.NSString *string1 = [NSString stringWithFor ...

  4. UI1_UITableViewSearchController

    // UI1_UITableViewSearchController // // Created by zhangxueming on 15/7/15. // Copyright (c) 2015年 ...

  5. WebServiceException

    在用cxf做webservice的时候,在写客户端程序的时候,出现以下异常: Could not find wsdl:binding operation info for web method tes ...

  6. 洛谷 P1195 口袋的天空

    题目背景 小杉坐在教室里,透过口袋一样的窗户看口袋一样的天空. 有很多云飘在那里,看起来很漂亮,小杉想摘下那样美的几朵云,做成棉花糖. 题目描述 给你云朵的个数N,再给你M个关系,表示哪些云朵可以连在 ...

  7. java.util.Vector

    public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, C ...

  8. oc常见误区

    1.同步请求可以从因特网请求数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据完成,才可以进行下一步操作, 2.异步请求不会阻塞主线程,而会建立一个新的线程来操作,用户发出异步请求后,依然 ...

  9. Android模拟器中安装APK文件(转)

    1.平台环境:Win7系统, 安装Eclipse,android4.0(sdk) 2.随便创建个工程(HelloWorld),结果如下: 3.运行(Run HelloWorld),启动模拟器,如下所示 ...

  10. monkey 测试 adb shell monkey

    adb shell monkey -p com.android.recorder --throttle 360 --ignore-crashes --monitor-native-crashes -- ...