Uva 459 Graph Connectivity

题目比较简单,是求子图的个数,用并查集求,我用的树实现,其实还有更简单的,只是我们老师要求而已。

最重要的是要注意输入输出的格式。

 1 #include <cstdio>
 2 #include<iostream>
 3 #define MAX 30
 4 using namespace std;
 5
 6 int CharNum;
 7 typedef struct node{
 8     int data_;//节点对应人的编号
 9     int rank_;//节点对应的秩
10     int parent_;//节点对应双亲下标
11 }UFSTree;
12 void MAKE_SET(UFSTree t[]);//初始化并查集树
13 int FIND_SET(UFSTree t[],int x);//在x所在子树中查找集合编号
14 void UNION(UFSTree t[],int x,int y);//将x和y所在的子树合并
15 int main()
16 {
17
18     //freopen("D:\\acm.txt","r",stdin);
19     int caseNum;
20     char MaxChar[20],pairChars[20];
21     scanf("%d",&caseNum);
22     getchar();getchar();
23     while(caseNum--){
24         gets(MaxChar);
25         CharNum = MaxChar[0] - ‘A‘ + 1;//字母的个数
26         int flag = 0;//子图的个数
27         UFSTree t[MAX];
28         MAKE_SET(t);
29         while(gets(pairChars)){
30             int x,y;
31             if(pairChars[0] == 0)break;
32             x = pairChars[0] - ‘A‘ + 1;
33             y = pairChars[1] - ‘A‘ + 1;
34             if(FIND_SET(t,x)!= FIND_SET(t,y)){
35                 UNION(t,x,y);
36             }
37         }
38         for(int i = 1;i <= CharNum;i++){//统计子图的个数
39             if(t[i].parent_ == i)flag++;
40         }
41         printf("%d\n",flag);
42         if(caseNum)printf("\n");//两个输出样例之间有一空行,因为这WA了好多次
43     }
44     return 0;
45 }
46 void MAKE_SET(UFSTree t[]){
47     for(int i = 0;i <= CharNum;i++){//要从0开始,否则会RE,实际0不使用。
48         t[i].data_ = i;//数据为该人编号
49         t[i].rank_ = 1;
50         t[i].parent_ = i;//父节点指向自己
51     }
52 }
53 int FIND_SET(UFSTree t[],int x){//找到祖宗节点
54     if(x != t[x].parent_){
55         return (FIND_SET(t,t[x].parent_));
56     }
57     else
58         return x;
59 }
60 void UNION(UFSTree t[],int x,int y){
61     x = FIND_SET(t,x);
62     y = FIND_SET(t,y);
63     if(x == y)return;
64     if(t[x].rank_ > t[y].rank_){
65             t[y].parent_ = x;
66             t[x].rank_ += t[y].rank_;//更新连接的个数
67     }
68     else{
69         t[x].parent_ = y;
70         t[y].rank_ += t[x].rank_;//更新连接的个数
71     }
72 }
时间: 2024-08-01 03:27:43

Uva 459 Graph Connectivity的相关文章

Graph Connectivity UVA, 459(并查集)

Graph Connectivity UVA, 459 Time Limit: 3000 MS  Graph Connectivity  Consider a graph G formed from a large number of nodes connected by edges. G is said to be connected if a path can be found in 0 or more steps between any pair of nodes in G. For ex

UVA 1479 - Graph and Queries(Treap)

UVA 1479 - Graph and Queries 题目链接 题意:给定一个n个结点m条边的无向图,每个结点一个权值,现在有3种操作 D x,删除id为x的边 Q x k 计算与x结点的连通分量中第k大的数字,不存在就是0 C x v 把x结点权值改为v 要求计算所有Q操作的和除以Q操作的次数的值 思路:Treap的经典题,进行离线操作,把操作全部逆向进行,删边就可以转化为加边,就可以利用并查集,那么维护第k大就利用Treap 代码: #include <cstdio> #include

poj2838 Graph Connectivity

Graph Connectivity Time Limit: 8000MS   Memory Limit: 131072K Total Submissions: 3381   Accepted: 883 Case Time Limit: 3000MS Description Let us consider an undirected graph G = < V, E >. At first there is no edge in the graph. You are to write a pr

uva 1479 - Graph and Queries(伸展树)

题目链接:uva 1479 - Graph and Queries 题目大意:有一张m条边的无向图,每个节点都有一个权值,现在有若干个操作, D x:删除ID为x的节点 Q x k:计算与节点x联通的节点当中,第k大的权值 C x v:把节点x的权值改为v 解题思路:把所有操作反过来处理,先执行所有的D操作,获得最终的图,然后逆操作的时候对于D来说即为合并操作,Q和C则是查询和修改操作. #include <cstdio> #include <cstring> #include &

uva 193 Graph Coloring(回溯)

uva 193 Graph Coloring You are to write a program that tries to find an optimal coloring for a given graph. Colors are applied to the nodes of the graph and the only available colors are black and white. The coloring of the graph is called optimal if

uva 10720 Graph Construction(贪心)

这道题目其实挺简单的,uva上通过率却很低,今天晚上的第二道题,什么时候一个晚上我能做4道中等难度的题目 我就满意了... 思路:本来我是想着排序后然后遍历数组,将当前i的后面a[i]个值一次减一,然后继续遍历继续重复这样做,里面 包含着条件,如果有数变成负数就不成立,或者后面的点小于当前的度数也不成立,然后就TLE了,这个方法是错误 的,因为当序列为 4 4 3 3 2 2的时候最后一个2就会比前面变成1的数字大,需要重新排序的...我是稍微瞄 了一眼别人提交的代码才想起来的,每次处理最大数字

UVA - 459 (并查集)

链接:  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22649 题意就是求有多少联通的块.(一开始自己脑补成双联通 然后死也过不了样例. 这道题目的读入也是挺恶心的. #pragma comment(linker, "/STACK:10240000,10240000") #include <algorithm> #include <iostream> #include <ss

UVA 1479 Graph and Queries (Treap)

题意:给一个无向图,再给一系列操作(以下3种),输出最后的平均查询结果. (1)D X 删除第x条边. (2)Q X k  查询与点X相连的连通分量中第k大的点的权值. (3)C X v  将点X的权值改为v. 吐槽:第一次写的人儿,WA,MLE,TLE各种惨.而且还好我写过splay,不然坑得更惨.耗时整整一天半在查错. 思路: 第一点,由于需要删除边,不是很方便,所以可以先将所有操作存起来,反序来操作,这样删边就变成加边,方便了不少.每次加边时若两点不属于同个连通分量,就将点少的整个连通分量

uva 193 Graph Coloring( 图染色 ) DFS+回溯

非自己出品就是容易wa啊,想了一会没想出来,就忍不住去找答案了,实在没忍住去找答案,结果还是wa了两 次,,,还是自己想的比较靠谱啊, 思路: 如果当前点可以被染成黑色,就把它染成黑色,继续深搜,之后回溯,把它染成白色 如果当前点只能被染成白色,就染成白色深搜 代码: #include<stdio.h> #include<string.h> #include<stdlib.h> int map[105][105]; int visit[105]; int ans[105