[CF575B]Bribes
[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的更多相关文章
- Bubble Cup 8 finals B. Bribes (575B)
题意: 给定一棵n个点和有向边构成的树,其中一些边是合法边,一些边是非法边, 经过非法边需要1的费用,并且经过之后费用翻倍. 给定一个长为m的序列,问从点1开始按顺序移动到序列中对应点的总费用. 1& ...
- 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)是倒过来的). ...
- 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 ...
- 越狱Season 1- Episode 16
Season 1, Episode 16 -Burrows:Don't be. It's not your fault. 不要,不是你的错 -Fernando: Know what I like? 知 ...
- C Primer Plus(第五版)5
第5章 运算符,表达式和语句 5.1 循环简单 程序清单 5.1 显示了一个示例程序,该程序做了一点算术运算来计算穿 9 码鞋的脚用英寸表示的长度.为了增加你对循环的理解,程序的第一版演示了不使用循环 ...
- 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. ...
- perl学习笔记--搭建开发环境
windows下perl开发环境搭建 perl下载地址:http://www.activestate.com/developer-tools 各个插件的安装方法:(通过代理上网的方法) 方法一:pad ...
- PMP模拟考试-1
1. A manufacturing project has a schedule performance index (SPI) of 0.89 and a cost performance ind ...
- 每日英语: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 ...
随机推荐
- Cannot uninstall 'html5lib'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
如标题,安装Tensorflow-gpu时遇到的完整问题 Cannot uninstall 'html5lib'. It is a distutils installed project and th ...
- java数组元素的复制
package day03; import java.util.Arrays; /** * * 数组元素的复制: int的默认值是0,boolean默认值是flase 数组的扩容和缩容(本质的实现数组 ...
- 微信小程序--代码构成---WXSS 样式
WXSS 样式 WXSS 具有 CSS 大部分的特性,小程序在 WXSS 也做了一些扩充和修改. 新增了尺寸单位.在写 CSS 样式时,开发者需要考虑到手机设备的屏幕会有不同的宽度和设备像素比,采用一 ...
- C# 合并两张图片
private BitmapSource CombineImage(BitmapSource img1,BitmapSource img2) { var composeImg = new Render ...
- Spring MVC基础知识整理➣国际化和异常处理
概述 Spring框架为WEB项目提供了国际化以及异常处理机制.所谓的国际化也就是不同国籍,显示不同国籍的语言与符号.异常处理,也就是能够捕获WEB项目下的所有异常信息,并能处理记录这些异常信息机制. ...
- ssm又乱码
以前用mybatis的时候是这样的 mysql.connection.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characte ...
- Python自带IDE设置字体
打开Python 3.7.0 shell 点击菜单项 “”Options“”>"Configure IDLE" 可选择窗口的字体和大小 可选择背景主题颜色 可自定义配置
- ORM框架之------Dapper,Net下无敌的ORM
一,介绍:Dapper是一款轻量级ORM工具.如果你在小的项目中,使用Entity Framework.NHibernate 来处理大数据访问及关系映射,未免有点杀鸡用牛刀.你又觉得ORM省时省力,这 ...
- 如何扩展Orchard
翻译自: http://msdn.microsoft.com/en-us/magazine/hh708754.aspx 动态类型系统 Content item是Orchard中的原子, 比如b ...
- 【转】通过 INotifyPropertyChanged 实现观察者模式
通过 INotifyPropertyChanged 实现观察者模式 原博客地址 http://frankdzu.blog.sohu.com/117654536.html 普通观察者模式存在的问题 我们 ...