Pagini recente »
Monitorul de evaluare
|
3333
|
Cod sursă (job #319727)
|
Istoria paginii runda/sequences2
|
Cod sursă (job #308989)
Cod sursă (job
#308989)
#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;
}
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(i < j && cmp1(i, j) == true){
addedge(i, 2 * n + j);
addedge(n + i, 2 * n + j);
//cout << i << ' ' << j << '\n';
} else if(i != j && cmp2(i, j) == true){
addedge(i, 2 * n + j);
addedge(n + i, 2 * n + j);
//cout << i << ' ' << j << '\n';
}
}
}
out << n - maxmatching() << '\n';
in.close();
out.close();
return 0;
}