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

 #include <cstdio>
#include <cstring>
#define MAXN 30 bool G[MAXN][MAXN];
int p[MAXN]; int find(int x)
{
return x == p[x] ? x : p[x]=find(p[x]);
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int T;
scanf("%d", &T);
getchar();
char str[];
gets(str);
while (T--)
{
memset(G, , sizeof(G));
gets(str);
int n = str[] - 'A' + ;
for (int i = ; i < n; i++)
p[i] = i;
int cnt = n;
while (gets(str))
{
if (str[] == ) break;
int a = str[] - 'A';
int b = str[] - 'A';
int pa = find(a);
int pb = find(b);
if (pa != pb)
{
p[pb] = pa;
cnt--;
}
}
printf("%d\n", cnt);
if (T) printf("\n");
}
return ;
}

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. android:editable is deprecated: Use an <EditText> to make it editable

    问题:android:editable is deprecated: Use an to make it editable   意思:Android的:编辑是反对:使用<</span> ...

  2. Zencart批量删除无图片产品

    Zencart批量删除无图片产品 2012-04-23 07:26:18|  分类: 默认分类 |字号 订阅 转自 http://zhongjia33.blog.163.com/blog/#m=0   ...

  3. 使用ReTrofit做缓存(结合上拉加载和下拉刷新)

    1. noCache 不使用缓存,全部走网络 2. noStore 不使用缓存,也不存储缓存 3. onlyIfCached 只使用缓存 4. maxAge 设置最大失效时间,失效则不使用 需要服务器 ...

  4. PHPstorm端口配置问题

  5. STL笔记之【map之总概】

    1.map和multimap内部数据结构: 红黑树(平衡二叉树的一种)2.在往map和multimap中插入元素时,会自动进行排序3.map和multimap的所有元素的key都被视为常数,其元素的实 ...

  6. tps,qps

    http://blog.itpub.net/22664653/viewspace-767265/

  7. zf-关于调用页面提示找不到className的原因

    多亏了蒋杰 还好他上次告诉我 关于节点的问题 我一看到这个函数就想到了他以前教我的    我这里一开始就调用js函数了 所以没获取到节点    后来把方法换到这里就OK了    

  8. Binary Watch

    Binary Watch 描述 Consider a binary watch with 5 binary digits to display hours (00 - 23) and 6 binary ...

  9. Block 再学习 !

    如何优雅的使用 Block? How Do I Declare A Block in Objective-C? 阮一峰的一句话解释简洁明了:闭包就是能够读取其它函数内部变量的函数 详情:http:// ...

  10. Codevs 5590 A+B 问题 超级版

    5590 A+B 问题 超级版 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 青铜 Bronze 题目描述 Description 不用+-*/%计算A+B 输入描述 Input De ...