Cod sursă (job #308992)

Utilizator avatar Men_in_Black Marco Polo Men_in_Black IP ascuns
Problemă Canibali (lot liceu) Compilator cpp | 2.68 kb
Rundă Arhiva de probleme Status evaluat
Dată 19 iul. 2017 16:23:53 Scor 65
#include <iostream>
#include <fstream>
#include <queue>
#include <algorithm>

using namespace std;

ifstream in("canibali.in");
ofstream out("canibali.out");

int const nmax = 2048;

int n, m;
int a[1 + nmax][1 + 4];
int dist[1 + 3 * nmax];
bool vis[1 + 3 * nmax];
vector<int> g[1 + 3 * nmax];
int match[1 + 3 * nmax];

void addedge(int x, int y) {
  g[x].push_back(y);
  g[y].push_back(x);
}

void bfs() {
  fill(dist + 1, dist + 3 * n + 1, -1);
  queue < int > q;
  for(int i = 1; i <= 2 * n; i++){
    if(match[i] <= 0){
      q.push(i);
      dist[i] = 0;
    }
  }

  while(!q.empty()){
    int u1 = q.front();
    q.pop();
    for(int i = 0; i < g[u1].size(); i++) {
      int v1 = g[u1][i];
      int u2 = match[v1];
      if(0 < u2 && dist[u2] < 0) {
        dist[u2] = dist[u1] + 1;
        q.push(u2);
      }
    }
  }
}

bool dfs(int u1) {
  vis[u1] = 1;
  for(int i = 0; i < g[u1].size(); i++) {
    int v1 = g[u1][i];
    int u2 = match[v1];
    if(match[v1] <= 0) {
      match[u1] = v1;
      match[v1] = u1;
      return true;
    } else if(vis[u2] == 0 && dist[u2] == dist[u1] + 1) {
      if(dfs(u2) == 1) {
        match[u1] = v1;
        match[v1] = u1;
        return true;
      }
    }
  }
  return false;
}

int maxmatching() {
  int answer = 0, add = 1;
  while(0 < add) {
    bfs();
    fill(vis + 1, vis + 3 * n + 1, false);

    add = 0;
    for(int i = 1; i <= 2 * n; i++) {
      if(match[i] <= 0){
        if(dfs(i) == true)
          add++;
      }
    }
    answer += add;
  }
  return answer;
}

bool cmp1(int x, int y){
  for(int i = 1; i <= 4; i++){
    if(a[x][i] != a[y][i])
      return false;
  }
  return true;
}

bool cmp2(int x, int y){
  for(int i = 1; i <= 4; i++){
    if(a[x][i] < a[y][i])
      return false;
  }
  return true;
}

bool hasedge(int i, int j){
  if(i == j)
    return false;
  else if(a[i][1] == a[j][1] && a[i][2] == a[j][2] &&
          a[i][3] == a[j][3] && a[i][4] == a[j][4] &&
          j < i)
    return true;
  else if(a[j][1] <= a[i][1] && a[j][2] <= a[i][2] &&
          a[j][3] <= a[i][3] && a[j][4] <= a[i][4])
    return true;
  else
    return false;
}

int main() {
  in >> n;
  for(int i = 1; i <= n; i++){
    in >> a[i][1] >> a[i][2] >> a[i][3] >> a[i][4];
  }

  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      if(hasedge(i, j)){
        addedge(i, j + 2 * n);
        addedge(i + n, j + 2 * n);
      }
    }
  }

  int sol = maxmatching();
  int cnt = 0;

  for(int i = 1; i <= 2 * n; i++){
    if(0 < match[i])
      cnt++;
  }
  out << n - cnt << '\n';

  in.close();
  out.close();
  return 0;
}