Advertisement
Guest User

Untitled

a guest
Apr 13th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. int N, max = 1;
  4. std::vector<std::pair<int, int> > tessere;
  5. std::vector<bool> preso;
  6.  
  7. int solve(int s, int p);
  8.  
  9. int main(void) {
  10.     std::ifstream in("input.txt");
  11.     std::ofstream ou("output.txt");
  12.     in >> N;
  13.     int t1, t2;
  14.  
  15.     for(unsigned i = 0; i < N; ++i) {
  16.         in >> t1 >> t2;
  17.         tessere.push_back(std::make_pair(t1, t2));
  18.         preso.push_back(false);
  19.     }
  20.  
  21.     // per ogni possibile tessera iniziale
  22.     for(int i = 0; i < tessere.size(); i++) {
  23.         preso[i] = true;
  24.         int gg = solve(i, 1);
  25.         preso[i] = false;
  26.     }
  27.  
  28.     ou << max;
  29.  
  30.     in.close();
  31.     ou.close();
  32.     return 0;
  33. }
  34.  
  35. int solve(int s, int p) {
  36.     // p rappresenta il numero di tessere che ho concatenato fin' ora
  37.     if(p > max) max = p;
  38.     for(int i = 0; i < N; i++) {
  39.         // se non ho ancora preso quella tessera e quella tessara ha
  40.         // almeno una faccia in comune con la precedente (s)
  41.         if(!preso[i] && (tessere[s].first == tessere[i].first ||
  42.                 tessere[s].first == tessere[i].second ||
  43.                 tessere[s].second == tessere[i].first ||
  44.                 tessere[s].second == tessere[i].second)) {
  45.             preso[i] = true;
  46.             solve(i, p+1);
  47.             preso[i] = false;
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement