2015长春网络赛 —— B. Ponds (拓扑排序删点+DFS)

简介: 2015长春网络赛 —— B. Ponds (拓扑排序删点+DFS)

题目描述

Description

Betty owns a lot of ponds, some of them are connected with other ponds by pipes, and there will not be more than one pipe between two ponds. Each pond has a value v.

Now Betty wants to remove some ponds because she does not have enough money. But each time when she removes a pond, she can only remove the ponds which are connected with less than two ponds, or the pond will explode.

Note that Betty should keep removing ponds until no more ponds can be removed. After that, please help her calculate the sum of the value for each connected component consisting of a odd number of ponds


Input

The first line of input will contain a number T(1≤T≤30) which is the number of test cases.

For each test case, the first line contains two number separated by a blank. One is the number p(1≤p≤104) which represents the number of ponds she owns, and the other is the number m(1≤m≤105) which represents the number of pipes.

The next line contains p numbers v1,…,vp, where vi(1≤vi≤108) indicating the value of pond i.

Each of the last m lines contain two numbers a and b, which indicates that pond a and pond b are connected by a pipe.


Output

For each test case, output the sum of the value of all connected components consisting of odd number of ponds after removing all the ponds connected with less than two pipes.


Samples

Input Copy

1

7 7

1 2 3 4 5 6 7

1 4

1 5

4 5

2 3

2 6

3 6

2 7

Output

21

Source

2015 ACM/ICPC Asia Regional Changchun Online

题意:

n点m边无向图,问将度数<2的点删除后,剩余的由奇数个点构成的连通块的权值之和。

思路:

拓扑排序删点,然后DFS。

代码:

#pragma GCC optimize(3)
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>PLL;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
#define I_int ll
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
char F[200];
inline void out(I_int x) {
    if (x == 0) return (void) (putchar('0'));
    I_int tmp = x > 0 ? x : -x;
    if (x < 0) putchar('-');
    int cnt = 0;
    while (tmp > 0) {
        F[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while (cnt > 0) putchar(F[--cnt]);
    //cout<<" ";
}
ll ksm(ll a,ll b,ll p){ll res=1;while(b){if(b&1)res=res*a%p;a=a*a%p;b>>=1;}return res;}
const int inf=0x3f3f3f3f,mod=1e9+7;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int maxn=1e5+7,maxm=3e5+7,N=1e6+7;
const double PI = atan(1.0)*4;
int h[N],e[N],ne[N],idx,ans=N,n,m;
int d[N];
int val[maxn];
queue<int>q;
bool vis[maxn];
void init(){
    idx=0;
    memset(h,-1,sizeof h);
    memset(vis,0,sizeof vis);
    memset(d,0,sizeof d);
}
void add(int a,int b){
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void topsort(){
    for(int i=1;i<=n;i++)
        if(d[i]<2) q.push(i),vis[i]=1;
    while(!q.empty()){
        int t=q.front();q.pop();
        for(int i=h[t];~i;i=ne[i]){
            int j=e[i];
            if(vis[j]) continue;
            if(--d[j]<2) q.push(j),vis[j]=1;
        }
    }
}
ll siz=0,sum=0;
void dfs(int u){
    siz++;
    vis[u]=1;
    sum+=val[u];
    for(int i=h[u];~i;i=ne[i]){
        int j=e[i];
        if(vis[j]) continue;
        dfs(j);
    }
}
int main(){
    int T=read();
    while(T--){
        init();
        n=read(),m=read();
        for(int i=1;i<=n;i++) val[i]=read();
        while(m--){
            int u=read(),v=read();
            add(u,v);add(v,u);
            d[u]++;d[v]++;
        }
        topsort();
        ll res=0;
        siz=sum=0;
        for(int i=1;i<=n;i++)
            if(!vis[i]){
                siz=sum=0;
                dfs(i);
                if(siz%2) res+=sum;
            }
        out(res);puts("");
        while(!q.empty()) q.pop();
    }
    return 0;
}
目录
相关文章
|
6月前
|
监控
计算机网络的拓扑结构
计算机网络的拓扑结构。
106 0
|
7月前
|
机器学习/深度学习 传感器 算法
基于Matlab模拟无线网络拓扑、估计链路质量并可视化拓扑
基于Matlab模拟无线网络拓扑、估计链路质量并可视化拓扑
|
11月前
|
运维 网络架构
《企业运维之云上网络原理与实践》——第五章 云上网络互连——配套实验:通过现网的配置分析当前 CEN TR 的组网拓扑(1)
《企业运维之云上网络原理与实践》——第五章 云上网络互连——配套实验:通过现网的配置分析当前 CEN TR 的组网拓扑(1)
87 0
|
11月前
|
运维
《企业运维之云上网络原理与实践》——第五章 云上网络互连——配套实验:通过现网的配置分析当前 CEN TR 的组网拓扑(2)
《企业运维之云上网络原理与实践》——第五章 云上网络互连——配套实验:通过现网的配置分析当前 CEN TR 的组网拓扑(2)
78 0
|
11月前
|
弹性计算 运维
《企业运维之云上网络原理与实践》——第五章 云上网络互连——配套实验:通过现网的配置分析当前 CEN TR 的组网拓扑(3)
《企业运维之云上网络原理与实践》——第五章 云上网络互连——配套实验:通过现网的配置分析当前 CEN TR 的组网拓扑(3)
67 0
|
11月前
|
机器学习/深度学习 人工智能 网络架构
图神经网络的困境,用微分几何和代数拓扑解决
图神经网络的困境,用微分几何和代数拓扑解决
108 0
|
弹性计算 运维 网络架构
企业运维训练营之云上网络原理与实践 — 第五讲配套实验:通过现网的配置分析当前CEN TR的组网拓扑
1、了解和熟悉云企业网TR的基本操作; 2、理解云企业网关联转发和路由学习原理; 3、学会如何打通云上多个VPC之间网络以及云内网元之间配置。
企业运维训练营之云上网络原理与实践 — 第五讲配套实验:通过现网的配置分析当前CEN TR的组网拓扑
|
机器学习/深度学习 算法 数据挖掘
【数据挖掘】神经网络简介 ( 有向图本质 | 拓扑结构 | 连接方式 | 学习规则 | 分类 | 深度学习 | 机器学习 )(二)
【数据挖掘】神经网络简介 ( 有向图本质 | 拓扑结构 | 连接方式 | 学习规则 | 分类 | 深度学习 | 机器学习 )(二)
232 0
|
机器学习/深度学习 物联网 数据挖掘
【数据挖掘】神经网络简介 ( 有向图本质 | 拓扑结构 | 连接方式 | 学习规则 | 分类 | 深度学习 | 机器学习 )(一)
【数据挖掘】神经网络简介 ( 有向图本质 | 拓扑结构 | 连接方式 | 学习规则 | 分类 | 深度学习 | 机器学习 )(一)
683 0
|
网络协议 Java
【Java 网络编程】TCP 传输机制 ( 数据拆分 | 排序 | 顺序发送 | 顺序组装 | 超时重发 )
【Java 网络编程】TCP 传输机制 ( 数据拆分 | 排序 | 顺序发送 | 顺序组装 | 超时重发 )
225 0
【Java 网络编程】TCP 传输机制 ( 数据拆分 | 排序 | 顺序发送 | 顺序组装 | 超时重发 )