[WC2007]剪刀石头布

嘟嘟嘟


这题不愧是冬令营的题,有思维难度。


题面就是说有一个完全图,让你给一些无向边定向,使三元环最多。
这题关键就是怎么计数三元环。直接记非常难,所以我们要正难则反!三元环总数是\(C_{n} ^ {3}\),然后考虑什么情况会破坏三元环:当一个点的出边大于1时,记\(d_i\)\(i\)的出边数量,则破坏的三元环数量就是\(C_{d_i} ^ 2\),所以三元环总数是\(C_{n} ^ {3} - \sum C_{d_i} ^ {2}\)。所以我们要最小化后面的那个东西。


观察\(C_{d_i} ^ {2} = \frac{d_i * (d_i - 1)}{2}\),发现这东西其实是一个等比数列(这都能发现)。所以我们把一个点\(i\)拆成\(n\)个点,分别向汇点连容量为1,费用为\(,1, 2, 3 \ldots n - 1\)的边。
然后把边看成点。对于一条无向边\((x, y)\),分别向点\(x, y\)连一条容量为1,费用为0的边,表示可以让其中一个点的出度+1;对于一条指向\(y\)的有向边,就只向点\(y\)连一条容量为1,费用为0的边。最后从源点向所有边代表的点连一条容量为1,费用为0的边。


至于输出矩阵,看对应边是否满流即可。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 105;
const int maxN = 2e4 + 5;
const int maxe = 1e7 + 5;
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}

int n, t, a[maxn][maxn];
struct Edge
{
  int nxt, from, to, cap, cos;
}e[maxe];
int head[maxN], ecnt = -1;
In void addEdge(int x, int y, int w, int c)
{
  e[++ecnt] = (Edge){head[x], x, y, w, c};
  head[x] = ecnt;
  e[++ecnt] = (Edge){head[y], y, x, 0, -c};
  head[y] = ecnt;
}

bool in[maxN];
int dis[maxN], pre[maxN], flow[maxN];
In bool spfa()
{
  Mem(dis, 0x3f), Mem(in, 0);
  dis[0] = 0, flow[0] = INF;
  queue<int> q; q.push(0);
  while(!q.empty())
    {
      int now = q.front(); q.pop(); in[now] = 0;
      for(int i = head[now], v; ~i; i = e[i].nxt)
	{
	  if(dis[v = e[i].to] > dis[now] + e[i].cos && e[i].cap > 0)
	    {
	      dis[v] = dis[now] + e[i].cos;
	      pre[v] = i;
	      flow[v] = min(flow[now], e[i].cap);
	      if(!in[v]) q.push(v), in[v] = 1;
	    }
	}
    }
  return dis[t] ^ INF;
}
int minCost = 0;
In void update()
{
  int x = t;
  while(x)
    {
      int i = pre[x];
      e[i].cap -= flow[t];
      e[i ^ 1].cap += flow[t];
      x = e[i].from;
    }
  minCost += flow[t] * dis[t];
}
In int MCMF()
{
  minCost = 0;
  while(spfa()) update();
  return minCost;
}

In int N(int x, int y) {return n + (x - 1) * n + y;}

int ans[maxn][maxn];
In void solve(int x, int y)
{
  for(int i = head[N(x, y)], v; ~i; i = e[i].nxt)
    {
      v = e[i].to;
      if(v && v <= n && !e[i].cap)
	{
	  if(v == y) ans[x][y] = 1;
	  else ans[y][x] = 1;
	}
    }
}

int main()
{
  Mem(head, -1);
  n = read(); t = n * n + n + 1;
  for(int i = 1; i <= n; ++i)
    for(int j = 1; j <= n; ++j) a[i][j] = read();
  for(int i = 1; i <= n; ++i)
    for(int j = i + 1; j <= n; ++j)
      {
	addEdge(0, N(i, j), 1, 0);
	if(a[i][j] == 2)
	  {
	    addEdge(N(i, j), i, 1, 0);
	    addEdge(N(i, j), j, 1, 0);
	  }
	else if(a[i][j] == 1) addEdge(N(i, j), j, 1, 0);
	else addEdge(N(i, j), i, 1, 0);
      }
  for(int i = 1; i <= n; ++i)
    for(int j = 0; j < n; ++j) addEdge(i, t, 1, j);
  write(1LL * n * (n - 1) * (n - 2) / 6 - MCMF()), enter;
  for(int i = 1; i <= n; ++i)
    for(int j = i + 1; j <= n; ++j) solve(i, j);
  for(int i = 1; i <= n; ++i, enter)
    for(int j = 1; j <= n; ++j) write(ans[i][j]), space;
  return 0;
}
posted @ 2019-05-05 18:50  mrclr  阅读(117)  评论(0编辑  收藏  举报