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 ...
随机推荐
- 理解Postgres性能
目录[-] 理解Postgres性能 理解缓存和缓存命中率 理解索引用途 Heroku Dashboard示例 索引缓存命中率 理解Postgres性能 对于很多应用程序开发人员来说数据库就是一个黑盒 ...
- last命令
last——列出目前与过去登入系统的用户信息 命令所在路径:/usr/bin/last 示例1: $ last
- 最新电脑公司最新GHOST WIN7系统32,64位极速安全版
系统来自系统妈:http://www.xitongma.com 电脑公司最新GHOST win7系统64位极速安全版 V2016年3月 系统简介 电脑公司ghost win7系统64位极速安全版集成了 ...
- UVA 12898 - And Or 与和或 (思路题)
思路就是有零一变化的位Or以后一定是1,And以后一定是0:那么如果b的二进制更长那么就把包含a的部分全部置为1或0,如果一样长那么就把不同的部分置为1或0. 今天被这题坑的地方:1默认是int,如果 ...
- OO第三次电梯作业优化
目录 第三次电梯作业个人优化 前言 优化思路 一.调度器 二.电梯 第三次电梯作业个人优化 前言 由于个人能力有限,第二次电梯作业只能完成正确性设计,没能进行优化,也因此损失了强测分数,于是第三次电梯 ...
- java基础——随机数问题
/** * 要求:随机打印50个随机(4-10长度)的字符串,要求字符串包含的范围是所有的英文字母包含大小写和数字, * 按照编码顺序排序,每行打印4个,要求首字符对齐 * @author fanyu ...
- 基于Nodejs的爬虫
简介 基于 Node.JS 爬取 博客园 1W+博文,对博文内容做关键词提取,生成词云. 演示 安装 安装 git.Node.JS.MongoDB.Yarn 克隆代码 git clone git@gi ...
- React支持装饰器
在用mobx时用到了装饰器,无奈环境不支持装饰器,搜索了半天,网上教程乱七八糟,最后想到了babel官网上肯定有,一搜果然有,安装教程 见Babel官网. 最快捷的教程是官网文档
- mysql 备份 常用脚本
全备: innobackupex --defaults-file=/data/mysql3316/my3316.cnf --user=root --password=mysqlpass /data/b ...
- PHP计算今天、昨天、本周、本月、上月开始时间和结束时间
PHP计算今天.昨天.本周.本月.上月开始时间和结束时间 $today = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d'),date('Y')) ...