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一次,很可能超时,因此可以用 ...
随机推荐
- BOM浏览器对象
BOM 浏览器对象 一.浏览器本身就自己有一些对象,不用创建就可以使用 window(当前浏览器窗体) 属性: status opener closed parent top 方法: alert(); ...
- EL&struts2标签 读取map,list集合
struts中的取map和list & jsp中取map和list <% List list = new ArrayList(); list.add("a"); li ...
- NTP时间服务器配置与解析
NTP时间服务器配置与解析 Edit By ZhenXing_Yu 目 录 编译安装ntp server 2 修改ntp.conf配置文件 2 配置时间同步客户机 2 在服务端验证: 3 在客户端进行 ...
- openssl安装
www.openssl.orgconfigure the environment:<pre lang="bash" escaped="true">t ...
- RMAN duplicate from active遇到ora-17167,ora-12154
最近在从活动数据库进行异机克隆时碰到了ORA-17629,ORA-17627,ORA-12154的错误,起初以为是一个Bug呢.Oracle Bug着实太多了,已经成了习惯性思维了.汗!错误提示是无法 ...
- ON、WHERE、HAVING的区别
ON .WHERE.HAVING都能通过限制条件筛选数据,但他们的使用及其不同.下面我们来分析三者之间的区别. 1. ON 和WHERE 所有的查询都回产生一个中间临时报表,查询结果就是从 ...
- Mysql避免全表扫描sql查询优化 .
对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引: .尝试下面的技巧以避免优化器错选了表扫描: · 使用ANALYZE TABLE tbl_n ...
- html:唤起手机qq开始对话 & 自动拨号
<a href="mqqwpa://im/chat?chat_type=wpa&uin=[qq号]&version=1">XXX</a> 另 ...
- Arduino命令行编译 树莓派连接Arduino 电脑上编译Arduino代码后 通过树莓派烧写到Arduino上
//本教程针对UNO 1.在file->preferences中找到preferences.txt文件 2:用记事本打开preferences.txt,选择hex文件存放的路径,在最后行加入 b ...
- gcc-4.8.3安装,gdb-7.6安装
gdb用法: http://blog.chinaunix.net/uid-26548237-id-3435525.html gdb-7.6.tar.gz: (官网下载:http://ftp.gnu. ...