Forests
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5782 | Accepted: 2218 |
Description
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 widely-read 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).
Input
A forest contains T trees numbered from 1 to T and P people numbered from 1 to P. 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.
Output
People may have different opinions as to which trees, according to Berkeley, have made a sound. Output 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
3 4 1 2 3 3 1 3 2 2 3 2 2 4
Sample Output
2
Source
1 #include<stdio.h> 2 #include<set> 3 #include<string.h> 4 using namespace std; 5 6 set <int> a[110] ; 7 bool flag [110] ; 8 int t , p ; 9 //set <int> :: iterator it ;迭代器 10 11 int main () 12 { 13 //freopen ("a.txt" , "r" , stdin ) ; 14 memset (flag , 0 , sizeof(flag) ) ; 15 scanf ("%d%d" , &t , &p ) ; 16 int i , j ; 17 int cnt = 0 ; 18 while (~ scanf ("%d%d" , &i , &j) ) { 19 a[i].insert (j) ; 20 } 21 for (int i = 1 ; i < p ; i++) { 22 if (flag[i] == 0) { 23 flag[i] = 1 ; 24 for (int j = i + 1 ; j <= p ; j++) { 25 if (a[i] == a[j]) { 26 flag [j] = 1 ; 27 } 28 } 29 cnt ++ ; 30 } 31 } 32 printf ("%d " , cnt ) ; 33 return 0 ; 34 }
从师兄们那学来的orz ,首先知道了如何把set中的元素输出:
set <int> ::iterator it ;
for (it = a.begin () ; it <= a.end () ; i++ )
printf ("%d " , *it ) ;
----------接下来是如何判断两个set集合相等----------
简单粗暴orz:
set <int> a , b ;
if (a == b) {
puts ("Yes") ;
}
else {
puts ("No") ;
}