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 ...
随机推荐
- ASP.NET Cookie(二)--控制Cookie的范围
默认情况下,一个站点的全部Cookie都一起存储在客户端上,而且所有Cookie都会随着对该站点发送的任何请求一起发送到服务器.也就是说,一个站点中的每个页面都能获得该站点的所有Cookie.但是,可 ...
- 2-部署phpmyadmin
软件下载地址:https://files.phpmyadmin.net/phpMyAdmin/4.5.5.1/phpMyAdmin-4.5.5.1-all-languages.zip 解压软件 [ro ...
- STM32C8T6 JTAG使用到PB3|PB4|PA13|PA14|PB15端口做普通IO时,需禁止JTAG!
GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIO ...
- jedisLock—redis分布式锁实现
一.使用分布式锁要满足的几个条件: 系统是一个分布式系统(关键是分布式,单机的可以使用ReentrantLock或者synchronized代码块来实现) 共享资源(各个系统访问同一个资源,资源的载体 ...
- [django]手动数据库备份
基本原理是按钮点击后,系统查询出数据表中信息,然后在网页中导出相关表格! 若有自动数据库备份的兄弟,指点一下! 模板代码: <a href="{% url 'work_backup' ...
- 项目游戏开发日记 No.0x000004
14软二杨近星(2014551622) 还有两周就要交项目了, 我们的作品, 作为作业, 好吧, 其实它完成了接近50%, (only the first bate), 其实也是各种各种忙, 然后才赶 ...
- C#进阶系列——一步一步封装自己的HtmlHelper组件:BootstrapHelper(三:附源码)
前言:之前的两篇封装了一些基础的表单组件,这篇继续来封装几个基于bootstrap的其他组件.和上篇不同的是,这篇的有几个组件需要某些js文件的支持. 本文原创地址:http://www.cnblog ...
- go database/sql sql-driver/mysql 操作
这里使用的是github.com/Go-SQL-Driver/MySQL, 所以需要下载一个github.com/Go-SQL-Driver/MySQL 引入 database/sql 和 githu ...
- 一个c#的输入框函数
private static string InputBox(string Caption, string Hint, string Default) { Form InputForm = new F ...
- UNC 目录格式检测C#代码
/// <summary> /// if path is UNC( Universal Naming Convention) path return or return false. // ...