hdu 5438 Ponds 拓扑排序
Ponds
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1001&cid=621
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
HINT
题意
给你一个图,然后要求把度数小于2的点全部删去,然后问你奇数集合的点权和是多少
注意,你删去了一个点之后,可能会使得一些点又变成了度数小于2的
题解:
用类似拓扑排序的思想去做就ok啦
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue> using namespace std; const int N=;
long long a[N],ans;
int n,m,T,cnt,ok[N],vis[N],pre[N],nxt[N],to[N],tot[N],col;
vector<int> s[N];
queue<int> q; void dfs(int x,int fa)
{
s[col].push_back(x);
ok[x]=;
for(int p=pre[x];p!=-;p=nxt[p])
{
if((!vis[p])||(!ok[to[p]])) continue;
if(p==(fa^)) continue;
dfs(to[p],p);
}
}
void makeedge(int x,int y)
{
to[cnt]=y;nxt[cnt]=pre[x];pre[x]=cnt++;
to[cnt]=x;nxt[cnt]=pre[y];pre[y]=cnt++;
} int main()
{
scanf("%d",&T);
while(T--)
{
memset(tot,,sizeof(tot));
memset(pre,-,sizeof(pre));
memset(ok,,sizeof(ok));
memset(vis,,sizeof(vis));
ans=0LL;cnt=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%I64d",&a[i]);
}
for(int i=;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
tot[x]++;tot[y]++;
makeedge(x,y);
}
while(!q.empty()) q.pop();
for(int i=;i<=n;i++)
{
if(tot[i]<)
{
q.push(i);
}
}
while(!q.empty())
{
int x=q.front();
q.pop();
ok[x]=;
for(int p=pre[x];p!=-;p=nxt[p])
{
vis[p]=;
tot[x]--;
tot[to[p]]--;
if(ok[to[p]]&&tot[to[p]]<)
{
q.push(to[p]);
}
}
}
col=;
for(int i=;i<=n;i++)
{
col++;
s[col].clear();
if(ok[i])
{
dfs(i,cnt+);
if(s[col].size()%==)
{
for(int j=;j<s[col].size();j++)
{
ans+=a[s[col][j]];
}
}
}
}
printf("%I64d\n",ans);
}
return ;
}
hdu 5438 Ponds 拓扑排序的更多相关文章
- hdu 5438(类似拓扑排序)
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- HDU.2647 Reward(拓扑排序 TopSort)
HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...
- HDU - 5438 Ponds(拓扑排序删点+并查集判断连通分量)
题目: 给出一个无向图,将图中度数小于等于1的点删掉,并删掉与他相连的点,直到不能在删为止,然后判断图中的各个连通分量,如果这个连通分量里边的点的个数是奇数,就把这些点的权值求和. 思路: 先用拓扑排 ...
- hdu 5438 Ponds(长春网络赛 拓扑+bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others) ...
- 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,依次计算... ...
- HDU5438:Ponds(拓扑排序)
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- HDU 5438 Ponds
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- hdu 4857 逃生 拓扑排序+PQ,剥层分析
pid=4857">hdu4857 逃生 题目是求拓扑排序,但不是依照字典序最小输出,而是要使较小的数排在最前面. 一開始的错误思路:给每一个点确定一个优先级(该点所能到达的最小的点) ...
随机推荐
- Hadoop安装教程_单机/伪分布式配置_Hadoop2.6.0/Ubuntu14.04
摘自: http://www.cnblogs.com/kinglau/p/3796164.html http://www.powerxing.com/install-hadoop/ 当开始着手实践 H ...
- kali获得已经安装的软件列表
有人说是用dpkg -l ,也有说是用dpkg --get-selections. debian:~# dpkg -l|grep install|wc -l2678debian:~# dpkg --g ...
- jQgrid问题总结
最近一段时间一直在使用jqgrid这个免费的插件,网上的资料也比较多.比较全,但是这里还是整理几个自己在开发过程中遇到的小问题. 1.自动换行 一行数据过多需要自动根据内容换行时,如果遇到在表格中的汉 ...
- Repeater的ItemCommand事件(LinkButton)
Repeater的ItemCommand事件,就是在里面加一个超链接的按钮,所有按钮都指向同一个事件,就是ItemCommand事件. 至于如何区分是点击的什么按钮,还有传的值,需要用到LinkBut ...
- poj2828
很容易想到一种动态的做法:平衡树…… 或者是二分+树状数组 但,前者编程复杂度较大,而且据说会被卡(没试过):后者理论上超时(据说可以擦边过?): 所以要尝试新的算法: 倒着考虑,显然最后一个对象的位 ...
- UVa 1599 (字典序最小的最短路) Ideal Path
题意: 给出一个图(图中可能含平行边,可能含环),每条边有一个颜色标号.在从节点1到节点n的最短路的前提下,找到一条字典序最小的路径. 分析: 首先从节点n到节点1倒着BFS一次,算出每个节点到节点n ...
- Ehcache和MemCached比较分析
项目 Memcache Ehcache 分布式 不完全,集群默认不实现 支持 集群 可通过客户端实现 支持(默认是异步同步) 持久化 可通过第三方应用实现,如sina研发的memcachedb,将ca ...
- InnoDB关键特性之doublewrite
部分写失效 想象这么一个场景,当数据库正在从内存向磁盘写一个数据页时,数据库宕机,从而导致这个页只写了部分数据,这就是部分写失效,它会导致数据丢失.这时是无法通过重做日志恢复的,因为重做日志记录的是对 ...
- 与非CCR代码互操作
导读:CCR可以轻松的承载STA组件或者与它互操作:组件应该创建一个只有一个线程的CCR Dispatcher实例,并且在Dispatcher的构造函数中指定线程套间策略.DispatcherQueu ...
- 018如何建立自动化框架 how to bulid the framwork
本讲包括: 一. objective 二. How to bulid 三. Keyview of frawork (关键视图) 四. conclusion automation framwork:自动 ...