Ponds

                                  Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
                                       

Problem 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.
 
Sample Input
1
7 7
1 2 3 4 5 6 7
1 4
1 5
4 5
2 3
2 6
3 6
2 7
 
Sample Output
21
 
Source
 
姿势题
题意:给你n点,m边,每次删除其连通边数小于2的点,直到不能删除,问你最后所剩点的价值为多少
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<bitset>
#include<set>
#include<vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define memfy(a) memset(a,-1,sizeof(a))
#define TS printf("111111\n");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)
#define READ(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mod 1000000007
#define maxn 1000006
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//**************************************** int n,m;
ll sum;
int s,vis[maxn*],v[maxn],out[maxn];
vector<int >G[maxn];
void init()
{
FOR(i,,n)G[i].clear();
mem(vis);
mem(out);
}
void dfs(int x)
{
vis[x]=;
s++;
sum+=v[x];
for(int i=;i<G[x].size();i++)
{
if(!vis[G[x][i]])
{
dfs(G[x][i]);
}
}
}
int main()
{
int T=read();
while(T--)
{
init();
scanf("%d%d",&n,&m);
FOR(i,,n)
{
scanf("%d",&v[i]);
}
int a,b;
FOR(i,,m)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
out[a]++;
out[b]++;
}
ll ans=;
queue<int >q;
FOR(i,,n)
{
if(out[i]<=)q.push(i);
ans+=v[i];
}
while(!q.empty())
{
int k=q.front();
q.pop();
vis[k]=;
ans-=v[k];
for(int i=;i<G[k].size();i++)
{
out[G[k][i]]--;
if(out[G[k][i]]<=&&!vis[G[k][i]])q.push(G[k][i]);
}
}
FOR(i,,n)
{
sum=; s=;
if(!vis[i])dfs(i);
if(s%==)ans-=sum;
}
cout<<ans<<endl;
}
return ;
}

代码

2015ACM/ICPC Asia Regional Changchun Online /HDU 5438 图的更多相关文章

  1. 2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】

    Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  2. 2015 ACM/ICPC Asia Regional Changchun Online HDU - 5441 (离线+并查集)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给你n,m,k,代表n个城市,m条边,k次查询,每次查询输入一个x,然后让你一个城市对(u,v ...

  3. (并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 Travel Time Limit: 1500/1000 MS (Java/Others)    Memo ...

  4. (二叉树)Elven Postman -- HDU -- 54444(2015 ACM/ICPC Asia Regional Changchun Online)

    http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Time Limit: 1500/1000 MS (Java/Others)  ...

  5. hdu 5444 Elven Postman(二叉树)——2015 ACM/ICPC Asia Regional Changchun Online

    Problem Description Elves are very peculiar creatures. As we all know, they can live for a very long ...

  6. (线段树 区间查询)The Water Problem -- hdu -- 5443 (2015 ACM/ICPC Asia Regional Changchun Online)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=5443 The Water Problem Time Limit: 1500/1000 MS (Java/ ...

  7. Hdu 5442 Favorite Donut (2015 ACM/ICPC Asia Regional Changchun Online 最大最小表示法 + KMP)

    题目链接: Hdu 5442 Favorite Donut 题目描述: 给出一个文本串,找出顺时针或者逆时针循环旋转后,字典序最大的那个字符串,字典序最大的字符串如果有多个,就输出下标最小的那个,如果 ...

  8. Hdu 5446 Unknown Treasure (2015 ACM/ICPC Asia Regional Changchun Online Lucas定理 + 中国剩余定理)

    题目链接: Hdu 5446 Unknown Treasure 题目描述: 就是有n个苹果,要选出来m个,问有多少种选法?还有k个素数,p1,p2,p3,...pk,结果对lcm(p1,p2,p3.. ...

  9. Hdu 5439 Aggregated Counting (2015长春网络赛 ACM/ICPC Asia Regional Changchun Online 找规律)

    题目链接: Hdu 5439 Aggregated Counting 题目描述: 刚开始给一个1,序列a是由a[i]个i组成,最后1就变成了1,2,2,3,3,4,4,4,5,5,5.......,最 ...

随机推荐

  1. No-2.注释

    01. 注释的作用 使用用自己熟悉的语言,在程序中对某些代码进行标注说明,增强程序的可读性 02. 单行注释(行注释) 以 # 开头,# 右边的所有东西都被当做说明文字,而不是真正要执行的程序,只起到 ...

  2. swiper 旋转木马效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  3. ubuntu(linux)占领小米平板2(mipad2)

    昨天 2014年,媳妇坐月子,给媳妇买了mi6和一个小米平板2(16G).是我们人生拥有的第一个平板,激动不已. 买之前看了小米平板1的口碑不错,arm构架,NVIDIA的主板好像,图形处理做得当然没 ...

  4. oracle dmp文件的导入导出

    一.命令行方式 exp 用户名/密码@库名 file=文件位置.dmp owner=用户名 imp 用户名/密码@库名 file=文件位置.dmp 注意 : 导入过程若有的表已经存在可能会报错,可以全 ...

  5. Django 模版语法 一

    创建项目 django_template 和 app django-admin startproject django_template python manage.py startapp app01 ...

  6. Cropping multiple images the same way

    The tools we’ll be using are =GIMP= and =mogrify= (from the ImageMagick suite), so make sure that yo ...

  7. 【02】HTML5与CSS3基础教程(第8版)(全)

    [02]HTML5与CSS3基础教程(第8版)(全)   共392页.   (魔芋:大体上扫了一遍.没有什么新东西,都是入门的一些基础知识.) 已看完.       [美]elizabeth cast ...

  8. wps填充1到1000

    A1单元格1 ,选中,填充,序列,确定

  9. get方法和set方法

    定义一个类,该类有一个私有成员变量,通过构造方法将其进行赋初值,并提供该成员的getXXX()和setXXX()方法 提示:假设有private String name;则有 public void  ...

  10. Leetcode 212.单词搜索II

    单词搜索II 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻&q ...