标签:
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 = 100010, MO = 1000000007; inline int read() { int s = 0; char c; while((c=getchar())<‘0‘||c>‘9‘); do{s=s*10+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[1000010],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*2]; struct segt { int tr[N*3]; int query(int p){ int s = 0; for(int i=S+p;i>=1;i>>=1) s += tr[i]; return s; } void db(int l,int r) { for(l=l+S-1,r=r+S+1;l^r^1;l>>=1,r>>=1) { if(~l&1) tr[l^1]++; if( r&1) tr[r^1]++; } } }tr1,tr2; inline void link(int b) { e[++tot].nx = h[aa]; e[tot].dt = bb; e[tot].le = 1; 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 = 1,nx,t,mx=0; fore(p) { if((nx=e[pt].dt)==ff) continue; if(e[pt^1].le) il[nx] = 1; if(e[pt].le) il2[nx] = 1; 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]+1,iw[aa]); else tr2.db(iw[aa]+1,iw[bb]); } int main() { int i,j; n = read(); for(S=1;S<=n+2;S<<=1); for(i=2,tot=1;i<=n;i++) aa = read(), bb = read(), link(!read()); dfs1(1,0); dfs2(1,1); q = read(); for(p2[0]=1,i=1;i<=q;i++) p2[i] = ((lint)p2[i-1]<<1ll)%MO; for(cur=1;q--;cur=tg) tg = read(), calc(cur,tg); for(i=1;i<=n;i++) { if(!il[i]) ans = ((lint)ans+p2[tr1.query(iw[i])]+MO-1)%MO; if(!il2[i]) ans = ((lint)ans+p2[tr2.query(iw[i])]+MO-1)%MO; } printf("%d\n",ans); return 0; }
Bubble Cup 8 finals B. Bribes (575B)
标签:
原文地址:http://www.cnblogs.com/meowww/p/4989807.html