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 ...
随机推荐
- Java 变量及基本数据类型
1.Java变量 1.1 变量的概念 内存中开辟的一块存储空间,用于存放运算过程中需要用到的数据: 该区域有自己的名称(变量名)和类型(数据类型): 该区域的数据可以在同一类型范围内不断变化: 1) ...
- docker-java的使用
1. docker java 的api需要证书的认证 在/home/hett文件下创建certs证书 生成服务器私钥,命令如下: $openssl genrsa -out server-key.pem ...
- 【UML】用例图Use Case diagram(转)
http://blog.csdn.net/sds15732622190/article/details/48858219 前言 总结完UML概述,就该说道UML中的九种图了,这九种图中,最先要说的,就 ...
- event loop、进程和线程、任务队列
本文原链接:https://cloud.tencent.com/developer/article/1106531 https://cloud.tencent.com/developer/articl ...
- stixel-world跑在kitti数据集
kitti数据集中每一帧的Calibration不同,每一帧都存储了4个相机的Calibration http://ww.cvlibs.net/publications/Geiger2013IJRR. ...
- 使用notepad++远程编辑Linux文档
上一篇中,我写了如何使用使用ftp服务器实现很方便的通信,这一篇我分享一个使用notepad++的一个NPPFTP插件远程编辑Linux中的文档的小技巧. 首先要确保你的Linux的ftp服务已经打开 ...
- macbook secureCRT终端中文乱码的问题
最近mac用crt中文总是显示的是一串串问号, 而用自带的终端软件就不会出现乱码, 经过一番折腾暂时解决了这一问题, 方法如下: 1. 打开终端操作 sudo vim /etc/profile 在最后 ...
- cocos2x (c++/lua) spine 文件的预加载
在之前,笔者写过一编博客,通过lua在加载场景加载spineAnimation动画精灵,保存在table中,然后在游戏中创建动画精灵时,提取加载好的spineAnimaiton中的 spSkeleto ...
- C# 用qq邮箱发邮件
一.在企业的QQ邮箱中开启POP3/SMTP服务 开启服务时,授权密码保存好. 二.示例 public static string UserName = ""; // 企业邮箱 p ...
- 安装mysqlclient失败
环境:python3.6 sudo apt-get install python3.6-dev sudo apt-get install default-libmysqlclient-dev 参考:h ...