HDU5438--Ponds (拓排+BFS)
题意:给定n个池塘,m对池塘相连,现在要将与少于2个池塘相连的池塘拆除,形成森林,求节点数为奇数的树权值之和
思路:按照拓排的思想不断删除度数小于2的节点
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e4+5;
vector <ll> G[N];
queue <ll> Q;//拓排队列
queue <ll> P;//BFS队列
bool visit[N];//符合条件的点
ll value[N];
ll vis[N];//bfs遍历过的点
ll degree[N];//度数
ll m,n,ans;
void topsort()
{
while(!Q.empty()) Q.pop();
for(int i=1;i<=n;i++)
{
if(degree[i]<2)
{
if(degree[i]==1) Q.push(i);
visit[i]=1;
}
}
while(!Q.empty())
{
ll temp=Q.front();
Q.pop();
int len=G[temp].size();
for(int i=0;i<len;i++)
{
if(--degree[G[temp][i]]<2)
{
if(degree[G[temp][i]]==1) Q.push(G[temp][i]);
visit[G[temp][i]]=1;
}
}
}
}
ll bfs(int x)
{
ll sum=value[x],num=1;
P.push(x);
vis[x]=1;
while(!P.empty())
{
ll temp=P.front();
P.pop();
int len=G[temp].size();
for(int i=0;i<len;i++)
{
if(!vis[G[temp][i]]&&!visit[G[temp][i]])
{
P.push(G[temp][i]);
vis[G[temp][i]]=1;
num++;
sum+=value[G[temp][i]];
}
}
}
if(num&1) return sum;
return 0;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(vis,0,sizeof(vis));
memset(visit,0,sizeof(visit));
memset(degree,0,sizeof(degree));
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++)
scanf("%lld",&value[i]);
for(int i=1;i<=n;i++)
G[i].clear();
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
degree[x]++;
degree[y]++;
G[x].push_back(y);
G[y].push_back(x);
}
topsort();
ans=0;
for(int i=1;i<=n;i++)
{
if(!vis[i]&&!visit[i])
ans+=bfs(i);
}
printf("%lld\n",ans);
}
return 0;
}
HDU5438--Ponds (拓排+BFS)的更多相关文章
- codeforces 721C (拓排 + DP)
题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...
- HDU-2647 Reward ,逆拓排。
Reward 发工资,以前看过这题,做没做忘了(应该是没做). 很明显的拓排.但数据范围这么大,吓得我当时就不敢动手.后来找题解发现还是相当于两层循环(are you kidding me?)当时卡在 ...
- The Party and Sweets CodeForces - 1159C (拓排)
优化连边然后拓排. #include <iostream> #include <sstream> #include <algorithm> #include < ...
- hdu5438 Ponds[DFS,STL vector二维数组]
目录 题目地址 题干 代码和解释 参考 题目地址 hdu5438 题干 代码和解释 解答本题时参考了一篇代码较短的博客,比较有意思,使用了STL vector二维数组. 可以结合下面的示例代码理解: ...
- hdu5438 Ponds dfs 2015changchun网络赛
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- HDU5438:Ponds(拓扑排序)
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- hdu5438 Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submissi ...
- [poj2762] Going from u to v or from v to u?(Kosaraju缩点+拓排)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Going from u to v or from v to u? Tim ...
- uva12489 Combating cancer(树同构)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud https://uva.onlinejudge.org/index.php?opt ...
随机推荐
- python中urllib和urllib2的简单用法
import urllib #引入urllib模块,这里用urllib2也可以 fpage = urllib.urlopen( url ) #打开网页:例如url=‘http://www.xxx.co ...
- 图片填充UIImageView大小不对
http://www.2cto.com/kf/201507/412894.html UIView的contentMode属性: 默认为Scale To Fill,会保留view的比例,不会完全按照设定 ...
- Android中visibility属性VISIBLE、INVISIBLE、GONE的区别
详情见:http://blog.csdn.net/chindroid/article/details/8000713
- android 回调的理解(结合接口)
什么是回调 回调其实是一种双向调用模式,也就是调用方在接口被调用时也会调用对方的接口.通俗的解释为:类A调用了类B中的方法1,然后类B最后又反过来调用类A中的方法2,即把结果返回给类A. 回调的具体实 ...
- Windows 8 App: Information about CloudsCool Helper application
Website:http://www.cloudscool.com: App download address:http://dwz.cn/7DOJm: App English introduct ...
- JDE修改Grid列样式
Set Grid Color :to change the background color of a cell, row, column, or the entire control. Set Gr ...
- [转]初探Struts2.0
本文转自:http://blog.csdn.net/kgd1120/article/details/1667301 Struts作为MVC 2的Web框架,自推出以来不断受到开发者的追捧,得到用广泛的 ...
- 一些qml资料
qml开发ios应用 http://www.seanyxie.com/qt-qml%E7%A7%BB%E5%8A%A8%E5%BC%80%E5%8F%91%E4%B9%8B%E5%9C%A8ios%E ...
- vbox内部linux :centos5.5与外部ping通(相互),而且域名访问
1 相互ping通:不能使用nat,nat只能单向通,虚拟机不能ping通主机,选择桥接: 如图: 2然后设置 ip:最好设置静态ip这样下次不用再改,这里我们只演示使用eth0网卡,=> vi ...
- linux :TOP命令及参数解析
第二行 分别显示:total进程总数. running正在运行的进程数. sleeping睡眠的进程数.stopped停止的进程数. zombie僵尸进程数. 第三行 分别显示: %us 用户空间占用 ...