题目:

给出一个无向图,将图中度数小于等于1的点删掉,并删掉与他相连的点,直到不能在删为止,然后判断图中的各个连通分量,如果这个连通分量里边的点的个数是奇数,就把这些点的权值求和。

思路:

先用拓扑排序删点并更新各个点的度数,然后用并查集判断各个连通分量里边的点个数的奇偶性就ok了。

代码:

  1. #include <bits/stdc++.h>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <queue>
  8. #include <iomanip>
  9. #define MAX 1000000000
  10. #define inf 0x3f3f3f3f
  11. #define FRE() freopen("in.txt","r",stdin)
  12.  
  13. using namespace std;
  14. typedef long long ll;
  15. const int maxn = ;
  16. int degree[maxn],fa[maxn],cnt[maxn],vis[maxn];
  17. int p,m;
  18. ll val[maxn],sum[maxn];
  19. vector<int> mp[maxn];
  20.  
  21. void init()
  22. {
  23. memset(cnt,,sizeof(cnt));
  24. memset(degree,,sizeof(degree));
  25. memset(sum,,sizeof(sum));
  26. memset(vis,,sizeof(vis));
  27. for(int i=; i<maxn; i++)
  28. {
  29. fa[i] = i;
  30. mp[i].clear();
  31. }
  32. }
  33.  
  34. int _find(int x)
  35. {
  36. return fa[x]==x?x:fa[x] = _find(fa[x]);
  37. }
  38.  
  39. void check()
  40. {
  41. for(int i=; i<=p; i++)
  42. {
  43. printf("%d ",degree[i]);
  44. }
  45. printf("\n");
  46. }
  47.  
  48. int main()
  49. {
  50. //FRE();
  51. int kase;
  52. scanf("%d",&kase);
  53. while(kase--)
  54. {
  55. init();
  56. scanf("%d%d",&p,&m);
  57. for(int i=; i<=p; i++)
  58. {
  59. scanf("%lld",&val[i]);
  60. }
  61. for(int i=; i<m; i++)
  62. {
  63. int u,v;
  64. scanf("%d%d",&u,&v);
  65. degree[u]++;
  66. degree[v]++;
  67. mp[u].push_back(v);
  68. mp[v].push_back(u);
  69. u = _find(u);//将同一个连通分量里边的点连接
  70. v = _find(v);
  71. if(u!=v)
  72. fa[u] = v;
  73. }
  74. //check();
  75. queue<int> que;
  76. for(int i=; i<=p; i++)
  77. {
  78. if(degree[i]<=)
  79. que.push(i);
  80. }
  81. while(!que.empty())//利用拓扑排序删点
  82. {
  83.  
  84. int u = que.front(); que.pop();
  85. //cout<<u<<endl;
  86. vis[u] = ;
  87. for(int i=; i<mp[u].size(); i++)
  88. {
  89. degree[mp[u][i]]--;
  90. if(!vis[mp[u][i]] && degree[mp[u][i]]<=)
  91. que.push(mp[u][i]);
  92. }
  93. }
  94. //check();
  95. ll ans = ;
  96. for(int i=; i<=p; i++)
  97. {
  98. if(degree[i]<=) continue;
  99. int r = _find(i);
  100. cnt[r]++;//统计该连通分量里边的点的个数
  101. sum[r] += val[i];//该连通分量的权值的和
  102. }
  103. for(int i=; i<=p; i++)
  104. {
  105. if(cnt[i]%)
  106. {
  107. ans += sum[i];
  108. }
  109. }
  110. printf("%lld\n",ans);
  111. }
  112. return ;
  113. }

HDU - 5438 Ponds(拓扑排序删点+并查集判断连通分量)的更多相关文章

  1. hdu 5438 Ponds 拓扑排序

    Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem ...

  2. HDU 1811 Rank of Tetris 【拓扑排序】+【并查集】

    <题目链接> 题目大意: 给你N个点(编号从0到N-1)和M个关系,要你判断这个图的所有点的顺序是否可以全部确定.不过对于任意点的关系可能存在A>B或A<B或A=B三种情况,如 ...

  3. HDU1811 拓扑排序判环+并查集

    HDU Rank of Tetris 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1811 题意:中文问题就不解释题意了. 这道题其实就是一个拓扑排序判圈 ...

  4. hdu 5438(类似拓扑排序)

    Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

  5. HDU.2647 Reward(拓扑排序 TopSort)

    HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...

  6. hdu 5438 Ponds(长春网络赛 拓扑+bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others)     ...

  7. HDU5438:Ponds(拓扑排序)

    Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

  8. ACM: hdu 2647 Reward -拓扑排序

    hdu 2647 Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Des ...

  9. HDU 2647 Reward (拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...

随机推荐

  1. IOS高级开发~Runtime(一)

    #import <Foundation/Foundation.h> @interface CustomClass : NSObject -(void)fun1; @end @interfa ...

  2. nginx重写模块

    参考:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html 1 语法 Syntax: if (condition) { ... } De ...

  3. Codeforces Round #402 (Div. 2) D

    Description Little Nastya has a hobby, she likes to remove some letters from word, to obtain another ...

  4. Random Query CodeForces - 846F

    题目 翻译: 给出一个n个数字的数列a[1],...,a[n],f(l,r)表示使a[l],a[l+1],...,a[r]组成的新序列中的重复元素只保留一个后,剩下元素的数量(如果l>r,则在计 ...

  5. h5-26-web本地存储

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. 【转载】(0, eval)(‘this’)

    var window = this || (0, eval)('this') 在avalon源码中有这么一行代码,var window = this很容易理解 这里复习一下Global Object: ...

  7. CentOS 7.2安装pip

    CentOS 7.2默认安装的python版本为python2.7.5,我的系统里面默认是没有安装pip 的,搜了下网上各路大侠的解决办法,如下: 使用yum安装python-pip,但是报错,说没有 ...

  8. 项目错误提示Multiple markers at this line

    新安装个Myeclipse,导入以前做的程序后程序里好多错,第一行提示: Multiple markers at this line         - The type java.lang.Obje ...

  9. js去掉数组的空字符串

    后台返回数据的时候,有些数据为空时,一般都不进行显示,需要去除空字符串. 基本思路:获取数组张度,遍历数组,当数组某个值等于‘’或null或数据类型为undefined时,根据splice方法去除数据 ...

  10. AJPFX总结方法重载与方法重写的区别

    方法重载在同一个类中,可以出现同名方法,但是这些同名方法的参数列表必须不同,这样定义方法叫做方法重载.方法重载的特点重载的注意事项重载与返回值无关重载与具体的变量标识符无关重载只与方法名与参数相关重载 ...