Ponds

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1288    Accepted Submission(s): 429

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
 
本题首先要删掉度数小于2的点,这里需要注意一个问题就是如果删掉一个点后产生另一个度数为小于二的点仍然需要删除,直到没有可以删除的点为止;
这里用vecotr和queue实现起来更方便一些;
最后再求连通块点数的时候用dfs即可
#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
const int maxm=;
int val[maxn];
vector<int> g[maxm];
int ind[maxn];
bool vis[maxn];
void dfs(int u,long long &cnt,long long &temp){
vis[u]=true;
cnt++;
temp+=val[u];
for(int i=;i<g[u].size();i++){
int v=g[u][i];
if(vis[v])
continue; dfs(v,cnt,temp); }
} int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m;
memset(val,,sizeof(val));
memset(ind,,sizeof(ind));
memset(vis,false,sizeof(vis)); scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
g[i].clear();
scanf("%d",&val[i]);
}
int u,v;
for(int i=;i<=m;i++){
scanf("%d%d",&u,&v);
g[v].push_back(u);
g[u].push_back(v);
ind[u]++;
ind[v]++;
}
queue<int>q;
for(int i=;i<=n;i++ ){
if(ind[i]<){
q.push(i); }
}
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=true;
ind[u]=;
for(int i=;i<g[u].size();i++){
int v=g[u][i];
ind[v]--;
if(ind[v]<&&!vis[v]){
q.push(v); }
}
}
long long ans=,cnt=,temp=;//注意,这里必须用long long,long int会wa
for(int i=;i<=n;i++){
if(vis[i]==true) continue;
temp=;
cnt=;
dfs(i,cnt,temp);
if(cnt%==)
ans+=temp;
}
printf("%lld\n",ans);
}
return ;
}

2015长春网络赛1001 求连通快数量的问题dfs的更多相关文章

  1. HDU 4759 Poker Shuffle(2013长春网络赛1001题)

    Poker Shuffle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  2. hdu 5446(2015长春网络赛J题 Lucas定理+中国剩余定理)

    题意:M=p1*p2*...pk:求C(n,m)%M,pi小于10^5,n,m,M都是小于10^18. pi为质数 M不一定是质数 所以只能用Lucas定理求k次 C(n,m)%Pi最后会得到一个同余 ...

  3. Aggregated Counting-----hdu5439(2015 长春网络赛 找规律)

    #include<stdio.h> #include<string.h> #include<iostream> #include<math.h> #in ...

  4. 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.......,最 ...

  5. hdu 5441 (2015长春网络赛E题 带权并查集 )

    n个结点,m条边,权值是 从u到v所花的时间 ,每次询问会给一个时间,权值比 询问值小的边就可以走 从u到v 和从v到u算不同的两次 输出有多少种不同的走法(大概是这个意思吧)先把边的权值 从小到大排 ...

  6. Hdu 5445 Food Problem (2015长春网络赛 ACM/ICPC Asia Regional Changchun Online)

    题目链接: Hdu  5445 Food Problem 题目描述: 有n种甜点,每种都有三个属性(能量,空间,数目),有m辆卡车,每种都有是三个属性(空间,花费,数目).问至少运输p能量的甜点,花费 ...

  7. HDU 4738 Caocao's Bridges (2013杭州网络赛1001题,连通图,求桥)

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. 2015北京网络赛 D-The Celebration of Rabbits 动归+FWT

    2015北京网络赛 D-The Celebration of Rabbits 题意: 给定四个正整数n, m, L, R (1≤n,m,L,R≤1000). 设a为一个长度为2n+1的序列. 设f(x ...

  9. 2015北京网络赛 Couple Trees 倍增算法

    2015北京网络赛 Couple Trees 题意:两棵树,求不同树上两个节点的最近公共祖先 思路:比赛时看过的队伍不是很多,没有仔细想.今天补题才发现有个 倍增算法,自己竟然不知道.  解法来自 q ...

随机推荐

  1. JavaScript_9_循环

    1. JavaScript for/in 语句循环遍历对象的属性: 可以遍历数组,也可以遍历一个对象的所有属性 <body> <p>点击按钮,循环遍历对象“person”的属性 ...

  2. HDU 3652 B-number (数位DP,入门)

    题意: 如果一个整数能被13整除,且其含有子串13的,称为"B数",问[1,n]中有多少个B数? 思路: 这题不要用那个DFS的模板估计很快秒了. 状态设计为dp[位数][前缀][ ...

  3. 51nod 1191 消灭兔子

    题目来源: 2013腾讯马拉松赛第三场 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N只兔子,每只有一个血量B[i],需要用箭杀死免子.有M种不同类型的箭可以 ...

  4. 【UML】构件图Component diagram(实现图)(转)

    http://blog.csdn.net/sds15732622190/article/details/49048887 前言 下面要介绍UML中的构建图,它属于实现图的一种,五种静态图之一. 定义 ...

  5. Array - Container With Most Water

    /** * 此为暴力解法 * Find two lines, which together with x-axis forms a container, such that the container ...

  6. centos7 python3 Saltstack配置

    Python安装完毕后,提示找不到ssl模块 pip is configured with locations that require TLS/SSL, however the ssl module ...

  7. iOS深拷贝与浅拷贝

    概念 对象拷贝有两种方式:浅复制和深复制.顾名思义,浅复制,并不拷贝对象本身,仅仅是拷贝指向对象的指针:深复制是直接拷贝整个对象内存到另一块内存中. 如图详解:

  8. 【最大权闭合子图 最小割】bzoj1497: [NOI2006]最大获利

    最大权闭合子图的模型:今天才发现dinic板子是一直挂的…… Description 新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战.THU集团旗下的CS&T通讯公司在 ...

  9. PHP数组函数 array_multisort() ----对多个数组或多维数组进行排序

    PHP中array_multisort可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序. 关联(string)键名保持不变,但数字键名会被重新索引. 输入数组被当成一个表的列并以 ...

  10. 多线程之volatile关键字(五)

    开始全文之前,先铺垫一下jvm基础知识以及线程栈: JVM栈是线程私有的,每个线程创建的同时都会创建JVM栈,JVM栈中存放的为当前线程中局部基本类型的变量(java中定义的八种基本类型:boolea ...