HDU 5438 Ponds (DFS,并查集)
题意:给定一个图,然后让你把边数为1的结点删除,然后求连通块结点数为奇的权值和。
析:这个题要注意,如果删除一些结点后,又形成了新的边数为1的结点,也应该要删除,这是坑,其他的,先用并查集判一下环,然后再找连通环。
代码如下:
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e4 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int p[maxn];
int a[maxn];
int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); }
int vis[maxn];
vector<int> vv;
vector<int> G[maxn]; bool dfs(int u, int fa){
if(vis[u]) return true;
if(!G[u].size()) return false;
bool ok = false; vis[u] = 1;
for(int i = 0; i < G[u].size(); ++i){
int v = G[u][i]; if(v == fa) continue; if(dfs(v, u)) ok = true;
} if(ok) vv.push_back(u);
return ok;
} int main(){
int T;
cin >> T;
while(T--){
scanf("%d %d", &n, &m);
for(int i = 0; i <= n; ++i) p[i] = i;
for(int i = 1; i <= n; ++i){ scanf("%d", &a[i]); G[i].clear(); } memset(vis, 0, sizeof(vis));
vector<int> v;
// memset(in, 0, sizeof(in));
int u, w;
for(int i = 0; i < m; ++i){
scanf("%d %d", &u, &w);
int x = Find(u);
int y = Find(w);
G[u].push_back(w);
G[w].push_back(u);
// ++in[u];
// ++in[w];
if(x != y) p[y] = x;
else v.push_back(u);
} LL ans = 0;
sort(v.begin(), v.end());
for(int i = 0; i < v.size(); ++i){
if(i && v[i] == v[i-1]) continue;
vv.clear();
dfs(v[i], -1);
if(vv.size() & 1){
for(int j = 0; j < vv.size(); ++j)
ans += a[vv[j]], a[vv[j]] = 0;
}
} cout << ans << endl;
}
return 0;
} /*
1
4 4
1 2 3 4
1 2
1 3
2 3
1 4 */
HDU 5438 Ponds (DFS,并查集)的更多相关文章
- hdu 5438 Ponds dfs
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descr ...
- HDU 5438 Ponds dfs模拟
2015 ACM/ICPC Asia Regional Changchun Online 题意:n个池塘,删掉度数小于2的池塘,输出池塘数为奇数的连通块的池塘容量之和. 思路:两个dfs模拟就行了 # ...
- hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)
hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...
- HDU 1811 拓扑排序 并查集
有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...
- HDU - 5438 Ponds(拓扑排序删点+并查集判断连通分量)
题目: 给出一个无向图,将图中度数小于等于1的点删掉,并删掉与他相连的点,直到不能在删为止,然后判断图中的各个连通分量,如果这个连通分量里边的点的个数是奇数,就把这些点的权值求和. 思路: 先用拓扑排 ...
- hdu 1198 Farm Irrigation(深搜dfs || 并查集)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...
- HDU 6370 dfs+并查集
Werewolf Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- Hdu 5458 Stability (LCA + 并查集 + 树状数组 + 缩点)
题目链接: Hdu 5458 Stability 题目描述: 给出一个还有环和重边的图G,对图G有两种操作: 1 u v, 删除u与v之间的一天边 (保证这个边一定存在) 2 u v, 查询u到v的路 ...
- HDU 5441 离线处理 + 并查集
题意:给n个节点m条带权值边的无向图.然后q个问题,每次询问点对的数目,点对需要满足的条件是:1)连通:2)其路径的最大权值不能超过询问值. 分析:如果没次询问一次,dfs一次,很可能超时,因此可以用 ...
随机推荐
- UVa 11090 Going in Cycle!!【Bellman_Ford】
题意:给出n个点m条边的加权有向图,求平均值最小的回路 自己想的是用DFS找环(真是too young),在比较找到各个环的平均权值,可是代码实现不了,觉得又不太对 后来看书= =好巧妙的办法, 使用 ...
- xcode安装app
安装 xcode 安装 xcode command line tool 检查是否安装 在终端中运行: xcrun simctl list 如果出现所有的 Device Types,则可以进行第3步 如 ...
- 06day2
蠕虫游戏 模拟 [问题描述] 蠕虫是一个古老的电脑游戏,它有许多版本.但所有版本都有一个共同规则:操纵一条蠕虫在屏幕上转圈,并试着去避免撞到自己或障碍物. 这里我们将模拟一个简单的版本.游戏将在 50 ...
- python练习程序(c100经典例19)
题目: 一个数如果恰好等于它的因子之和,这个数就称为“完数”.例如6=1+2+3.编程找出1000以内的所有完数. def foo(a): sra=a; lis=[1]; while 1: for i ...
- 【JavaScript学习笔记】鼠标样式
style="cursor:hand" 手形 style="cursor:crosshair" 十字形 style="cursor ...
- <Liunx常用命令一>之TOP
一:作用 ----->TOP命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况. ----->TOP是一个动态显示过程,即可以通过用 ...
- Windows 和 Linux 的IPC API对应表
原文出处:http://blog.csdn.net/zhengdy/article/details/5485472 ...
- Auto Updating the exe from a network location when application starts z
http://www.codeproject.com/Tips/869588/Auto-Updating-the-exe-from-a-network-location-when?msg=499218 ...
- IntelliJ IDEA svn 提交错误
环境说明: 系统:Mac OS X 10.9 以及 10.10 系统设置:LANG=zh_CN.UTF-8 svn 客户端:1.8.10 IntelliJ IDEA 13 毫无疑问,IntelliJ ...
- 【python】python+behave自动化
留坑,后面再写,先写下request对http请求的校验.