Bubble Cup 8 finals B. Bribes (575B)
题意:
给定一棵n个点和有向边构成的树,其中一些边是合法边,一些边是非法边,
经过非法边需要1的费用,并且经过之后费用翻倍。
给定一个长为m的序列,问从点1开始按顺序移动到序列中对应点的总费用。
1<=n<=10^5,
1<=m<=10^6
题解:
还是比较水的…
正解是各种方法求LCA,在点上打标记,最后DFS一遍就可以得到答案。
用tarjan求LCA可以做到总复杂度O(n*α)…
我傻傻地见树就剖,强行O(n log n log n)碾过去了…
每次把起点终点之间的路径的经过次数加一,最后统计非法边对应的点,
对答案的贡献是 2^(次数)-1 。
ZKW线段树的常数还是比较可以接受的…虽然Codeforces机子本来就快…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 #include <cstdio>
#include <cstring>
#define fore(p) for(int pt=h[p];pt;pt=e[pt].nx)
typedef long long lint;
const int N = , MO = ;
inline int read()
{
int s = ; char c; while((c=getchar())<'0'||c>'9');
do{s=s*+c-'0';}while((c=getchar())>='0'&&c<='9');
return s;
}
int n,q,aa,bb,tot,ttot,cur,tg,curd,tim,ans,ql,qr,S,p2[],h[N],top[N],d[N],hs[N],f[N],iw[N];
bool il[N],il2[N],qv;
struct eg{int dt,nx;bool le;}e[N*];
struct segt
{
int tr[N*];
int query(int p){ int s = ; for(int i=S+p;i>=;i>>=) s += tr[i]; return s; }
void db(int l,int r)
{
for(l=l+S-,r=r+S+;l^r^;l>>=,r>>=)
{
if(~l&) tr[l^]++;
if( r&) tr[r^]++;
}
}
}tr1,tr2;
inline void link(int b)
{
e[++tot].nx = h[aa]; e[tot].dt = bb; e[tot].le = ; h[aa] = tot;
e[++tot].nx = h[bb]; e[tot].dt = aa; e[tot].le = b; h[bb] = tot;
}
int dfs1(int p,int ff)
{
f[p] = ff, d[p] = ++curd;
int sz = ,nx,t,mx=;
fore(p)
{
if((nx=e[pt].dt)==ff) continue;
if(e[pt^].le) il[nx] = ;
if(e[pt].le) il2[nx] = ;
t = dfs1(nx,p);
if(t>mx) mx = t, hs[p] = nx;
}
curd--;
return sz;
}
void dfs2(int p,int tp)
{
top[p] = tp;
iw[p] = ++tim;
if(hs[p]) dfs2(hs[p],tp);
fore(p)
if(e[pt].dt!=f[p]&&e[pt].dt!=hs[p])
dfs2(e[pt].dt,e[pt].dt);
}
void calc(int aa,int bb)
{
if(aa==bb) return;
while(top[aa]!=top[bb])
{
if(d[top[aa]]>d[top[bb]]) tr1.db(iw[top[aa]],iw[aa]), aa = f[top[aa]];
else tr2.db(iw[top[bb]],iw[bb]), bb = f[top[bb]];
}
if(d[aa]>d[bb]) tr1.db(iw[bb]+,iw[aa]);
else tr2.db(iw[aa]+,iw[bb]);
}
int main()
{
int i,j;
n = read();
for(S=;S<=n+;S<<=);
for(i=,tot=;i<=n;i++) aa = read(), bb = read(), link(!read());
dfs1(,); dfs2(,); q = read();
for(p2[]=,i=;i<=q;i++) p2[i] = ((lint)p2[i-]<<1ll)%MO;
for(cur=;q--;cur=tg)
tg = read(), calc(cur,tg);
for(i=;i<=n;i++)
{
if(!il[i]) ans = ((lint)ans+p2[tr1.query(iw[i])]+MO-)%MO;
if(!il2[i]) ans = ((lint)ans+p2[tr2.query(iw[i])]+MO-)%MO;
}
printf("%d\n",ans);
return ;
}
补上O(n*α)的做法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 #include <cstdio>
#include <cstring>
#define fore(p) for(int pt=h[p];pt;pt=e[pt].nx)
typedef long long lint;
const int N = , MO = ;
inline int read()
{
int s = ; char c; while((c=getchar())<'0'||c>'9');
do{s=s*+c-'0';}while((c=getchar())>='0'&&c<='9');
return s;
}
int n,q,aa,bb,tot,qtot,cur,tg,curd,tim,ans,p2[],h[N],qh[N],rp[N],f[N],w[N],w2[N],mx;
bool il[N],il2[N],b[N],qv;
struct eg{int dt,nx;bool le;}e[N*];
struct qu{int dt,nx;}qs[N*];
inline void link(int b)
{
e[++tot].nx = h[aa]; e[tot].dt = bb; e[tot].le = ; h[aa] = tot;
e[++tot].nx = h[bb]; e[tot].dt = aa; e[tot].le = b; h[bb] = tot;
}
inline void linkq()
{
if(aa==bb) return;
qs[++qtot].nx = qh[aa]; qs[qtot].dt = bb; qh[aa] = qtot;
qs[++qtot].nx = qh[bb]; qs[qtot].dt = aa; qh[bb] = qtot;
w[aa]++; w2[bb]++;
}
int findf(int p){ return f[p]==p?p:(f[p]=findf(f[p])); }
void dfs(int p)
{
b[p] = ;
for(int nx,pt=qh[p];pt;pt=qs[pt].nx)
if(b[qs[pt].dt])
nx = findf(qs[pt].dt), w[nx]--, w2[nx]--;
for(int nx,pt=h[p];pt;pt=e[pt].nx)
if(!b[nx=e[pt].dt])
{
if(e[pt^].le) il[nx] = ;
if(e[pt].le) il2[nx] = ;
dfs(nx);
f[nx] = p;
w[p] += w[nx];
w2[p] += w2[nx];
}
if(w[p]>mx) mx = w[p];
if(w2[p]>mx) mx = w2[p];
}
int main()
{
int i,j;
for(n=read(),i=,tot=,f[]=;i<=n;i++) aa = read(), bb = read(), link(!read()), f[i] = i;
for(q=read(),i=,qtot=,aa=;i<=q;i++,aa=bb) bb = read(), linkq();
dfs();
for(p2[]=,i=;i<=mx;i++) p2[i] = ((lint)p2[i-]<<1ll)%MO;
for(i=;i<=n;i++)
{
if(!il[i]) ans = ((lint)ans+p2[w[i]]-)%MO;
if(!il2[i]) ans = ((lint)ans+p2[w2[i]]-)%MO;
}
printf("%d\n",ans);
return ;
}
Bubble Cup 8 finals B. Bribes (575B)的更多相关文章
- 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][ ...
- Bubble Cup 11 - Finals [Online Mirror, Div. 1]题解 【待补】
Bubble Cup 11 - Finals [Online Mirror, Div. 1] 一场很好玩的题啊! I. Palindrome Pairs 枚举哪种字符出现奇数次. G. AI robo ...
- 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)是倒过来的). ...
- Bubble Cup 8 finals I. Robots protection (575I)
题意: 有一个正方形区域, 要求支持两个操作: 1.放置三角形,给定放置方向(有4个方向,直角边与坐标轴平行),直角顶点坐标,边长 2.查询一个点被覆盖了多少次 1<=正方形区域边长n<= ...
- Bubble Cup 8 finals H. Bots (575H)
题意: 简单来说就是生成一棵树,要求根到每个叶子节点上的路径颜色排列不同, 且每条根到叶子的路径恰有n条蓝边和n条红边. 求生成的树的节点个数. 1<=n<=10^6 题解: 简单计数. ...
- Bubble Cup 8 finals G. Run for beer (575G)
题意: 给定一个带权无向图,每条边的代价为边权/当前速度,每次到达一个新节点,速度都会除以10. 求0号点到n-1号点的最小代价,如果多解输出点数最少的解,输出代价.路径点数.路径经过的点. 1< ...
- Bubble Cup 8 finals F. Bulbo (575F)
题意: 给定初始位置,查询n次区间,每次查询前可以花费移动距离的代价来移动, 查询时需要花费当前位置到区间内最近的点的距离,求最小代价. 1<=n<=5000,1<=所有位置< ...
- Bubble Cup 8 finals E. Spectator Riots (575E)
题意: 一个长宽是100000单位的球场上有很多暴动的观众,每个观众都有一个速度v, 在一秒内,观众会等概率地移动到与原位置的曼哈顿距离<=v的地方(不会移动到界外). 你需要选取三个位置,这三 ...
- Bubble Cup 8 finals D. Tablecity (575D)
题意: (无输入,纯输出题) 一个城市用1000列2行的格子表示,一个小偷藏在城市的某一处. 在每一小时的开始, 在(X, Y)位置的小偷可以移动到 (X - 1, Y), (X + 1, Y),(X ...
随机推荐
- T-SQL 转义select … like中的特殊字符(百分号)
众所周知,T-SQL中LIKE运算符使用%符号表示通配符.很多时候可能需要查询包含有%的数据,比如需要查询字段coupon中含有5%的数据.那么如何使用已经有百分号(%)符号的LIKE搜索字符串呢? ...
- JavaScript (If...Else和Switch和循环遍历) 语句以及常用消息框
If...Else 语句 JavaScript中if...else语句和Java中的语法和使用方法是一样的. 只是在JavaScript中要使用小写字母.使用大写的 IF 会出错! 至于if...el ...
- Eclipse创建Maven工程报错
问题 用Eclipse创建maven工程的时候,总是会报错,例如提示: Unable to create project from archetype [org.apache.maven.archet ...
- 【java开发】ubuntu常用命令及环境搭建
学习第一天,今天内容相对简单,主要就是ubuntu一些常用命令及常规操作,后续涉及到环境的搭建,也会在本文再更. ubuntu环境搭建 第一种 也是最简单最方便的 通过vm虚拟机软件,下载iso镜像进 ...
- Mybatis
Mybatis MyBatis本是apache的一个开源项目iBatis,2010年这个项目有Apache software foundation 迁移到了Google code,并改名MyBatis ...
- hadoop fs -ls no such file or directory
http://blog.csdn.net/baolibin528/article/details/43650919
- 如何给SVG填充和描边应用线性渐变
给SVG元素应用填充和描边有三种方法(戳这里学习SVG填充和描边的相关内容).你可以使用纯色.图案或渐变.前面两种方法我们之前已经讲过了,现在我们来讨论第三种方法——渐变. SVG提供了两种渐变——线 ...
- JSP九大内置对象及四个作用域
九大对象: 内置对象(又叫隐含对象,有9个内置对象):不需要预先声明就可以在脚本代码和表达式中随意使用 1-out: javax.servlet.jsp.JspWriter类型,代表输出流的对象.作用 ...
- codevs 2830 蓬莱山辉夜
2830 蓬莱山辉夜 http://codevs.cn/problem/2830/ 题目描述 Description 在幻想乡中,蓬莱山辉夜是月球公主,居住在永远亭上,二次设定说她成天宅在家里玩电脑, ...
- oAuth 2.0 笔记
OAuth 2.0规范于2012年发布,很多大型互联网公司(比如:微信.微博.支付宝)对外提供的SDK中,授权部分基本上都是按这个规范来实现的. OAuth 2.0提供了4种基本的标准授权流程,最为复 ...