UVA Solution 459 - Graph Connectivity - Solution in C,C++ - Online Judge Solution

Latest

It is a free Online judges problems solution list. Here you can find UVA online Judge Solution, URI Online Judge Solution, Code Marshal Online Judge Solution, Spoz Online Judge Problems Solution

Friday, May 19, 2017

UVA Solution 459 - Graph Connectivity - Solution in C,C++

UVA Solution 459 - Graph Connectivity - Solution in C,C++

UVA Online Judge Solution 459 - Graph Connectivity | Volume 4
UVA Problem Link -459 - Graph Connectivity

Problem Name: 459 - Graph Connectivity Solution
Problem Number : UVA - 459 - Graph Connectivity Code
Online Judge : UVA Online Judge Solution
Volume: 4
Solution Language : C plus plus

UVA Online Judge Solution, UVA OJ Solution list, UVA Problems Solution, UVA solver, UVA all problem solution list

UVA Solution 459 - Graph Connectivity Code in C/CPP:

#include <stdio.h>
int p[26], r[26];
void init(int n) {
    while(n >= 0) {
        p[n] = n, r[n] = 1;
        n--;
    }
}
int find(int x) {
    return p[x] == x ? x : (p[x]=find(p[x]));
}
int joint(int x, int y) {
    x = find(x), y = find(y);
    if(x != y) {
        if(r[x] > r[y])
            r[x] += r[y], p[y] = x;
        else
            r[y] += r[x], p[x] = y;
        return 1;
    }
    return 0;
}
int main() {
    int t;
    char s[10];
    scanf("%d ", &t);
    while(t--) {
        gets(s);
        init(s[0]-'A');
        int ans = s[0]-'A'+1;
        while(gets(s)) {
            if(s[0] == '\0')
                break;
            ans -= joint(s[0]-'A', s[1]-'A');
        }
        printf("%d\n", ans);
        if(t)
            puts("");
    }
    return 0;
}


Tags: UVA Online Judge Solution, UVA OJ Solution list, UVA Problems Solution, UVA solver, UVA all problem solution list, UVA 459 code in C, UVA 459 code in C++, UVA solution in C, UVA graph connectivity solution

No comments:

Post a Comment