Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n;
- const int maxN = 55;
- int v[maxN][2];
- int votes_left[maxN];
- int potential_votes[maxN];
- bool backtrack(int idx, int monster){
- if(idx == n+1) return true;
- if(idx == monster || v[idx][0] == monster || v[idx][0] == monster) return backtrack(idx+1,monster);
- for (int t = 0; t < 2; t++) {
- if (votes_left[v[idx][t]] >= potential_votes[v[idx][t]]) {
- potential_votes[v[idx][0]]--;
- potential_votes[v[idx][1]]--;
- votes_left[v[idx][t]]--;
- if (backtrack(idx + 1, monster)) return true;
- potential_votes[v[idx][0]]++;
- potential_votes[v[idx][1]]++;
- votes_left[v[idx][t]]++;
- return false;
- }
- }
- potential_votes[v[idx][0]]--;
- potential_votes[v[idx][1]]--;
- for (int t = 0; t < 2; t++) {
- if (votes_left[v[idx][t]] == 0) continue;
- votes_left[v[idx][t]]--;
- if (backtrack(idx + 1, monster)) return true;
- votes_left[v[idx][t]]++;
- }
- potential_votes[v[idx][0]]--;
- potential_votes[v[idx][1]]--;
- return false;
- }
- void solve() {
- cin >> n;
- for(int i = 1;i <= n; i++){
- cin >> v[i][0] >> v[i][1];
- }
- int ans = 0;
- for(int i = 1; i <= n; i++){
- for(int j = 1; j <= n; j++){
- canBeVoted[j] = 0;
- potential_votes[j] = 0;
- }
- for(int j = 1; j <= n; j++){
- if(v[j][0] == i || v[j][1] == i){
- potential_votes[i]++;
- continue;
- }else{
- potential_votes[v[j][0]]++;
- potential_votes[v[j][1]]++;
- }
- }
- if((n+2)/3 + 3 < potential_votes[i])
- continue;
- if (potential_votes[i] < 2) {
- ans++;
- continue;
- }
- for(int j = 1; j <= n; j++){
- if(j == i)
- continue;
- votes_left[j] = potential_votes[i] - 1;
- }
- votes_left[v[i][0]]--;
- votes_left[v[i][1]]--;
- if(!backtrack(1,i)){
- ans++;
- }
- }
- cout << ans << "\n";
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement