hdu 5438(类似拓扑排序)
Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3178 Accepted Submission(s): 988
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<iostream>
#include<string.h>
#include <stdlib.h>
#include<math.h>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long LL;
const int N = ;
const int M = ;
struct Edge{
int v,next;
}edge[*M];
int head[N];
LL value[N];
int indegree[N];
int tot;
int vis[N];
queue<int> q;
void init(){
while(!q.empty()) q.pop();
memset(indegree,,sizeof(indegree));
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
tot=;
}
void addEdge(int u,int v,int &k){
edge[k].v = v,edge[k].next = head[u],head[u]=k++;
}
LL weight;
int cnt;
void dfs(int u){
vis[u]=;
cnt++;
weight+=value[u];
for(int k=head[u];k!=-;k=edge[k].next){
int v=edge[k].v;
if(!vis[v]){
dfs(v);
}
}
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--)
{
init();
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) {
scanf("%lld",&value[i]);
}
for(int i=;i<=m;i++){
int a,b;
scanf("%d%d",&a,&b);
indegree[a]++,indegree[b]++;
addEdge(a,b,tot);
addEdge(b,a,tot);
}
for(int i=;i<=n;i++){
if(indegree[i]==) q.push(i);
}
while(!q.empty()){
int u = q.front();
vis[u]=;
q.pop();
for(int k = head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
if(!vis[v]){
indegree[v]--;
indegree[u]--;
if(indegree[v]==) q.push(v);
}
}
}
LL res = ;
for(int i=;i<=n;i++){
weight=,cnt=;
if(!vis[i])
dfs(i);
if(cnt&&&cnt>){
res+=weight;
}
}
printf("%lld\n",res);
}
return ;
}
hdu 5438(类似拓扑排序)的更多相关文章
- hdu 5438 Ponds 拓扑排序
Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem ...
- HDU.2647 Reward(拓扑排序 TopSort)
HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...
- bfs+dfs乱搞+类似拓扑排序——cf1182D
代码不知道上了多少补丁..终于过了 用类似拓扑排序的办法收缩整棵树得到x,然后找到x直连的最远的和最近的点 只有这三个点可能是根,依次判一下即可 另外题解的第一种方法时找直径,然后判两端点+重心+所有 ...
- ACM: hdu 2647 Reward -拓扑排序
hdu 2647 Reward Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Des ...
- HDU 2647 Reward (拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...
- hdu 4857 逃生 拓扑排序+PQ,剥层分析
pid=4857">hdu4857 逃生 题目是求拓扑排序,但不是依照字典序最小输出,而是要使较小的数排在最前面. 一開始的错误思路:给每一个点确定一个优先级(该点所能到达的最小的点) ...
- HDU 1285 经典拓扑排序入门题
确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU 2647 逆向拓扑排序
令每一个员工都有一个自己的等级level[i] , 员工等级越高,那么工资越高,为了使发的钱尽可能少,所以每一级只增加一单位的钱 输入a b表示a等级高于b,那么我们反向添加边,令b—>a那么i ...
- HDU 4917 Permutation 拓扑排序的计数
题意: 一个有n个数的排列,给你一些位置上数字的大小关系.求合法的排列有多少种. 思路: 数字的大小关系可以看做是一条有向边,这样以每个位置当点,就可以把整个排列当做一张有向图.而且题目保证有解,所以 ...
随机推荐
- LaTeX中常用数学符号总结
博主一些小小的总结,以后会继续更的. 某个传送门. ⎝⎛•‿•⎞⎠⎝⎛•‿•⎞⎠⎝⎛•‿•⎞⎠ 1.左右一个$: 1+1=2 $1+1=2$ ($3$及以后的都需要$) 2.左右两个$: 1+1=2 ...
- charles 模拟手机弱网、修改请求参数、修改返回值
1.charles模拟弱网(断网) 2.charles修改请求参数 (1)先访问一次需要改的请求,在charles上找到相应的请求地址 (2)然后在需要打断点的请求上右键,勾选[Breakpoints ...
- Linux远程传输命令scp
指令:scp在不同的linux主机间复制文件带有Security的文件copy,基于ssh登录. 有些linux发行版没有自带scp,因此需要安装scp# yum -y install openssh ...
- ccf 201712-3 Crontab(Python实现)
一.原题 问题描述 试题编号: 201712-3 试题名称: Crontab 时间限制: 10.0s 内存限制: 256.0MB 问题描述: 样例输入 3 201711170032 201711222 ...
- 《零基础入门学习Python》【第一版】视频课后答案第002讲
测试题答案: 0. 什么是BIF?BIF 就是 Built-in Functions,内置函数.为了方便程序员快速编写脚本程序(脚本就是要编程速度快快快!!!),Python 提供了非常丰富的内置函数 ...
- poj-3009 curling2.0(搜索)
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26408 Accepted: 10546 Des ...
- ACM-ICPC 2018 徐州赛区网络预赛 B. BE, GE or NE
In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl named &qu ...
- Linux学习-Shell的变量功能
什么是变量? 简单的说,就是让某一个特定字串代表不固定的内容. 变量的可变性与方便性 举例来说,我们每个帐号的邮件信箱默认是以 MAIL 这个变量来进行存取的, 当 dmtsai 这个 使用者登陆时, ...
- 《鸟哥的Linux私房菜》学习笔记(3)——根文件系统
一.Linux目录结构 rootfs:根文件系统,根是"/". 1./boot 系统启动相关的文件,如内核.intrd.以及grub(bootloader) root@hao:~# ...
- 搭建基于金山快盘的Git服务器
最近迷上了Git,这货堪称神器,用了它就再也不想用其他VCS了,就像上了高速就不想再走国道一样. 一般人使用Git+Github来搭建进行本地远程交互,不过Github弄个私人仓库是要刀乐思的,如果你 ...