Cod sursa(job #2958062)

Utilizator nicu_ducalNicu Ducal nicu_ducal Data 24 decembrie 2022 13:49:51
Problema Balanta Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.97 kb
#include <bits/stdc++.h>
using namespace std;

template <typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template <typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
using i64 = long long int;

const int INF = INT_MAX, MOD = 1e9 + 7;
const long long LINF = LLONG_MAX;
const double EPS = 1e-9, PI = acos(-1);
const int dx[] = {0, 0, 0, -1, 1, -1, 1, 1, -1};
const int dy[] = {0, -1, 1, 0, 0, -1, 1, -1, 1};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  /// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

  ifstream cin("balanta.in");
  ofstream cout("balanta.out");

  int N, M; cin >> N >> M;
  vector<int> marked(N + 1, -2);
  vector<int> left(N + 1), right(N + 1);
  for (int i = 0; i < M; i++) {
    int k, r; cin >> k;
    for (int j = 0; j < k; j++)
      cin >> left[j];
    for (int j = 0; j < k; j++)
      cin >> right[j];
    cin >> r;
    if (r == 0) {
      for (int j = 0; j < k; j++) {
        marked[left[j]] = 0;
        marked[right[j]] = 0;
      }
    } else if (r == 1) {
      for (int j = 0; j < k; j++) {
        marked[left[j]] = marked[left[j]] == 0 ? 0 : 1;
        marked[right[j]] = marked[right[j]] == 0 ? 0 : -1;
      }
    } else {
      for (int j = 0; j < k; j++) {
        marked[left[j]] = marked[left[j]] == 0 ? 0 : -1;
        marked[right[j]] = marked[right[j]] == 0 ? 0 : 1;
      }
    }
  }

  int ones = 0, neg_ones = 0, idx_one, idx_neg_one;
  for (int i = 1; i <= N; i++) {
    if (marked[i] == 1) {
      ++ones;
      idx_one = i;
    }
    else if (marked[i] == -1) {
      ++neg_ones;
      idx_neg_one = i;
    }
  }

  if (ones == 1 and neg_ones > 1)
    cout << idx_one << "\n";
  else if (neg_ones == 1 and ones > 1)
    cout << idx_neg_one << "\n";
  else
    cout << 0 << "\n";

  return 0;
}