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. Autoit3脚本编写举例

    以任务管理器为例 1.首先打开任务管理器 2.点击结束任务操作 第一步打开任务管理器 run("C:\WINDOWS\system32\taskmgr.exe"); 第二步点击结束 ...

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

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

  3. python基础教程总结7——异常

    1.Python异常类 Python是面向对象语言,所以程序抛出的异常也是类.常见的Python异常有: 异常 描述 NameError 尝试访问一个没有申明的变量 ZeroDivisionError ...

  4. ES6, Angular,React和ABAP中的String Template(字符串模板)

    String Template(字符串模板)在很多编程语言和框架中都支持,是一个很有用的特性.本文将Jerry工作中使用到的String Template的特性做一个总结. ES6 阮一峰老师有一个专 ...

  5. UI EventSystem事件监听

    Unity5.0 EventSystem事件系统的详细说明 一.EventSystem对象的说明 当我们在场景中创建任一UI对象后,Hierarchy面板中都可以看到系统自动创建了对象EventSys ...

  6. codeforecs Gym 100286B Blind Walk

    交互式程序,要用到一个函数fflush,它的作用是对标准输出流的清理,对stdout来说是及时地打印数据到屏幕上,一个事实:标准输出是以『行』为单位进行的,也即碰到\n才打印数据到屏幕.这就可能造成延 ...

  7. pycharm 使用技巧

    格式化代码为pep8: ctrl+alt+l http://edu.51cto.com//index.php?do=lession&id=163794

  8. C#MySQL增删改查

    首先在项目中添加引用 using MySql.Data.MySqlClient; 连接字符串  private string connString="server=localhost;use ...

  9. TypeError: Cannot read property 'tap' of undefined

    E:\vue-project\vue-element-admin-master>npm run build:prod vue-element-admin@3.8.1 build:prod E:\ ...

  10. 二十一、C++中的临时对象

    思考: 构造函数是一个特殊的函数 是否可以直接调用? 是否可以在构造函数中调用构造函数? 直接调用构造函数的行为是什么? 答: 直接调用构造函数将产生一个临时对象 临时对象的生命周期只有一条语句的时间 ...