Advertisement
kburnik

C++ Zadatak Stablo

Oct 13th, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <map>
  4.  
  5. using namespace std;
  6.  
  7. map < string , pair < string , string > > stablo;
  8.  
  9.  
  10. int obilazak(string pocetak, string trazi, string put) {
  11.         if (pocetak.size() == 0) {
  12.             return 0;
  13.         }
  14.        
  15.         put += pocetak;
  16.        
  17.         if (pocetak == trazi) {
  18.             cout << put << endl;
  19.             return 1;
  20.         } else {
  21.             put += " -> ";
  22.             return
  23.                 obilazak(stablo[pocetak].first, trazi, put)
  24.                 + obilazak(stablo[pocetak].second, trazi, put)
  25.             ;
  26.         }
  27. }
  28.  
  29.  
  30. int main() {
  31.    
  32.  
  33.     int n;
  34.     cin >> n;
  35.    
  36.     string x,l,d,root;
  37.     for (int i = 0; i < n; i++) {
  38.         cin >> x >> l >> d;
  39.         stablo[x] = make_pair(l,d);
  40.         if (i == 0) root = x;
  41.     }
  42.    
  43.     cin >> n;
  44.     for (int i = 0; i < n; i++) {
  45.         cin >> x;
  46.         if (!obilazak(root,x,"")) {
  47.             cout << "Nema puta!" << endl;
  48.         }
  49.     }
  50.    
  51.     return 0;    
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement