Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************************************
- * Md. Abdulla Al Mamun (Nayon)
- * ID: 1306001
- * Session: 2013-2014
- * Department of Computer Science and Engineering
- * Begum Rokeya University, Rangpur (BRUR)
- *
- * Project Name: Graph Connectivity
- * File Created on: Monday, 2015-07-06-00.50.13
- * Current File: D:\My Codes\C and C++\Code-Blocks Projects\Graph Connectivity\main.cpp
- * Language: English (U.S.)
- * Encoding: windows-1252
- *****************************************************************************************/
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <cstring>
- #include <algorithm>
- #include <vector>
- #include <cmath>
- using namespace std;
- struct graph{
- int parent;
- int depth;
- };
- graph nodes[30];
- void ini(int n)
- {
- int i;
- for(i = 1; i <= n; i++){
- nodes[i].parent = i;
- nodes[i].depth = 0;
- }
- }
- int find_repre(int n)
- {
- if(nodes[n].parent != n){
- nodes[n].parent = find_repre(nodes[n].parent);
- }
- return nodes[n].parent;
- }
- void make_friend(int a, int b)
- {
- int x = find_repre(a);
- int y = find_repre(b);
- if(x != y){
- if(nodes[x].depth < nodes[y].depth)nodes[x].parent = y;
- else if(nodes[x].depth > nodes[y].depth)nodes[y].parent = x;
- else{
- nodes[y].parent = x;
- nodes[x].depth++;
- }
- }
- }
- int main()
- {
- //freopen("in.txt", "r", stdin);
- int t, i, ans, n;
- char str[5], s, e;
- scanf("%d", &t);
- getchar();
- getchar();
- bool f = false;
- while(t--){
- if(f)printf("\n");
- f = true;
- gets(str);
- n = str[0] - 'A'+1;
- ini(n);
- while((gets(str)) && (strlen(str) != 0)){
- s = str[0];
- e = str[1];
- make_friend(s-'A'+1, e-'A'+1);
- }
- ans = 0;
- for(i = 1; i <= n; i++){
- if(nodes[i].parent == i)ans++;
- }
- printf("%d\n", ans);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement