【CF802L】Send the Fool Further! (hard)

题意:给你一棵n个节点的树,每条边有长度,从1号点开始,每次随机选择一个相邻的点走,走到一个叶子时就停止,问期望走的总路程。

$n\le 10^5$

题解:很自然想到游走那题,于是想到高斯消元,但是正常高斯消元是$O(n^3)$的。不过我们有一个套路:在树上进行高斯消元的复杂度是$O(n)$的。

先列出方程:设f(x)表示从x开始期望还要走的路程,x的度数是d,那么$f(x)=\frac {f(fa)+len} d+\frac {\sum f(ch)+len} d$。而在叶子处,方程是形如$f(x)=k\cdot f(fa)+b$的,将其代入父亲的方程,便可以使父亲的方程也变成$f(x)=k\cdot f(fa)+b$的形式,这样一路消上去,就得到了根节点的答案了。

如果想知道所有点的答案的话,再一路消下来就好了。想不到这个套路在pkuwc上用到了233。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=100010;
const ll P=1000000007;
int n,cnt;
int fa[maxn],to[maxn<<1],nxt[maxn<<1],head[maxn],q[maxn],d[maxn];
ll k[maxn],b[maxn],f[maxn];
inline ll pm(ll x,ll y)
{
ll z=1;
while(y)
{
if(y&1) z=z*x%P;
x=x*x%P,y>>=1;
}
return z;
}
inline void add(int a,int b)
{
to[cnt]=b,nxt[cnt]=head[a],head[a]=cnt++;
}
void dfs(int x)
{
q[++q[0]]=x;
for(int i=head[x];i!=-1;i=nxt[i]) if(to[i]!=fa[x]) fa[to[i]]=x,dfs(to[i]);
}
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+(gc^'0'),gc=getchar();
return ret*f;
}
int main()
{
n=rd();
int i,x,y,z;
memset(head,-1,sizeof(head));
for(i=1;i<n;i++) x=rd()+1,y=rd()+1,z=rd(),add(x,y),add(y,x),k[x]++,k[y]++,d[x]++,d[y]++,b[x]+=z,b[y]+=z;
dfs(1);
for(i=n;i>=2;i--) if(d[q[i]]!=1)
{
x=q[i];
ll tmp=pm(k[x],P-2);
k[fa[x]]=(k[fa[x]]-tmp)%P;
b[fa[x]]=(b[fa[x]]+b[x]*tmp)%P;
}
f[1]=b[1]*pm(k[1],P-2)%P;
printf("%lld",(f[1]+P)%P);
return 0;
}

【CF802L】Send the Fool Further! (hard) 高斯消元的更多相关文章

  1. [高斯消元] POJ 2345 Central heating

    Central heating Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 614   Accepted: 286 Des ...

  2. 【BZOJ-3143】游走 高斯消元 + 概率期望

    3143: [Hnoi2013]游走 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2264  Solved: 987[Submit][Status] ...

  3. 【BZOJ-3270】博物馆 高斯消元 + 概率期望

    3270: 博物馆 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 292  Solved: 158[Submit][Status][Discuss] ...

  4. *POJ 1222 高斯消元

    EXTENDED LIGHTS OUT Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9612   Accepted: 62 ...

  5. [bzoj1013][JSOI2008][球形空间产生器sphere] (高斯消元)

    Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球 面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便于摧 ...

  6. hihoCoder 1196 高斯消元·二

    Description 一个黑白网格,点一次会改变这个以及与其连通的其他方格的颜色,求最少点击次数使得所有全部变成黑色. Sol 高斯消元解异或方程组. 先建立一个方程组. \(x_i\) 表示这个点 ...

  7. BZOJ 2844 albus就是要第一个出场 ——高斯消元 线性基

    [题目分析] 高斯消元求线性基. 题目本身不难,但是两种维护线性基的方法引起了我的思考. void gauss(){ k=n; F(i,1,n){ F(j,i+1,n) if (a[j]>a[i ...

  8. SPOJ HIGH Highways ——Matrix-Tree定理 高斯消元

    [题目分析] Matrix-Tree定理+高斯消元 求矩阵行列式的值,就可以得到生成树的个数. 至于证明,可以去看Vflea King(炸树狂魔)的博客 [代码] #include <cmath ...

  9. UVALive 7138 The Matrix Revolutions(Matrix-Tree + 高斯消元)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

随机推荐

  1. 【RespberryPi】数码管

    http://blog.mangolovecarrot.net/2015/06/03/raspi-study0801/ 应该可以用两块74HC595来驱动显示8位数的数码管.

  2. localstorage和sessionstorage上手使用记录

    通过阅读各路大神对web存储locastorage和sessionstorage的用法解析,自己试用了一下,在此留个备忘. 在项目中,如果用到很多次storage,要存储很多数据,就要把它封装成函数了 ...

  3. Ubuntu中的“资源管理器”System Monitor

    我们在Windows中经常要查看“资源管理器”来了解系统运行情况,对进程进行操作等等... 在Windows桌面任务栏右键“启动任务管理器”: 学习Linux,自然也希望有类似的“资源管理器”,安装完 ...

  4. js跟随的广告

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Nginx配置中文域名

    今天碰到一个好玩的问题,还以为是nginx的缓存,各种清理就差把nginx卸载了,后来想想不对应该是中文域名的问题,对中文进行编码,搞定,如下: ... server { listen 80; ser ...

  6. Log4net用法(.config文件)

    1.引用log4net.dll 2.在AssemblyInfo.cs中添加初始化: [assembly: log4net.Config.XmlConfigurator(ConfigFile = &qu ...

  7. Top 20 NuGet packages for captcha

    Top 20 NuGet packages for captcha CaptchaMvc.Mvc4 CaptchaMvc will implement your web MVC application ...

  8. 系统头文件cmath,cstdlib报错

    >C:\Program Files (x86)\Microsoft Visual Studio\\Community\VC\Tools\MSVC\\include\cstdlib(): erro ...

  9. jinja语法

    <!--base.html--> <!DOCTYPE html> <html lang="en"> <head> <!--ht ...

  10. linux 开机自启转载

    设置开机启动示例: # touch /etc/init.d/rc.local 设置为可执行: # chmod +x /etc/init.d/rc.local 用 update-rc.d 设置启动级别: ...