题目大意:给你一个无向图的顶点和边集,让你求图中连通分量的个数。使用并查集解决。

  1. #include <cstdio>
  2. #include <cstring>
  3. #define MAXN 30
  4.  
  5. bool G[MAXN][MAXN];
  6. int p[MAXN];
  7.  
  8. int find(int x)
  9. {
  10. return x == p[x] ? x : p[x]=find(p[x]);
  11. }
  12.  
  13. int main()
  14. {
  15. #ifdef LOCAL
  16. freopen("in", "r", stdin);
  17. #endif
  18. int T;
  19. scanf("%d", &T);
  20. getchar();
  21. char str[];
  22. gets(str);
  23. while (T--)
  24. {
  25. memset(G, , sizeof(G));
  26. gets(str);
  27. int n = str[] - 'A' + ;
  28. for (int i = ; i < n; i++)
  29. p[i] = i;
  30. int cnt = n;
  31. while (gets(str))
  32. {
  33. if (str[] == ) break;
  34. int a = str[] - 'A';
  35. int b = str[] - 'A';
  36. int pa = find(a);
  37. int pb = find(b);
  38. if (pa != pb)
  39. {
  40. p[pb] = pa;
  41. cnt--;
  42. }
  43. }
  44. printf("%d\n", cnt);
  45. if (T) printf("\n");
  46. }
  47. return ;
  48. }

UVa 459 - Graph Connectivity的更多相关文章

  1. UVa 10720 - Graph Construction(Havel-Hakimi定理)

    题目链接: 传送门 Graph Construction Time Limit: 3000MS     Memory Limit: 65536K Description Graph is a coll ...

  2. UVA 10720 Graph Construction 贪心+优先队列

    题目链接: 题目 Graph Construction Time limit: 3.000 seconds 问题描述 Graph is a collection of edges E and vert ...

  3. uva 193 Graph Coloring(图染色 dfs回溯)

    Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...

  4. UVA 193 Graph Coloring 图染色 DFS 数据

    题意:图上的点染色,给出的边的两个点不能都染成黑色,问最多可以染多少黑色. 很水的一题,用dfs回溯即可.先判断和当前点相连的点是否染成黑色,看这一点是否能染黑色,能染色就分染成黑色和白色两种情况递归 ...

  5. UVa 10720 - Graph Construction

    题目大意:给n个整数, 分别代表图中n个顶点的度,判断是否能构成一张图. 看到这个题后,除了所有数之和应该为偶数之外,没有别的想法了,只好在网上搜解题报告了.然后了解了Havel-Hakimi定理.之 ...

  6. UVA 1479 Graph and Queries (Treap)

    题意: 给一个无向图,再给一系列操作(以下3种),输出最后的平均查询结果. (1)D X 删除第x条边. (2)Q X k  查询与点X相连的连通分量中第k大的点的权值. (3)C X v  将点X的 ...

  7. 论文解读(g-U-Nets)《Graph U-Nets》

    论文信息 论文标题:Graph U-Nets论文作者:Hongyang Gao, Shuiwang Ji论文来源:2019,ICML论文地址:download 论文代码:download 1 Intr ...

  8. nvGRAPH API参考分析(二)

    nvGRAPH API参考分析(二) nvGRAPH Code Examples 本文提供了简单的示例. 1. nvGRAPH convert topology example void check( ...

  9. UVA Graph Coloring

    主题如以下: Graph Coloring  You are to write a program that tries to find an optimal coloring for agiven ...

随机推荐

  1. leveldb性能分析

    Leveldb是一个google实现的非常高效的kv数据库,目前的版本1.2能够支持billion级别的数据量了. 在这个数量级别下还有着非常高的性能,主要归功于它的良好的设计.特别是LSM算法. 那 ...

  2. 关于NIOS ii烧写的几种方式(转)

    源:http://www.cnblogs.com/bingoo/p/3450850.html 1. 方法一:.sof和.elf全部保存在FPGA内,程序加载和运行也是在FPGA内部. 把FPGA的配置 ...

  3. Java中的字符串分割 .

    转自 http://blog.csdn.net/yuwenhao0518/article/details/7161059 http://longkm.blog.163.com/blog/static/ ...

  4. IE6下最小19px像素

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

  5. PAT (Advanced Level) 1064. Complete Binary Search Tree (30)

    因为是要构造完全二叉树,所以树的形状已经确定了. 因此只要递归确定每个节点是多少即可. #include<cstdio> #include<cstring> #include& ...

  6. [其他]volatile 关键字

    用  volatile 关键字修饰函数 的作用是 告诉编译器该函数不会返回 , 让编译器能产生更好的代码 另外也能避免一些假警告信息,如未初始化的变量等

  7. FatMouse and Cheese 动态化搜索

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  8. oc js 交互

    http://blog.csdn.net/lwjok2007/article/details/47058101     iOS调JS http://blog.csdn.net/lwjok2007/ar ...

  9. openlayers画图形返回范围

    //画图形返回圖形的范围 var polygonLayer = new OpenLayers.Layer.Vector("选择范围"); var drawControls = ne ...

  10. 编写一个python脚本功能-备份

    版本一 解决方案当我们基本完成程序的设计,我们就可以编写代码了,它是对我们的解决方案的实施.版本一例10.1 备份脚本——版本一 #!/usr/bin/python # Filename: backu ...