Counting 4-Cliques

链接:https://www.nowcoder.com/acm/contest/145/E
来源:牛客网
 

Counting 4-Cliques

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld

题目描述

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.

题意:输出k个k派系。什么是k派系?k派系就是总共四个点任何两点都可相互到达(即四个点的完全图),然后输出只有k个k派系的图。

分析:如果C(n,4)==k,那么就是n个点的完全图了。如果不相等,找到C(t,4)<=k的最大的t,然后再补上少的。

怎么补少的就是在t个点的完全图上在加一个点,向完全图连边。

1.构造一个大小为t的完全图,和a, b, c, d, e五个点。
2.a, b, c, d, e五个点之间没有边,他们只会向t个点连边。

3.如果连了x个,构成C(x, 3)个大小为4个团。
4.找到最大的t,枚举a,b,c,d,计算e。

为什么是五个点??

C(70,4)+4*C(70,3)>1e6、C(70,4)+5*C(70,3)>1e6。为什么额外五个点而不是额外四个点??

因为如果比完全图额外多17个派系,就必须用五个点构造出来

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
 
ll C4(ll n) { // C(n, 4)
    ll ans = n * (n - 1) * (n - 2) * (n - 3) / (1 * 2 * 3 * 4);
    return ans;
}
 
ll C3(ll n) { // C(n, 3)
    ll ans = n * (n - 1) * (n - 2) / (1 * 2 * 3);
    return ans;
}
 
ll mp[200000 + 5];
int k;
 
int main() {
    memset(mp, -1, sizeof(mp));
    for (ll i = 1; i <= 100; i++) {
        mp[C3(i)] = i;
    }
    scanf("%d", &k);
    ll C = 70;
    ll t = 4;
    while ((t + 1) <= C && C4(t + 1) <= k) {
        t++;
    }
    C = min(C, t);
    ll a, b, c, d, e = -1;
    for (a = 2; a <= C; a++) {
        for (b = 2; b <= C; b++) {
            for (c = 2; c <= C; c++) {
                for (d = 2; d <= C; d++) {
                    ll cnt = C3(a) + C3(b) + C3(c) + C3(d);
                    if (cnt <= k - C4(t)
                            && mp[k - C4(t) - cnt] >= 0
                            && mp[k - C4(t) - cnt] <= C) {
                        e = mp[k - C4(t) - cnt];
                        break;
                    }
                }
                if (e != -1)    break;
            }
            if (e != -1)    break;
        }
        if (e != -1)    break;
    }
    printf("%lld %lld\n", t + 5, t * (t - 1) / 2 + a + b + c + d + e);
    for (int i = 1; i <= t; i++) {
        for (int j = i + 1; j <= t; j++) {
            printf("%d %d\n", i, j);
        }
    }
    for (int j = 1; j <= a; j++)    printf("%lld %d\n", t + 1, j);
    for (int j = 1; j <= b; j++)    printf("%lld %d\n", t + 2, j);
    for (int j = 1; j <= c; j++)    printf("%lld %d\n", t + 3, j);
    for (int j = 1; j <= d; j++)    printf("%lld %d\n", t + 4, j);
    for (int j = 1; j <= e; j++)    printf("%lld %d\n", t + 5, j);
}

猜你喜欢

转载自blog.csdn.net/qq_37891604/article/details/81583508
今日推荐