[CF575B]Bribes

题目大意:

一棵\(n(n\le10^5)\)个结点的树,有些边有方向,对于每条边,如果第\(i\)次逆向走过这条边,就会产生\(2^{i-1}\)的代价。开始在\(1\)号点,依次经过给出的\(m(m\le10^6)\)个点,求总代价最小值。

思路:

维护树上差分即可。

源代码:

#include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=1e5+1,mod=1e9+7;
inline int power(int a,int k) {
int ret=1;
for(;k;k>>=1) {
if(k&1) ret=1ll*ret*a%mod;
a=1ll*a*a%mod;
}
return ret;
}
std::vector<int> e[N];
inline void add_edge(const int &u,const int &v) {
e[u].push_back(v);
e[v].push_back(u);
}
struct Edge {
int u,v;
bool t;
};
Edge edge[N];
int dep[N],par[N],son[N],size[N],top[N],d[2][N];
void dfs(const int &x,const int &par) {
size[x]=1;
::par[x]=par;
dep[x]=dep[par]+1;
for(unsigned i=0;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==par) continue;
dfs(y,x);
size[x]+=size[y];
if(size[y]>size[son[x]]) {
son[x]=y;
}
}
}
void dfs(const int &x) {
top[x]=x==son[par[x]]?top[par[x]]:x;
if(son[x]) dfs(son[x]);
for(unsigned i=0;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==par[x]||y==son[x]) continue;
dfs(y);
}
}
inline int lca(int x,int y) {
while(top[x]!=top[y]) {
if(dep[top[x]]<dep[top[y]]) {
std::swap(x,y);
}
x=par[top[x]];
}
if(dep[x]<dep[y]) std::swap(x,y);
return y;
}
void solve(const int &x) {
for(unsigned i=0;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==par[x]) continue;
solve(y);
d[0][x]+=d[0][y];
d[1][x]+=d[1][y];
}
}
int main() {
const int n=getint();
for(register int i=1;i<n;i++) {
edge[i].u=getint();
edge[i].v=getint();
edge[i].t=getint();
add_edge(edge[i].u,edge[i].v);
}
dfs(1,0);
dfs(1);
const int m=getint();
for(register int i=0,s=1;i<m;i++) {
const int t=getint();
const int u=lca(s,t);
d[0][s]++;
d[0][u]--;
d[1][t]++;
d[1][u]--;
s=t;
}
solve(1);
int ans=0;
for(register int i=1;i<n;i++) {
if(!edge[i].t) continue;
const int &u=edge[i].u,&v=edge[i].v;
const int tmp=dep[u]<dep[v]?d[0][v]:d[1][u];
(ans+=(power(2,tmp)+mod-1)%mod)%=mod;
}
printf("%d\n",ans);
return 0;
}

[CF575B]Bribes的更多相关文章

  1. Bubble Cup 8 finals B. Bribes (575B)

    题意: 给定一棵n个点和有向边构成的树,其中一些边是合法边,一些边是非法边, 经过非法边需要1的费用,并且经过之后费用翻倍. 给定一个长为m的序列,问从点1开始按顺序移动到序列中对应点的总费用. 1& ...

  2. 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)是倒过来的). ...

  3. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 K. King’s Rout

    K. King's Rout time limit per test 4 seconds memory limit per test 512 megabytes input standard inpu ...

  4. 越狱Season 1- Episode 16

    Season 1, Episode 16 -Burrows:Don't be. It's not your fault. 不要,不是你的错 -Fernando: Know what I like? 知 ...

  5. C Primer Plus(第五版)5

    第5章 运算符,表达式和语句 5.1 循环简单 程序清单 5.1 显示了一个示例程序,该程序做了一点算术运算来计算穿 9 码鞋的脚用英寸表示的长度.为了增加你对循环的理解,程序的第一版演示了不使用循环 ...

  6. Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)

    Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. ...

  7. perl学习笔记--搭建开发环境

    windows下perl开发环境搭建 perl下载地址:http://www.activestate.com/developer-tools 各个插件的安装方法:(通过代理上网的方法) 方法一:pad ...

  8. PMP模拟考试-1

    1. A manufacturing project has a schedule performance index (SPI) of 0.89 and a cost performance ind ...

  9. 每日英语:China's New Anti-Graft Website: A Tale of Tigers, Flies and Bath Tubs

    With considerable fanfare, China's anti-graft squad has rolled out a brand new website in the ongoin ...

随机推荐

  1. java 学习中出过的错误

    1. 运行异常 C:\Users\plan-B\java>java TapeDeckTestDrive.class 错误: 找不到或无法加载主类 TapeDeckTestDrive.class

  2. 重新认识python

    为什么这样说呢,我几个月前就开始学python,但是一直都没有进步,还就只是会一些其它语言的共性的问题,也就是新学习的约等于0. 后来一直找一些适合自己的教材,通过同学找到了一个学长的教程. 开始了新 ...

  3. 装饰器实现session登陆 验证功能

    装饰器 登陆验证功能 1.装饰器模板 from django.shortcuts import render, redirect, HttpResponse from django.conf impo ...

  4. asp.net core 缓存和Session

    缓存 缓存在内存中 ASP.NET Core 使用 IMemoryCache内存中缓存是使用依赖关系注入从应用中引用的服务. 请在ConfigureServices中调用AddMemoryCache( ...

  5. SQL Server 常用函数使用方法

    之前就想要把一些 SQL 的常用函数记录下来, 直到今天用到substring()这个函数,C# 里面这个方法起始值是 0,而 SQL 里面起始值是 1.傻傻分不清楚... 这篇博客作为记录 SQL ...

  6. 利用sqlmap注入测试

    安装:yum install -y gitcd /usr/local && git clone https://github.com/sqlmapproject/sqlmap.gitc ...

  7. java.lang.OutOfMemoryError: unable to create new native thread

    ps -o nlwp 70753 sudo -u tomcat jmap -dump:format=b,file=fundmarketmanage.hprof 78894

  8. Java面向对象三大特性

    封装.继承.多态. 1.封装 封装就是将对象的属性和行为特征包装到一个程序单元(即类)中,把实现细节隐藏起来,通过公用的方法来展现类对外提供的功能,提高了类的内聚性,降低了对象之间的耦合性. 2.继承 ...

  9. mysql binary

    mysql在比较字符串的时候是忽略大些写的 比如有用户叫ABC和abc select * from `sys_user` where username = 'abc' 会出来两条记录 select * ...

  10. pkusc2018数学题

     题解: 还是很水的 复制个题面过来 好吧我不会复制 https://www.cnblogs.com/skylee03/p/9127130.html 题目链接 第一题是个傻逼题 初中数学题随便凑凑 ( ...