2015ACM/ICPC Asia Regional Changchun Online /HDU 5438 图
Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
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
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.
7 7
1 2 3 4 5 6 7
1 4
1 5
4 5
2 3
2 6
3 6
2 7
//
#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 图的更多相关文章
- 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 ...
- 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 ...
- (并查集)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 ...
- (二叉树)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) ...
- 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 ...
- (线段树 区间查询)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/ ...
- Hdu 5442 Favorite Donut (2015 ACM/ICPC Asia Regional Changchun Online 最大最小表示法 + KMP)
题目链接: Hdu 5442 Favorite Donut 题目描述: 给出一个文本串,找出顺时针或者逆时针循环旋转后,字典序最大的那个字符串,字典序最大的字符串如果有多个,就输出下标最小的那个,如果 ...
- 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.. ...
- 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.......,最 ...
随机推荐
- pycharm激活2018
因为我的是Windows,所以这篇文章只针对Windows系统. 1.将“0.0.0.0 account.jetbrains.com”中的内容添加到hosts文件中,hosts路径为:C:\Windo ...
- [css或js控制图片自适应]
[css或js控制图片自适应]图片自动适应大小是一个非常常用的功能,在进行制作的时候为了防止图片撑开容器而对图片的尺寸进行必要的控制,我们可不可以用CSS控制图片使它自适应大小呢?此个人博客想到了一个 ...
- 一道超级复杂的js题目
先看以下代码: function Foo(){ getName = function(){ alert(1); }; return this; } Foo.getName = function(){ ...
- mysql cluster配置
依赖包要求:cmake gcc gcc-c++ ncurses Perl ncurses-devel 在7.3以及更高的版本中, WITH_NDB_JAVA是默认 ...
- Buffer.compare()
Buffer.compare(buf1, buf2) buf1 {Buffer} buf2 {Buffer} 返回:{Number} 比较 buf1 和 buf2 通常用于 Buffer 数组的排序目 ...
- Mac使用Aria2下载百度网盘,突破下载限速的方法教程
百度网盘目前可以说是在国内网盘环境中一枝独秀,日常使用触及在所难免,尤其是对于喜欢下载资源的朋友来说,但是一些限制让它的使用越来越难,尤其是下载速度,普通用户的下载往往远低于自己的预期,特别是对于 M ...
- 找到多个与名为“Home”的控制器匹配的类型。
原因分析 其实上面已经讲的很清楚了,找到了两个同名Home控制器,需要配置命名空间来区分. 解决方法 方法一:修改RouteConfig.cs 方法二:修改RouteConfig.cs 和 Admin ...
- windows下mysql使用实录
之前密码忘了,卸载重装,配置好环境变量,登录,成功 操作命令可参考http://www.runoob.com/mysql/mysql-tutorial.html 这里只列举了我需要用到的命令 登录:m ...
- 【转】Asp.net MVC Comet推送
原文链接:http://www.cnblogs.com/kissdodog/p/4283485.html 一.简介 在Asp.net MVC实现的Comet推送的原理很简单. 服务器端:接收到服务器发 ...
- 九度oj 题目1077:最大序列和
题目1077:最大序列和 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6435 解决:1931 题目描述: 给出一个整数序列S,其中有N个数,定义其中一个非空连续子序列T中所有数的和为T ...