# 2017乌鲁木齐赛区网络赛 I Colored Graph（完全图边定向构造）

I: Colored Graph time limit
1000ms
memory limit
131072KB
In graph theory, graph colouring is a special case of graph labelling.
It is an assignment of labels traditionally called colours to edges of a graph.
Here we consider the simplest form.
Given an undirected simple complete graph G with n nodes, this problem asks
about a black-and-white edge-colouring of G, which contains the smallest total number of pure-coloured triangles.
A pure-coloured triangle in G is a set of three different nodes with three same-coloured edges between them.
Input Format
The input has several test cases and the first line provides the total number of test cases.
For each test case, a line with an integer n (n ≤ 500) indicates that the given graph G is an undirected simple complete graph with n nodes.
Output Format
For each test case, output n + 1 lines.
The first line contains the smallest number of pure-coloured triangles.
The following n lines describes an adjacent matrix A = (aij ) of graph G. The answer may not be unique and you can output anyone.
If the edge between i and j is white, aij and aji should be 1.
If the edge between i and j is black, aij and aji should be 2.

Elements of the main diagonal should be 0. Sample Input
2
3
6
Sample Output
0
0 1 1
1 0 2
1 2 0
2
0 2 2 1 1 1
2 0 2 1 1 1
2 2 0 1 1 1
1 1 1 0 2 2
1 1 1 2 0 2
1 1 1 2 2 0

### 题目大意：

有一个V$V$个结点的完全图，让你给每条边标记0$0$或者1$1$，使得标记相同的三元环最少。输出最小满足要求的三元环数和标记方案。

### 解题思路：

我们可以发现对于任意一个标记不相同的三元环，一定会有两个点，每个点的两条边标记不同。那么我们就可以通过容次计算出最终满足要求三元环的个数为C3V(degi(V1degi))/2$C^{3}_{V}-(\sum deg_{i}\cdot (V-1-deg_{i}))/2$，其中degi$deg_{i}$为编号为i$i$的结点标记为1的边的个数。要使这个最小，那么就是要让degi(V1degi)$\sum deg_{i}\cdot (V-1-deg_{i})$最大，显然当degi=V12$deg_{i}=\frac{V-1}{2}$时目标式最大，答案最小。那么答案就是C3VVV12(V1V12)/2$C^{3}_{V}- V \cdot \frac{V-1}{2}\cdot (V-1-\frac{V-1}{2})/2$
接下来就是构造满足要求的图了，令needi$need_{i}$为编号为i$i$的结点当前还需要连接的标记为1的边的数目。于是我们可以贪心的建图，每次取need$need$最小的点u$u$，优先向need$need$最大的点连needu$need_{u}$条边。直到所有点的need$need$都为0。总复杂度O(V2logV)$O(V^2 \cdot log V)$

### AC代码：

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
using namespace std;
#define LL long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))
#define sqr(x) ((x)*(x))

const int MAXV=500+3;

int V;
int G[MAXV][MAXV];
pair<int, int> point[MAXV];//need, u

void init()//初始化
{
for(int i=0;i<V;++i)
for(int j=0;j<V;++j)
G[i][j]=false;
}

int main()
{
int T_T;
scanf("%d", &T_T);
while(T_T--)
{
scanf("%d", &V);
init();
for(int i=0;i<V;++i)
{
point[i].fi=(V-1)/2;//初始状态，每个结点都需要(V-1)/2条编号为1的边
point[i].se=i;
}
for(int i=0;i<V;++i)//贪心构造
{
sort(point+i, point+V);
for(int j=V-1;j>max(i, V-1-point[i].fi);--j)
{
G[point[i].se][point[j].se]=G[point[j].se][point[i].se]=1;
--point[j].fi;
}
}
printf("%d\n", V*(V-1)*(V-2)/6-V*((V-1)/2*(V-1-(V-1)/2))/2);//直接计算出答案
for(int i=0;i<V;++i)
for(int j=0;j<V;++j)
printf("%d%c", i==j?0:G[i][j]+1, j==V-1?'\n':' ');
}

return 0;
}