Counting 4-Cliques

本文介绍了一种算法,用于构造一个无向简单图,该图恰好包含k个大小为4的团(即完全子图)。文章分析了如何通过组合不同节点数量的完全图来实现这一目标,并提供了一段C++代码示例。

链接:https://www.nowcoder.com/acm/contest/145/E

题目描述

You love doing graph theory problems. You've recently stumbled upon a classical problem : Count the number of 4-cliques in an undirected graph.

Given an undirected simple graph G, a 4-clique of G is a set of 4 nodes such that all pairs of nodes in this set are directly connected by an edge.

This task would be too easy for you, wouldn't it? Thus, your task here is to find an undirected simple graph G with exactly k 4-cliques. Can you solve this task?

 

输入描述:

The first line of input contains a single integer k (1 ≤ k ≤ 106).

输出描述:

On the first line, output two space-separated integers, n, m (1 ≤ n ≤ 75, 1 ≤ m ≤ n * (n - 1) / 2). On the next m lines, output two space-separated integers denoting an edge of the graph u, v (1 ≤ u, v ≤ n), where u and v are the endpoints of the edge.

Your graph must not contain any self-loops or multiple edges between the same pair of nodes. Any graph that has exactly k 4-cliques and satisfies the constraints will be accepted. It can be proven that a solution always exist under the given constraints.

示例1

输入

复制

1

输出

复制

4 6
1 2
1 3
1 4
2 3
2 4
4 3

说明

In the sample, the whole graph is a 4-clique.

题意:

就是完全图的问题,构造一个<=75个点的图,使得⼤小为4的团(就是指互相可达的四个点)恰有k个。 k<=1e6

分析:

有点小技巧,就是拼数的时候,有一个特殊的边界,K最大的完全图是71,但是上下的团不能用剩余的4个点表示,但是可以用70个点的完全图和5个点分别于这70个点相连可以构成。五个点之间没有边,他们只会向t个点构成的完全图连边。如果连了了x个,构成C(x, 3)个⼤大⼩小为4个团。
代码:

#include<bits/stdc++.h>
using namespace std;
vector<int>v[100];
int c[80][5],dp[10][211005];
int num,str,maxn;
void pp()
{
    memset(c, 0, sizeof(c));
    c[1][1] = 1;
    for (int i = 0; i < 80; i++)
        c[i][0] = 1;
    for (int i = 1; i < 80; i++)
        for (int j = 1; j < 5; j++)
            c[i][j] = c[i - 1][j-1] + c[i - 1][j];
}
void dfs(int len,int key)
{
    int i;
    if(len==0)return;
    for(i=0;i<=num;i++)
    {
        if(dp[len-1][key-c[i][3]])
        {
            v[str].push_back(i);
            maxn+=i;
            str++;
            dfs(len-1,key-c[i][3]);
            return;
        }
    }

}
int main()
{
    int i,j,k,n,sum,sec,m,l;
    pp();
    memset(dp,0,sizeof(dp));
    scanf("%d",&k);
    num=-1;
    for(i=4;i<71;i++)
    {
        if(c[i][4]<=k)num=i;
    }
    for(i=1;i<80;i++)
        v[i].clear();
    sum=k-c[num][4];
    dp[0][0]=1;
    str=num+1;
    //printf("num =%d\n",num);
    maxn=num*(num-1)/2;
    for(i=0;i<5;i++)
    {
        for(j=0;j<100002;j++)
        {
            if(dp[i][j])
            {
                for(l=0;l<=num;l++)
                {
                    if(c[l][3]+j<=1000000)
                    dp[i+1][c[l][3]+j]=1;
                }
            }
        }
    }
    dfs(5,sum);
    printf("%d %d\n",str-1,maxn);
    for(i=1;i<num;i++)
    {
        for(j=i+1;j<=num;j++)
            printf("%d %d\n",i,j);
    }
    for(i=num+1;i<str;i++)
    {
        for(j=1;j<=v[i][0];j++)
            printf("%d %d\n",i,j);
    }

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值