Algorithm


problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1168

If a tree falls in the forest, and there’s nobody there to hear, does it make a sound? This classic conundrum was coined by George Berkeley (1685-1753), the Bishop and influential Irish philosopher whose primary philosophical achievement is the advancement of what has come to be called subjective idealism. He wrote a number of works, of which the most widelyread are Treatise Concerning the Principles of Human Knowledge (1710) and Three Dialogues between Hylas and Philonous (1713) (Philonous, the “lover of the mind”, representing Berkeley himself). A forest contains T trees numbered from 1 to T and P people numbered from 1 to P. Input The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs. Standard input consists of a line containing P and T followed by several lines, containing a pair of integers i and j, indicating that person i has heard tree j fall. People may have different opinions as to which trees, according to Berkeley, have made a sound. Output For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line. How many different opinions are represented in the input? Two people hold the same opinion only if they hear exactly the same set of trees. You may assume that P < 100 and T < 100.

Sample Input 1 3 4 1 2 3 3 1 3 2 2 3 2 2 4

Sample Output 2

Code Examples

#1 Code Example with C Programming

Code - C Programming

#include <bits/stdc++.h>
using namespace std;

string setToString(set < int>& set){
    string res = "";
    for(auto it=set.begin(); it!=set.end();it++)
        res += to_string(*it) + ":";
    return res;
}

int main() {
    int t,i,j,p,q;
    string in;
    scanf("%d\n\n",&t);
    while(t--){
        unordered_set < string> opinions;
        scanf("%d %d\n",&p,&q);
        vector<set<int>> heard(p, set<int>());
        while(getline(cin,in),!in.empty()){
            istringstream iss(in);
            iss >> i >> j;
            heard[i-1].insert(j);
        }
        for(auto& vec: heard){
            string str = setToString(vec);
            opinions.insert(str);
        }
        cout << opinions.size() << endl;
        if(t) cout << endl;
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
1
3 4
1 2
3 3
1 3
2 2
3 2
2 4

Output

x
+
cmd
2
Advertisements

Demonstration


UVA Online Judge solution - 10227 - Forest - UVA Online Judge solution C,C++,Java

Previous
UVA Online Judge solution - 10226 - Hardwood Species - UVA Online Judge solution C,C++,Java
Next
UVA Online Judge solution -10235 - Simply Emirp - UVA Online Judge solution C,C++,Java