//UVa 599 - The Forrest for the Trees #include <iostream> #include <string> using namespace std; int count(char node, bool edge[26][26], bool visited[26]) { visited[node] = true; int c = 1; for (char neighbor = 0; neighbor < 26; neighbor++) if (!visited[neighbor] && edge[node][neighbor]) { c += count(neighbor, edge, visited); } return c; } int main() { int cases; for (cin >> cases; cases; cases--) { string line; bool edge[26][26] = { false }; for (cin >> line; line[0] != '*'; cin >> line) { char a = line[1] - 'A', b = line[3] - 'A'; edge[a][b] = true; edge[b][a] = true; } string nodes; cin >> nodes; int acorns = 0, trees = 0; bool visited[26] = { false }; for (int i = 0; i < nodes.length(); i++) { if (nodes[i] < 'A' || nodes[i] > 'Z') continue; char node = nodes[i] - 'A'; if (visited[node]) continue; if (count(node, edge, visited) == 1) acorns++; else trees++; } cout << "There are " << trees << " tree(s) and " << acorns << " acorn(s)." << endl; } return 0; }
Monday, January 9, 2017
UVa 599 - The Forrest for the Trees
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment