可以将这个图转换成森林来进行树形dp求解。看了这篇具体教学才会的:http://www.cnblogs.com/WABoss/p/5696926.html

大致思路:求解一下点双连通分量(Tarjan),新构造一个节点连向这个分量中每一个节点。每个点双连通分量都这样构造好之后,原本连通的一张图就形成了一棵树,并且这个树中拿掉一个节点之后的连通性和原图相同!因此,可以在树上求解答案(树dp即可)。

注意:数据不一定保证原图是连通的,所以要特别注意原图中单个点的情况。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
char c = getchar(); while(!isdigit(c)) c = getchar();
int x = ;
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
return x;
} const int maxn=;
const LL mod=1e9+; int h[maxn],tot;
struct X { int u,v,nx; }e[*maxn]; int pre[maxn],dfs_clock,low[maxn],bcc_cnt,bccno[maxn]; struct Edge
{
int u,v;
Edge(int from,int to) { u=from; v=to; }
};
stack<Edge> S;
vector<int>bcc[maxn]; int T,n,m,be[*maxn],block,sz,cnt[maxn];
bool f[maxn];
LL w[*maxn],ans[maxn],val[maxn],SUM,pru[*maxn]; int head[*maxn];
struct Tree{ int u,v,nx; }tree[*maxn];
int id; LL extend_gcd(LL a,LL b,LL &x,LL &y)
{
if(a==&&b==) return -;
if(b==){x=;y=;return a;}
LL d=extend_gcd(b,a%b,y,x);
y-=a/b*x;
return d;
} LL mod_reverse(LL a)
{
LL x,y;
LL d=extend_gcd(a,mod,x,y);
if(d==) return (x%mod+mod)%mod;
else return -;
} void ADD(int a,int b)
{
tree[tot].u=a, tree[tot].v=b, tree[tot].nx=head[a], head[a]=tot++;
} void AddEdge(int a,int b)
{
e[tot].u=a, e[tot].v=b, e[tot].nx=h[a], h[a]=tot++;
} int Tarjan(int u,int fa)
{
int lowu=pre[u]=++dfs_clock;
for(int i=h[u];i!=-;i=e[i].nx)
{
int v=e[i].v;
Edge e = Edge(u,v);
if(!pre[v])
{
S.push(e);
int lowv=Tarjan(v,u); lowu=min(lowv,lowu);
if(lowv>=pre[u])
{
bcc_cnt++; id++; w[id]=;
for(;;)
{
Edge x = S.top();S.pop();
if(bccno[x.u]!=bcc_cnt) {
ADD(x.u,id); ADD(id,x.u); be[id]=x.u; bccno[x.u]=bcc_cnt;
} if(bccno[x.v]!=bcc_cnt) {
ADD(x.v,id); ADD(id,x.v); be[id]=x.v; bccno[x.v]=bcc_cnt;
} if(x.u==u&&x.v==v) break;
}
}
}
else if(pre[v]<pre[u]&&v!=fa) { S.push(e); lowu=min(lowu,pre[v]); }
}
return lowu;
} void find_bcc()
{
memset(pre,,sizeof(pre));
memset(bccno,,sizeof(bccno));
dfs_clock=bcc_cnt=;
for(int i=;i<=n;i++) if(!pre[i]) Tarjan(i,-);
} void dfs(int x)
{
sz++; f[x]=; be[x]=block;
for(int i=h[x];i!=-;i=e[i].nx) if(!f[e[i].v]) dfs(e[i].v);
} void dp(int x)
{
f[x]=; pru[x]=w[x]; LL s=;
for(int i=head[x];i!=-;i=tree[i].nx)
{
if(f[tree[i].v]) continue;
dp(tree[i].v); pru[x]=(pru[x]*pru[tree[i].v])%mod;
s=(s+pru[tree[i].v])%mod;
}
s=(s+val[be[x]]*mod_reverse(pru[x])%mod)%mod;
ans[x]=((SUM-val[be[x]]+mod)%mod+s)%mod;
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) w[i]=(LL)read(); memset(h,-,sizeof h); tot=;
for(int i=;i<=m;i++) {
int u=read(),v=read(); AddEdge(u,v); AddEdge(v,u);
} memset(f,block=sz=,sizeof f);
for(int i=;i<=n;i++)
{
if(f[i]) continue;
block++; sz=; dfs(i); cnt[block]=sz;
} SUM=;
for(int i=;i<=block;i++) val[i]=;
for(int i=;i<=n;i++) val[be[i]]=(val[be[i]]*w[i])%mod;
for(int i=;i<=block;i++) SUM=(SUM+val[i])%mod; memset(head,-,sizeof head); tot=; id=n;
find_bcc(); memset(f,,sizeof f);
for(int i=;i<=n;i++)
if(cnt[be[i]]==) ans[i]=(SUM-val[be[i]]+mod)%mod;
for(int i=n+;i<=id;i++) if(!f[i]) dp(i); LL S=;
for(int i=;i<=n;i++) S=(S+((LL)i*ans[i])%mod)%mod;
printf("%lld\n",S);
}
return ;
}

HDU 5739 Fantasia的更多相关文章

  1. HDU 5739 Fantasia 双连通分量 树形DP

    题意: 给出一个无向图,每个顶点有一个权值\(w\),一个连通分量的权值为各个顶点的权值的乘积,一个图的权值为所有连通分量权值之和. 设删除顶点\(i\)后的图\(G_i\)的权值为\(z_i\),求 ...

  2. hdu 5739 割点

    Fantasia Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  3. HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...

  4. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  6. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  7. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  8. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  9. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

随机推荐

  1. ajax 假上传文件

    1. <form name="certForm" id="certForm" method="post" action="x ...

  2. android mediaplayer

  3. scanf函数与scanf_s函数

    ANSI C中没有scanf_s(),只有scanf(),scanf()在读取时不检查边界,所以可能会造成内存泄露.所以vc++2005/2008中提供了scanf_s(),在最新的VS2013中也提 ...

  4. Loadrunner中参数和变量的使用

    //字符串复制strcpy(str,"Hello ") ; //字符串连接strcat(str,"World !");lr_message("str: ...

  5. OA系统出现窗口拦截的解决办法

    我们使用oa时候,有时候会出现“你打开了窗口拦截功能”.如图 出现窗口被拦截主要有三种情况,分别是IE浏览器本身拦截功能.第三方插件(如百度工具栏.搜搜工具栏.谷歌工具栏等).第三方浏览器拦截功能(如 ...

  6. Word试卷文档模型化解析存储到数据库

    最近在搞一套在线的考试系统,有许多人反映试题的新增比较麻烦(需要逐个输入),于是呼就整个了试卷批量导入了 poi实现word转html 模型化解析html html转Map数组 Map数组(数组的操作 ...

  7. Spring Bean 生命周期

    转自:也谈Spring Bean的生命周期 开篇先用一张老图描述下Spring中Bean容器的生命周期. 插叙一下,记得某个博文中提到:“Spring的Bean容器只管理非单例Bean的生命周期,单例 ...

  8. hdu_3966_Aragorn's Story(树链剖分裸题)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:给你一棵树,然后给定点之间的路径权值修改,最后单点查询 题解:树链剖分裸题,这里我用树状数 ...

  9. vb脚本自动更新版本信息

    使用的串口显示软件为secureCrt,支持脚本功能,今天写了一个简单的软件升级脚本(VB脚本). 如下: # $language = "VBScript" # $interfac ...

  10. LeetCode OJ 86. Partition List

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...