Skip to content

Instantly share code, notes, and snippets.

@joaofnds
Created August 18, 2017 04:58
Show Gist options
  • Save joaofnds/4e787f1c4b13f06a7ad0ae6ae75bd315 to your computer and use it in GitHub Desktop.
Save joaofnds/4e787f1c4b13f06a7ad0ae6ae75bd315 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <iostream>
using namespace std;
inline int char_to_index(char c) { return c - 'a'; }
inline char index_to_char(int index) { return 'a' + index; }
int main(void) {
int vertices_number = 0, edges_number = 0, test_case_number = 5;
char sets[26];
char v1, v2;
cin >> test_case_number;
for (int i = 0; i < test_case_number; i++) {
// Reset sets
for (int j = 0; j < 26; j++) sets[j] = index_to_char(j);
scanf("%d %d\n", &vertices_number, &edges_number);
for (int j = 0; j < edges_number; j++) {
scanf("%c %c\n", &v1, &v2);
sets[char_to_index(v2)] = sets[char_to_index(v1)];
}
cout << "Case #" << i + 1 << ":" << endl;
int connected_cout = 0;
for (int j = 0; j < vertices_number; j++) {
char j_to_char = index_to_char(j);
if (sets[j] != j_to_char) continue;
for (int k = 0; k < vertices_number; k++) {
if (sets[k] == j_to_char) cout << index_to_char(k) << ",";
}
if (sets[j] == j_to_char) {
connected_cout++;
cout << endl;
}
}
cout << connected_cout << " connected components" << endl << endl;
} // for each test_case_number
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment