B. Bribes

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/575/problem/B

Description

Ruritania is a country with a very badly maintained road network, which is not exactly good news for lorry drivers that constantly have to do deliveries. In fact, when roads are maintained, they become one-way. It turns out that it is sometimes impossible to get from one town to another in a legal way – however, we know that all towns are reachable, though illegally!

Fortunately for us, the police tend to be very corrupt and they will allow a lorry driver to break the rules and drive in the wrong direction provided they receive ‘a small gift’. There is one patrol car for every road and they will request 1000 Ruritanian dinars when a driver drives in the wrong direction. However, being greedy, every time a patrol car notices the same driver breaking the rule, they will chargedouble the amount of money they requested the previous time on that particular road.

Borna is a lorry driver that managed to figure out this bribing pattern. As part of his job, he has to make K stops in some towns all over Ruritania and he has to make these stops in a certain order. There are N towns (enumerated from 1 to N) in Ruritania and Borna’s initial location is the capital city i.e. town 1. He happens to know which ones out of the N - 1 roads in Ruritania are currently unidirectional, but he is unable to compute the least amount of money he needs to prepare for bribing the police. Help Borna by providing him with an answer and you will be richly rewarded.

Input

The first line contains N, the number of towns in Ruritania. The following N - 1 lines contain information regarding individual roads between towns. A road is represented by a tuple of integers (a,b,x), which are separated with a single whitespace character. The numbers a and b represent the cities connected by this particular road, and x is either 0 or 1: 0 means that the road is bidirectional, 1 means that only the a → b direction is legal. The next line contains K, the number of stops Borna has to make. The final line of input contains K positive integers s1, …, sK: the towns Borna has to visit.

  • 1 ≤ N ≤ 105
  • 1 ≤ K ≤ 106
  • 1 ≤ a, b ≤ N for all roads
  •  for all roads
  • 1 ≤ si ≤ N for all 1 ≤ i ≤ K

Output

The output should contain a single number: the least amount of thousands of Ruritanian dinars Borna should allocate for bribes, modulo109 + 7.

Sample Input

51 2 02 3 05 1 13 4 155 4 5 2 2

Sample Output

4

HINT

题意

给你一棵树,有些边是无向边,有些边是有向边

每次你反方向走有向边的时候,就会使得这条边的边权乘以2

然后最后问你这个树的所有边权和是多少

题解:

建两棵树咯

比如从a->b,lca(a,b)=c,那就记录标记,第一棵树 a++,c--;第二棵树b++,c--;

最后按照深度排序,然后跑BFS统计一下每个边的和就好了……

大概就是酱紫,记录一下标记就吼了

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN=1000005;
const int DEG=20;
const int MOD=1000000007;
struct Edge
{int to,next,type;
}edge[MAXN*2];
int head[MAXN],tot;
void addedge(int u,int v,int type)
{edge[tot].to=v;edge[tot].next=head[u];edge[tot].type=type;head[u]=tot++;
}
void init()
{tot=0;memset(head,-1,sizeof(head));
}
int fa[MAXN][DEG];
int deg[MAXN],up[MAXN];
int cnt[2][MAXN];
void BFS(int root)
{queue<int>q;deg[root]=0;fa[root][0]=root;up[root]=0;q.push(root);while(!q.empty()){int u=q.front();q.pop();for(int i=1;i<DEG;i++)fa[u][i]=fa[fa[u][i-1]][i-1];for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(v==fa[u][0])continue;deg[v]=deg[u]+1;fa[v][0]=u;up[v]=-edge[i].type;q.push(v);}}
}
void DFS(int u)
{for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(v==fa[u][0])continue;DFS(v);for(int j=0;j<2;j++)cnt[j][u]+=cnt[j][v];}
}
int LCA(int u,int v)
{if(deg[u]>deg[v])swap(u,v);int hu=deg[u],hv=deg[v];int tu=u,tv=v;for(int det=hv-hu,i=0;det;det>>=1,i++)if(det&1)tv=fa[tv][i];if(tu==tv)return tu;for(int i=DEG-1;i>=0;i--){if(fa[tu][i]==fa[tv][i])continue;tu=fa[tu][i];tv=fa[tv][i];}return fa[tu][0];
}
int fp(int a,int k)
{int res=1;while(k){if(k&1)res=1LL*res*a%MOD;a=1LL*a*a%MOD;k>>=1;}return res;
}
int main()
{init();int n;scanf("%d",&n);int a,b,x;for(int i=1;i<n;i++){scanf("%d%d%d",&a,&b,&x);if(x==0){addedge(a,b,0);addedge(b,a,0);}else{addedge(a,b,1);addedge(b,a,-1);}}int root=1;BFS(root);int k,la=1,now;scanf("%d",&k);while(k--){scanf("%d",&now);if(la==now)continue;int tp=LCA(la,now);cnt[0][la]++;cnt[0][tp]--;cnt[1][now]++;cnt[1][tp]--;la=now;}DFS(root);int ans=0;for(int i=1;i<=n;i++){if(!up[i])continue;if(up[i]==-1)ans=(ans+fp(2,cnt[0][i])-1)%MOD;elseans=(ans+fp(2,cnt[1][i])-1)%MOD;}ans=(ans+MOD)%MOD;printf("%d\n",ans);return 0;
}

Codeforces Bubble Cup 8 - Finals [Online Mirror] B. Bribes lca相关推荐

  1. Bubble Cup 14 - Finals Online Mirror (Unrated, ICPC Rules, Teams Preferred, Div. 2) J. Robot Factor

    翻译: 你已经收到了Bubble机器人的数据.你知道你的任务是制造工厂设施,但在你开始之前,你需要知道工厂有多大,有多少房间.当你查看数据时,你会发现你有这个结构的尺寸,它是矩形的:N x M. 然后 ...

  2. Bubble Cup 14 - Finals Online Mirror (Unrated, ICPC Rules, Teams Preferred, Div. 2) E. Array Game

    Array Game Link 题目大意: A l i c e Alice Alice 与 B o b Bob Bob ,从一个长度为 n n n 的数组中轮流从数组首部或尾部取数, A l i c ...

  3. Bubble Cup 14 - Finals Online Mirror (Unrated, ICPC Rules, Teams Preferred, Div. 2) J. Robot Factory

    传送门 题意: 给你一个矩阵,矩阵中的每一个数的二进制串可以表示出该位置周围的围墙信息,让你求出所有的房间大小. 思路: 建图,搜索,一气呵成. #include<iostream> #i ...

  4. Bubble Cup 12 - Finals [Online Mirror, unrated, Div. 1] F. Workout plan(优先队列)

    有一个人要锻炼 n 天,初始时战斗力为 k,每天都要达到 x[i] 才可以,每天都有一款饮料价格为 c[i],每一款饮料都可以让他的战斗力增加 A,问最少要花都少钱才能完成每天的计划,如果有一天不能输 ...

  5. Bubble Cup X - Finals [Online Mirror] G. Bathroom terminal(STL)

    Description Smith wakes up at the side of a dirty, disused bathroom, his ankle chained to pipes. Nex ...

  6. Bubble Cup 8 finals D. Tablecity (575D)

    题意: (无输入,纯输出题) 一个城市用1000列2行的格子表示,一个小偷藏在城市的某一处. 在每一小时的开始, 在(X, Y)位置的小偷可以移动到 (X - 1, Y), (X + 1, Y),(X ...

  7. codeforces VK Cup 2018 - Wild-card Round 1 (unofficial unrated mirror)

    开开心心的打开codeforces, 本来打算疯狂AC一波,结果开了这一场,进去一看题好简单呀,火速交一发,结果交第二题的时候提示不让我交了??????????? 提示每种语言只能AC一道题,ok,难 ...

  8. codeforces ABBYY Cup 3.0 - Finals (online version) B2. Shave Beaver!

    题目:http://codeforces.com/contest/331/problem/B2 题意: 给定一个数组a1,a2,···,an,有两类操作:1.对于i1<i2<···< ...

  9. CodeForces ABBYY Cup 3.0 - Finals B Shave Beaver! (思维+线段树)

    题目链接 题意:给你n个数1~n的序列,随后又q次询问,每次询问有2种操作,操作1:查询从序列中取出数值为x,x+1,...,y-1,y的数的最少需要取多少次(且每取一次只能是一个递增的子序列)    ...

最新文章

  1. 如何使用 Spinnaker 和 Kubernetes 进行数据库变更发布?
  2. CentOS下查看最后登录的用户信息以及LOG记录
  3. 为什么说人工智能是一个大谎言
  4. JavaScript表单
  5. Oracle登陆SQL Plus,Oracle基础学习登陆SQLPLUS(一)
  6. 谷歌苹果齐降税,却没能拯救开发者的“钱包”!
  7. Reeder的本地使用
  8. GitHub 爬虫项目
  9. 记一次戴尔灵越7000(7000-7591)的坑爹螺丝设计,中招了保修都没门
  10. 漫话Kubernetes的网络架构,该用NodePort还是Ingress还是load balancer?
  11. 由两个重要极限推导常见等价无穷小以及常见导数公式
  12. PUN☀️八、拓展网络同步:RPCs 和 Properties
  13. python对excel操作简书_Python-Excel操作
  14. wakeup_sources
  15. 腾讯发布区块链产业加速器,生态共创助力新基建建设
  16. 跨境电商面临“寒冬”考验,如何转型升级入局新赛道(Starday)
  17. 微信小程序第三方平台开发流程以及改善
  18. 04 TCP/IP协议详解
  19. python宇晨_第三十届全国青少年科技创新大赛青少年科技创意作品中学组.PDF
  20. 漫长的岁月,沧海历经的人生

热门文章

  1. 开发函数计算的正确姿势 —— 排查超时问题
  2. Linux基本命令(三)-----Linux常用命令(按功能分类)
  3. 如何使用@vue/cli 3.0在npm上创建,发布和使用你自己的Vue.js组件库
  4. LNMP与LAMP的工作原理
  5. 数据结构 栈的实例应用,括号匹配
  6. Tab选项卡界面(1)
  7. 如何将Node.js Streaming MapReduce引入Amazon EMR
  8. 5.Knockout.Js(自定义绑定)
  9. Java的常用Swing外观类总结
  10. Java技术专题之JVM逻辑内存回收机制研究图解版