HDU - 5438 Ponds(拓扑排序删点+并查集判断连通分量)
题目:
给出一个无向图,将图中度数小于等于1的点删掉,并删掉与他相连的点,直到不能在删为止,然后判断图中的各个连通分量,如果这个连通分量里边的点的个数是奇数,就把这些点的权值求和。
思路:
先用拓扑排序删点并更新各个点的度数,然后用并查集判断各个连通分量里边的点个数的奇偶性就ok了。
代码:
- #include <bits/stdc++.h>
- #include <cstdio>
- #include <cstring>
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <queue>
- #include <iomanip>
- #define MAX 1000000000
- #define inf 0x3f3f3f3f
- #define FRE() freopen("in.txt","r",stdin)
- using namespace std;
- typedef long long ll;
- const int maxn = ;
- int degree[maxn],fa[maxn],cnt[maxn],vis[maxn];
- int p,m;
- ll val[maxn],sum[maxn];
- vector<int> mp[maxn];
- void init()
- {
- memset(cnt,,sizeof(cnt));
- memset(degree,,sizeof(degree));
- memset(sum,,sizeof(sum));
- memset(vis,,sizeof(vis));
- for(int i=; i<maxn; i++)
- {
- fa[i] = i;
- mp[i].clear();
- }
- }
- int _find(int x)
- {
- return fa[x]==x?x:fa[x] = _find(fa[x]);
- }
- void check()
- {
- for(int i=; i<=p; i++)
- {
- printf("%d ",degree[i]);
- }
- printf("\n");
- }
- int main()
- {
- //FRE();
- int kase;
- scanf("%d",&kase);
- while(kase--)
- {
- init();
- scanf("%d%d",&p,&m);
- for(int i=; i<=p; i++)
- {
- scanf("%lld",&val[i]);
- }
- for(int i=; i<m; i++)
- {
- int u,v;
- scanf("%d%d",&u,&v);
- degree[u]++;
- degree[v]++;
- mp[u].push_back(v);
- mp[v].push_back(u);
- u = _find(u);//将同一个连通分量里边的点连接
- v = _find(v);
- if(u!=v)
- fa[u] = v;
- }
- //check();
- queue<int> que;
- for(int i=; i<=p; i++)
- {
- if(degree[i]<=)
- que.push(i);
- }
- while(!que.empty())//利用拓扑排序删点
- {
- int u = que.front(); que.pop();
- //cout<<u<<endl;
- vis[u] = ;
- for(int i=; i<mp[u].size(); i++)
- {
- degree[mp[u][i]]--;
- if(!vis[mp[u][i]] && degree[mp[u][i]]<=)
- que.push(mp[u][i]);
- }
- }
- //check();
- ll ans = ;
- for(int i=; i<=p; i++)
- {
- if(degree[i]<=) continue;
- int r = _find(i);
- cnt[r]++;//统计该连通分量里边的点的个数
- sum[r] += val[i];//该连通分量的权值的和
- }
- for(int i=; i<=p; i++)
- {
- if(cnt[i]%)
- {
- ans += sum[i];
- }
- }
- printf("%lld\n",ans);
- }
- return ;
- }
HDU - 5438 Ponds(拓扑排序删点+并查集判断连通分量)的更多相关文章
- hdu 5438 Ponds 拓扑排序
Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem ...
- HDU 1811 Rank of Tetris 【拓扑排序】+【并查集】
<题目链接> 题目大意: 给你N个点(编号从0到N-1)和M个关系,要你判断这个图的所有点的顺序是否可以全部确定.不过对于任意点的关系可能存在A>B或A<B或A=B三种情况,如 ...
- HDU1811 拓扑排序判环+并查集
HDU Rank of Tetris 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1811 题意:中文问题就不解释题意了. 这道题其实就是一个拓扑排序判圈 ...
- 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(长春网络赛 拓扑+bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others) ...
- HDU5438:Ponds(拓扑排序)
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- 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,依次计算... ...
随机推荐
- IOS高级开发~Runtime(一)
#import <Foundation/Foundation.h> @interface CustomClass : NSObject -(void)fun1; @end @interfa ...
- nginx重写模块
参考:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html 1 语法 Syntax: if (condition) { ... } De ...
- Codeforces Round #402 (Div. 2) D
Description Little Nastya has a hobby, she likes to remove some letters from word, to obtain another ...
- Random Query CodeForces - 846F
题目 翻译: 给出一个n个数字的数列a[1],...,a[n],f(l,r)表示使a[l],a[l+1],...,a[r]组成的新序列中的重复元素只保留一个后,剩下元素的数量(如果l>r,则在计 ...
- h5-26-web本地存储
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【转载】(0, eval)(‘this’)
var window = this || (0, eval)('this') 在avalon源码中有这么一行代码,var window = this很容易理解 这里复习一下Global Object: ...
- CentOS 7.2安装pip
CentOS 7.2默认安装的python版本为python2.7.5,我的系统里面默认是没有安装pip 的,搜了下网上各路大侠的解决办法,如下: 使用yum安装python-pip,但是报错,说没有 ...
- 项目错误提示Multiple markers at this line
新安装个Myeclipse,导入以前做的程序后程序里好多错,第一行提示: Multiple markers at this line - The type java.lang.Obje ...
- js去掉数组的空字符串
后台返回数据的时候,有些数据为空时,一般都不进行显示,需要去除空字符串. 基本思路:获取数组张度,遍历数组,当数组某个值等于‘’或null或数据类型为undefined时,根据splice方法去除数据 ...
- AJPFX总结方法重载与方法重写的区别
方法重载在同一个类中,可以出现同名方法,但是这些同名方法的参数列表必须不同,这样定义方法叫做方法重载.方法重载的特点重载的注意事项重载与返回值无关重载与具体的变量标识符无关重载只与方法名与参数相关重载 ...