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
个结点的完全图,让你给每条边标记
解题思路:
我们可以发现对于任意一个标记不相同的三元环,一定会有两个点,每个点的两条边标记不同。那么我们就可以通过容次计算出最终满足要求三元环的个数为
接下来就是构造满足要求的图了,令
needi
为编号为
i
的结点当前还需要连接的标记为1的边的数目。于是我们可以贪心的建图,每次取
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;
}