[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 ...
随机推荐
- ERP系统
ERP系统是企业资源计划(Enterprise Resource Planning )的简称,是指建立在信息技术基础上,集信息技术与先进管理思想于一身,以系统化的管理思想,为企业员工及决策层提供决策手 ...
- SQL 查询表的第一条数据 和 最后一条数据
方法一: 使用TOP SELECT TOP 1 * FROM user; SELECT TOP 1 * FROM user order by id desc; 方法二: 使用LIMIT SELECT ...
- 547. Friend Circles
There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...
- Python学习(十九) —— 前端基础之HTML
转载自:http://www.cnblogs.com/liwenzhou/p/7988087.html 一.HTML介绍 1.Web服务本质 import socket sk = socket.soc ...
- jQuery懒加载插件 – jquery.lazyload.js简单调用
Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...
- PHP使用Apache中的ab测试网站的压力性能及mpm介绍
打开Apache安装的bin目录 shift+鼠标右键 复制粘贴以下代码->回车 ab -n 1000 -c 100 http://localhost/test.php 上例表示总共访问http ...
- Ubuntu 硬盘分区只读,重新挂载为读写分区之后,文件依然创建出错
原因: 分区只读,可能是windows没有正常关机,或者使用了混合休眠模式. 解决方案: sudo mount -o remount,rw /dev/sdaX 若重新挂载后,创建文件以及文件夹失败: ...
- Flume的四个使用案例
一.Flume监听端口 1,在linux机器上下载telnet工具 yum search telnet yumm install telnet.x86_64 2.编写flume的配置文件,并将文件复制 ...
- poj 2528 Mayor’s posters 【离散化】+【线段树】
<题目链接> 题目大意: 往一堵墙上贴海报,依次输出这些海报张贴的范围,这些海报能够相互覆盖,问最后能够看见几张海报? 解题分析: 由于是给出每张海报的区间,所以在这些区间内的很多点可能用 ...
- Linux下发送邮件
Linux下发送邮件 1.配置 vim /etc/mail.rc 文件尾增加以下内容 set from=ymwugui@linuxidc.com smtp=smtp.sina.com.cn set s ...