2015长春网络赛1001 求连通快数量的问题dfs
Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1288 Accepted Submission(s): 429
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
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.
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.
7 7
1 2 3 4 5 6 7
1 4
1 5
4 5
2 3
2 6
3 6
2 7
#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的更多相关文章
- HDU 4759 Poker Shuffle(2013长春网络赛1001题)
Poker Shuffle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- 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最后会得到一个同余 ...
- Aggregated Counting-----hdu5439(2015 长春网络赛 找规律)
#include<stdio.h> #include<string.h> #include<iostream> #include<math.h> #in ...
- 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.......,最 ...
- hdu 5441 (2015长春网络赛E题 带权并查集 )
n个结点,m条边,权值是 从u到v所花的时间 ,每次询问会给一个时间,权值比 询问值小的边就可以走 从u到v 和从v到u算不同的两次 输出有多少种不同的走法(大概是这个意思吧)先把边的权值 从小到大排 ...
- Hdu 5445 Food Problem (2015长春网络赛 ACM/ICPC Asia Regional Changchun Online)
题目链接: Hdu 5445 Food Problem 题目描述: 有n种甜点,每种都有三个属性(能量,空间,数目),有m辆卡车,每种都有是三个属性(空间,花费,数目).问至少运输p能量的甜点,花费 ...
- HDU 4738 Caocao's Bridges (2013杭州网络赛1001题,连通图,求桥)
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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 ...
- 2015北京网络赛 Couple Trees 倍增算法
2015北京网络赛 Couple Trees 题意:两棵树,求不同树上两个节点的最近公共祖先 思路:比赛时看过的队伍不是很多,没有仔细想.今天补题才发现有个 倍增算法,自己竟然不知道. 解法来自 q ...
随机推荐
- 用NPOI操作EXCEL-锁定列CreateFreezePane()
public void ExportPermissionRoleData(string search, int roleStatus) { var workbook = new HSSFWorkboo ...
- 11gR2 如何诊断节点重启问题
本文对如何诊断11gR2 GI环境下的节点重启问题进行了一些介绍. 首先,像10g版本一样,我们首先介绍在GI中能够导致节点重启的进程.1.Ocssd.bin:这个进程的功能和10g版本的功能基本差不 ...
- uva10129 PlayOnWords(并查集,欧拉回路)
判断无向图是否存在欧拉回路,就是看度数为奇数的点有多少个,如果有两个,那么以那分别两个点为起点和终点,可以构造出一条欧拉回路,如果没有,就任意来,否则,欧拉回路不存在. 首先用并查集判断连通,然后统计 ...
- OO作业第三单元总结
目录 一.JML语言理论基础及应用工具链 二.部署JMLUnitNG,自动生成测试用例 三.架构设计 第一次作业 第二次作业 第三次作业 四.Bug分析 五.心得体会 一.JML语言理论基础及应用工具 ...
- JavaScript深入浅出第2课:函数是一等公民是什么意思呢?
摘要: 听起来很炫酷的一等公民是啥? <JavaScript深入浅出>系列: JavaScript深入浅出第1课:箭头函数中的this究竟是什么鬼? JavaScript深入浅出第2课:函 ...
- 说说TCP的三次握手
在说这个问题之前,先说说IP协议和TCP协议 问题:IP协议能做什么?不能做什么? 我们都知道IP协议是无连接的通信协议,它不会占用两个正在通信的计算机的通信线路,这样就降低了IP对网络传输中的需求, ...
- String中关于BeanFactory
org.springframework.beans及org.springframework.context包是Spring IoC容器的基础.BeanFactory提供的高级配置机制,使得管理任何性质 ...
- MVCPager学习小记
1.PageIndexParameterName怎么关联? 答:其实就是Action里面的pageindex参数 例子: @Html.Pager(Model, new PagerOptions { P ...
- Yii2应用的运行过程
每一个框架都有一个入口脚本,Yii2也不例外.一般来说,对于Web应用的入口脚本是YiiBasePath/frontend/web目录下的index.php. 先观察这个文件: <?php de ...
- 【Git版本控制】GitLab Fork项目的工作流程
转载自简书: GitLab Fork项目工作流程