Fantasia

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1654    Accepted Submission(s): 429

Problem Description
Professor Zhang has an undirected graph G with n vertices and m edges. Each vertex is attached with a weight wi. Let Gi be the graph after deleting the i-th vertex from graph G. Professor Zhang wants to find the weight of G1,G2,...,Gn.

The weight of a graph G is defined as follows:

1. If G is connected, then the weight of G is the product of the weight of each vertex in G.
2. Otherwise, the weight of G is the sum of the weight of all the connected components of G.

A connected component of an undirected graph G is a subgraph in which any two vertices are connected to each other by paths, and which is connected to no additional vertices in G.

 
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains two integers n and m (2≤n≤105,1≤m≤2×105) -- the number of vertices and the number of edges.

The second line contains n integers w1,w2,...,wn (1≤wi≤109), denoting the weight of each vertex.

In the next m lines, each contains two integers xi and yi (1≤xi,yi≤n,xi≠yi), denoting an undirected edge.

There are at most 1000 test cases and ∑n,∑m≤1.5×106.

 
Output
For each test case, output an integer S=(∑i=1ni⋅zi) mod (109+7), where zi is the weight of Gi.
 
Sample Input
1
3 2
1 2 3
1 2
2 3
 
Sample Output
20
 
/*
hdu 5739 割点 problem:
给你一个无向图,G[i]为删除i点时,无向图的价值. 求 sum(i*G[i])%mod
如果当前是连通的,那么连通分量的价值为所有点权值的积(任意两个节点连通)
否则为拆分后的各个连通分量的价值的和 solve:
所以需要判断当前点是否是割点.
如果不是割点,只需要减去这个点的权值即可. 如果是割点,要减去这个连通分量的价值再加上拆散后的各个连通分量的值 最开始题意理解错了- -,而且模板有点问题,一直wa. hhh-2016-08-27 19:47:17
*/
#pragma comment(linker,"/STACK:124000000,124000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <math.h>
#include <queue>
#include <map>
#define lson i<<1
#define rson i<<1|1
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define scanfi(a) scanf("%d",&a)
#define scanfl(a) scanf("%I64d",&a)
#define key_val ch[ch[root][1]][0]
#define inf 1e9
using namespace std;
const ll mod = 1e9+7;
const int maxn = 100005; struct Edge
{
bool cut ;
int v,next,w;
} edge[maxn*5]; int head[maxn],tot;
int low[maxn],dfn[maxn],Stack[maxn],index,top;
bool Instack[maxn],cut[maxn];
int bridge;
ll val[maxn],mul[maxn],ans[maxn],fans[maxn],tval[maxn];
void add_edge(int u,int v)
{
edge[tot].v = v,edge[tot].next = head[u],head[u] = tot++;
} ll tans =1 ; ll pow_mod(ll a,ll n)
{
ll cnt =1 ;
while(n)
{
if(n & 1) cnt = cnt*a%mod;
a = a*a%mod;
n >>= 1;
}
return cnt ;
}
int now;
vector<int> vec[maxn];
int from[maxn];
void Tarjan(int u,int ance,int pre)
{
int v;
vec[now].push_back(u);
from[u] = now;
low[u] = dfn[u] = ++index;
Stack[top++] = u;
Instack[u] = true;
tans = tans*val[u] % mod;
int son = 0;
for(int i= head[u]; i!= -1; i = edge[i].next)
{
v = edge[i].v;
if(v == pre){
continue;
}
if(!dfn[v])
{
son ++ ;
ll tp = tans;
Tarjan(v,ance,u);
low[u] = min(low[u],low[v]); if(u != pre && low[v] >= dfn[u])
{
cut[u] = true;
ll ta = tans * pow_mod(tp,mod-2)%mod;
// cout <<"node:" << u <<" ta:" <<ta <<endl;
ans[u] = (ans[u] + ta)%mod;
fans[u] = (fans[u] * ta) % mod;
}
}
else if(low[u] > dfn[v])
low[u] = dfn[v];
}
if(u == ance && son > 1)
cut[u] = true;
Instack[u] = false;
top --;
} void init(int n)
{
for(int i = 0; i <= n+1; i++)
{
head[i] = -1;
ans[i] = 0;
fans[i] = 1;
Instack[i]=cut[i]= 0;
dfn[i] = 0;
vec[i].clear();
}
tot=top=index=0;
} int main()
{
// freopen("in.txt","r",stdin);
int T,n,m,u,v; scanfi(T);
while(T--)
{ scanfi(n),scanfi(m);
init(n);
for(int i =1; i <= n; i++)
{
scanfl(val[i]);
fans[i] = 1;
}
for(int i = 0; i < m; i++)
{
scanfi(u),scanfi(v);
add_edge(u,v);
add_edge(v,u);
}
now = 1;
ll ob = 0;
for(int i = 1; i <= n; i++)
{
if(!dfn[i])
{
tans= 1;
Tarjan(i,i,-1);
tval[now] = tans;
ll amul = tans;
ob = (ob+tans) %mod;
// cout << "all:" <<tans<<endl;
for(int j = 0 ; j < vec[now].size(); j ++)
{
int to = vec[now][j]; // cout << to <<" " << fans[to] << endl;
if(to == i) continue;
ans[to] = (ans[to] + amul*pow_mod(fans[to]*val[to]%mod,mod-2)%mod);
if(ans[to] > mod) ans[to] -= mod;
}
now ++;
}
}
ll out = 0;
ll tm;
for(int i = 1; i <= n; i++)
{
// cout << fans[i] <<" " << ans[i] <<" " <<cut[i] << endl;
int tf = from[i];
if(cut[i])
{
tm = (ob - tval[tf] + ans[i] + mod)%mod;
}
else
{
if(vec[from[i]].size() > 1)
tm = (ob - tval[tf] + tval[tf]*pow_mod(val[i],mod-2)%mod + mod) % mod;
else
tm = (ob - tval[tf] + mod) % mod;
}
out = (out + i * tm % mod) % mod;
}
printf("%I64d\n",out);
}
return 0;
} /*
3
4 3
1 2 3 4
1 2
2 3
1 3 4 2
100000000 131231232 312354435 432134234
1 2
3 4 66
315142079 */

  

hdu 5739 割点的更多相关文章

  1. hdu 4587(割点的应用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4587 思路:题目的意思很简单,就是删除任意2个节点以及关联的边,求图的最大连通分量数.我们知道删除割点 ...

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

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

  3. HDU 5739 Fantasia

    可以将这个图转换成森林来进行树形dp求解.看了这篇具体教学才会的:http://www.cnblogs.com/WABoss/p/5696926.html 大致思路:求解一下点双连通分量(Tarjan ...

  4. HDU 4587 TWO NODES 割点

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4587 题意: 删除两个点,使连通块的数目最大化 题解: 枚举删除第一个点,然后对删除了第一个点的图跑 ...

  5. HDU 4587 TWO NODES 枚举+割点

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4587 TWO NODES Time Limit: 24000/12000 MS (Java/Other ...

  6. hdu 4587 推断孤立点+割点+ 删除点之后,剩下多少连通分量

    做了非常久...... 题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4587 先枚举删除的第一个点,第二个点就是找割点.没有割点当然也有答案 学到 ...

  7. HDU 3844 Mining Your Own Business(割点,经典)

    题意: 给出一个连通图,要求将某些点涂黑,使得无论哪个点(包括相关的边)撤掉后能够成功使得剩下的所有点能够到达任意一个涂黑的点,颜料不多,涂黑的点越少越好,并输出要涂几个点和有多少种涂法. 思路: 要 ...

  8. Destroying the bus stations HDU - 2485(最小割点)

    题意: 就是求最小割点 解析: 正向一遍spfa 反向一遍spfa  然后遍历每一条边,对于当前边 如果dis1[u] + dis2[v] + 1 <= k 那么就把这条边加入到网络流图中, 每 ...

  9. Key Vertex (hdu 3313 SPFA+DFS 求起点到终点路径上的割点)

    Key Vertex Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

随机推荐

  1. alpha-咸鱼冲刺day1

    一,合照 emmmmm.自然是没有的. 二,项目燃尽图 三,项目进展 登陆界面随意写了一下.(明天用来做测试的) 把学姐给我的模板改成了自家的个人主页界面,侧边栏啥的都弄出来了(快撒花花!) 四,问题 ...

  2. Beta冲刺 第二天

    Beta冲刺 第二天 1. 昨天的困难 由于前面的冲刺留下的问题很多,而且混乱的代码给我们接下来的完善工作带来了巨大的困难. 2. 今天解决的进度 潘伟靖: 1.对代码进行了review 2.为系统增 ...

  3. xcode修改代码目录结构出现clang:error:nosuchfileordirectory解决方法

    需要迁移一个开源工程的一部分内容到自己工程,迁移对方的工程到自己工程之后,因目录结构配置整理需要,对嵌入的工程目录进行了结构改变,编译后出现: clang: error: no such file o ...

  4. C# 一个初学者对 依赖注入 IOC 的理解( 含 Unity 的使用)

    通过 人打电话 来谈谈自己对IOC的理解 版本1.0 public class Person { public AndroidPhone Phone { get; set; } public void ...

  5. 作业三:模拟 mysql 进行增删改查

    # !/usr/bin/env python3 # _*_coding:utf-8_*_ def help_sql(cmd): if cmd in func_dic.keys(): print('{} ...

  6. python中的进程池:multiprocessing.Pool()

    python中的进程池: 我们可以写出自己希望进程帮助我们完成的任务,然后把任务批量交给进程池 进程池帮助我们创建进程完成任务,不需要我们管理.进程池:利用multiprocessing 下的Pool ...

  7. 用js来实现那些数据结构(数组篇03)

    终于,这是有关于数组的最后一篇,下一篇会真真切切给大家带来数据结构在js中的实现方式.那么这篇文章还是得啰嗦一下数组的相关知识,因为数组真的太重要了!不要怀疑数组在JS中的重要性与实用性.这篇文章分为 ...

  8. 前端学习之jquery/下

    前端学习之jquery 一 属性操作 html(): console.log($("div").html()); $(".test").html("& ...

  9. 安卓手机USB共享网络给PC上网

    开端 哈哈,最近我又发现了一个校园网的漏洞,但是只能手机连接,于是就想手机连接之后通过usb共享给电脑上网. 在手机上连接校园网WiFi,开启USB网络共享并且连接电脑之后,却发现电脑十分的卡顿!CP ...

  10. 关于tr069网管开发系列教程

    原创作品,转载请注明出处,严禁非法转载.如有错误,请留言! email:40879506@qq.com 声明:本系列涉及的开源程序代码学习和研究,严禁用于商业目的. 如有任何问题,欢迎和我交流.(企鹅 ...