【HDU5952】Counting Cliques

题目大意:给定一个\(N\)个点,\(M\)条边的无向图,求图中有多少个大小为\(S\)的团。\(N \le 100,deg(i)\le 20,i\in [1,n]\)。

题解:
考虑搜索。
需要确定一种搜索顺序,使得团的计数不重不漏。考虑枚举团中最小编号的节点,且搜索状态转移中只能转移到比当前团中编号最大的节点编号更大的点。

由于\(N\)上限是100,但是每个节点的度数很小,若直接用邻接矩阵进行状态转移,复杂度较高,因此考虑建立邻接表进行转移。在判断两点是否存在边时用邻接矩阵快速判断即可。

由于编号由小到大进行转移,因此对于建图来说,每条边可以仅由编号小的点指向编号大的点,这样转移时又可以省去一半的复杂度。

搜索过程中加入一个剪枝,即:当前团中的点加上后面可以加的所有的点如果小于要求的团的大小,直接返回即可。

代码如下

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n, m, S;
    cin >> n >> m >> S;
    vector<vector<int>> adj(n);
    vector<vector<int>> mat(n, vector<int>(n));
    for (int i = 0; i < m; i++) {
      int x, y;
      cin >> x >> y;
      x--, y--;
      if (x > y) swap(x, y);
      adj[x].push_back(y);
      mat[x][y] = 1;
    }
    int ans = 0;
    vector<int> cyc;
    function<void(int)> dfs = [&](int x) {
      if ((int)cyc.size() == S) {
        ans++;
        return;
      }
      if (n - 1 - x < S - (int)cyc.size()) {
        return;
      }
      for (auto y : adj[x]) {
        bool ok = 1;
        for (auto z : cyc) {
          if (mat[z][y] == 0) {
            ok = 0;
            break;
          }
        }
        if (ok) {
          cyc.push_back(y);
          dfs(y);
          cyc.pop_back();
        }
      }
    };
    for (int i = 0; i < n; i++) {
      cyc.clear();
      cyc.push_back(i);
      dfs(i);
    }
    cout << ans << endl;
  }
  return 0;
}

原文地址:https://www.cnblogs.com/wzj-xhjbk/p/11830656.html

时间: 2024-07-31 19:20:56

【HDU5952】Counting Cliques的相关文章

【LeetCode】Counting Bits(338)

1. Description Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example: For num = 5 you should return [0,1,1,2,1,2]. 2. Answ

【Leetcode】Counting Bits

题目链接:https://leetcode.com/problems/counting-bits/ 题目: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example: For num = 5 y

【HDU5862】Counting Intersections

题意 有n条线段,且都平行于坐标轴.对于每条线段,给出两个端点的坐标.问一共有多少个线段的交点. 分析 最最简单的扫描法了.用线段树或者树状数组都可以. 由题目可知,线段只有两种,要么平行于x轴要么平行于y轴.而交点只能是两个不平行的线段产生的. 所有我们以一条平行于x轴的线为扫描线,从下向上扫.先把横坐标进行离散化,然后把平行于y轴的线段拆成上下两个端点.当扫到下端点的时候就在它横坐标+1,当扫到上端点的时候,就在它横坐标-1.对于每一条平行于x轴的线,则将左右端点内的值相加.就酱~ 这里一个

【LeetCode】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j

【LeetCode】位运算 bit manipulation(共32题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [78]Subsets [136]Single Number [137]Single Number II [169]Majority Element [187]Repeated DNA Sequences [190]Reverse Bits [191]Number of 1 Bits [201]Bitwise AND of Numbers Range [231]Pow

【算法系列学习】巧妙建图,暴搜去重 Counting Cliques

E - Counting Cliques http://blog.csdn.net/eventqueue/article/details/52973747 http://blog.csdn.net/yuanjunlai141/article/details/52972715 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<

【HackerRank】 The Full Counting Sort

In this challenge you need to print the data that accompanies each integer in a list. In addition, if two strings have the same integers, you need to print the strings in their original order. Hence, your sorting algorithm should be stable, i.e. the

UVALive 6602 Counting Lattice Squares 【几何】【机智】

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4613 题目大意:给你一个n*m的矩阵格子,在这n*m的矩阵格子中要你选出四个点,形成一个正方形,让正方形的面积为奇数,问可以形成多少个这样的正方形. 题目思路:从每一个奇数开始作为一个基本单元. 面积     边 能组成的正方形: 1*1      1      

【题解】晋升者计数 Promotion Counting [USACO 17 JAN] [P3605]

[题解]晋升者计数 Promotion Counting [USACO 17 JAN] [P3605] 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训.!牛是可怕的管理者! [题目描述] 奶牛从 \(1\) ~ \(N(1≤N≤1e5)\) 进行了编号,把公司组织成一棵树,\(1\)号奶牛作为总裁(树的根节点).除总裁以外的每头奶牛都有且仅有唯一的一个的上司(即它在树上的父结点).每一头牛\(i\)都有一个不同的能力指数 \(p(i)\),描述了她对其工作的擅长程度.如果奶牛