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

87 篇文章 0 订阅
9 篇文章 0 订阅

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 个结点的完全图,让你给每条边标记0或者 1 ,使得标记相同的三元环最少。输出最小满足要求的三元环数和标记方案。

解题思路:

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

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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值