Advertisement
Um_nik

Untitled

Mar 14th, 2023
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n;
  6.  
  7. const int maxN = 55;
  8. int v[maxN][2];
  9. int votes_left[maxN];
  10. int potential_votes[maxN];
  11.  
  12. bool backtrack(int idx, int monster){
  13.     if(idx == n+1) return true;
  14.     if(idx == monster || v[idx][0] == monster || v[idx][0] == monster) return backtrack(idx+1,monster);
  15.  
  16.     for (int t = 0; t < 2; t++) {
  17.         if (votes_left[v[idx][t]] >= potential_votes[v[idx][t]]) {
  18.             potential_votes[v[idx][0]]--;
  19.             potential_votes[v[idx][1]]--;
  20.             votes_left[v[idx][t]]--;
  21.             if (backtrack(idx + 1, monster)) return true;
  22.             potential_votes[v[idx][0]]++;
  23.             potential_votes[v[idx][1]]++;
  24.             votes_left[v[idx][t]]++;
  25.             return false;
  26.         }
  27.     }
  28.  
  29.     potential_votes[v[idx][0]]--;
  30.     potential_votes[v[idx][1]]--;
  31.     for (int t = 0; t < 2; t++) {
  32.         if (votes_left[v[idx][t]] == 0) continue;
  33.         votes_left[v[idx][t]]--;
  34.         if (backtrack(idx + 1, monster)) return true;
  35.         votes_left[v[idx][t]]++;
  36.     }
  37.     potential_votes[v[idx][0]]--;
  38.     potential_votes[v[idx][1]]--;
  39.     return false;
  40. }
  41.  
  42. void solve() {
  43.    
  44.     cin >> n;
  45.  
  46.     for(int i = 1;i <= n; i++){
  47.         cin >> v[i][0] >> v[i][1];  
  48.     }
  49.  
  50.     int ans = 0;
  51.     for(int i = 1; i <= n; i++){
  52.  
  53.         for(int j = 1; j <= n; j++){
  54.             canBeVoted[j] = 0;
  55.             potential_votes[j] = 0;
  56.         }
  57.  
  58.         for(int j = 1; j <= n; j++){
  59.             if(v[j][0] == i || v[j][1] == i){
  60.                 potential_votes[i]++;
  61.                 continue;
  62.             }else{
  63.                 potential_votes[v[j][0]]++;
  64.                 potential_votes[v[j][1]]++;
  65.             }
  66.            
  67.         }
  68.  
  69.         if((n+2)/3 + 3 < potential_votes[i])
  70.             continue;
  71.         if (potential_votes[i] < 2) {
  72.             ans++;
  73.             continue;
  74.         }
  75.  
  76.         for(int j = 1; j <= n; j++){
  77.             if(j == i)
  78.                 continue;
  79.             votes_left[j] = potential_votes[i] - 1;
  80.         }
  81.         votes_left[v[i][0]]--;
  82.         votes_left[v[i][1]]--;
  83.  
  84.         if(!backtrack(1,i)){
  85.             ans++;
  86.         }
  87.     }
  88.     cout << ans << "\n";
  89. }
  90.  
  91. int main() {
  92.     ios_base::sync_with_stdio(false);
  93.     cin.tie(0);
  94.  
  95.     solve();
  96.  
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement