hdu 5952 Counting Cliques 求图中指定大小的团的个数 暴搜
题目链接
题意
给定一个\(n个点,m条边\)的无向图,找出其中大小为\(s\)的完全图个数\((n\leq 100,m\leq 1000,s\leq 10)\)。
思路
暴搜。
搜索的时候判断要加进来的点是否与当前集合中的每个点之间都有边。搜到集合大小为\(s\)就答案+1.
注意
如果不做处理的话,每个完全图都会被搜到\(2^s\)次,其中只有一次是必要的。
因此,一个很显然的常用的考虑是:搜索的时候下一个节点比当前的节点编号大,这样就肯定不会搜重复了。
再稍微转化一下,在建图的时候就可以只建小点指向大点的边。
Code
#include <bits/stdc++.h>
#define maxn 110
#define maxm 1010
using namespace std;
typedef long long LL;
bool mp[maxn][maxn];
int st[12], ecnt, s, ne[maxn];
LL ans;
struct Edge {
int to, ne;
Edge(int _to=0, int _ne=0) : to(_to), ne(_ne) {}
}edge[maxm];
bool add(int u, int v) {
edge[ecnt] = Edge(v, ne[u]);
ne[u] = ecnt++;
}
bool check(int v, int tot) {
for (int i = 0;i <= tot; ++i) {
if (!mp[st[i]][v]) return false;
}
return true;
}
void dfs(int tot, int u) {
st[tot] = u;
if (tot == s-1) { ++ans, --tot; return; }
for (int i = ne[u]; ~i; i = edge[i].ne) {
int v = edge[i].to;
if (check(v, tot)) dfs(tot+1, v);
}
}
void work() {
int n, m;
scanf("%d%d%d", &n, &m, &s);
memset(mp, 0, sizeof(mp));
ecnt = 0; memset(ne, -1, sizeof(ne));
for (int i = 0; i < m; ++i) {
int x, y;
scanf("%d%d", &x, &y);
if (x > y) swap(x, y);
mp[x][y] = 1; add(x, y);
}
ans = 0;
for (int i = 1; i <= n; ++i) dfs(0, i);
printf("%lld\n", ans);
}
int main() {
int T;
scanf("%d", &T);
while (T--) work();
return 0;
}
hdu 5952 Counting Cliques 求图中指定大小的团的个数 暴搜的更多相关文章
- HDU - 5952 Counting Cliques(dfs搜索)
题目: A clique is a complete graph, in which there is an edge between every pair of the vertices. Give ...
- HDU - 5952 Counting Cliques
Counting Cliques HDU - 5952 OJ-ID: hdu-5952 author:Caution_X date of submission:20191110 tags:dfs,gr ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5952 Counting Cliques(dfs)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU - 5952 Counting Cliques(DFS)
A clique is a complete graph, in which there is an edge between every pair of the vertices. Given a ...
- Floyd-Warshall求图中任意两点的最短路径
原创 除了DFS和BFS求图中最短路径的方法,算法Floyd-Warshall也可以求图中任意两点的最短路径. 从图中任取两点A.B,A到B的最短路径无非只有两种情况: 1:A直接到B这条路径即是最短 ...
- 求二叉树中第K层结点的个数
一,问题描述 构建一棵二叉树(不一定是二叉查找树),求出该二叉树中第K层中的结点个数(根结点为第0层) 二,二叉树的构建 定义一个BinaryTree类来表示二叉树,二叉树BinaryTree 又是由 ...
- hdu4587 Two Nodes 求图中删除两个结点剩余的连通分量的数量
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4587 题目给了12000ms,对于tarjan这种O(|V|+|E|)复杂度的算法来说,暴力是能狗住的 ...
- poj The Settlers of Catan( 求图中的最长路 小数据量 暴力dfs搜索(递归回溯))
The Settlers of Catan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1123 Accepted: ...
随机推荐
- 搭建zipkin参数配置
Environment Variables zipkin-server is a drop-in replacement for the scala query service. yaml confi ...
- PHP实现的敏感词过滤方法
PHP实现的敏感词过滤方法,以下是一份过滤敏感词的编码.有需要可以参考参考. /** * @todo 敏感词过滤,返回结果 * @param array $list 定义敏感词一维数组 * @para ...
- php - 去除php代码中的多余空格
<?php class Test{ public function test(){ $tmplContent = file_get_contents('./test.php'); $tmplCo ...
- jQuery具体实例介绍什么时候用ajax,ajax应该在什么地方使用
网站开发时,ajax是一个非常方便的工具,它具有和表单相同的功能完成前端和后台之间的交互!它起到局部刷新的功能!那什么时候用ajax呢?下面给大家介绍几个实例,首先应该分为两类: 一.在用表单和aj ...
- JZOJ 5818. 【NOIP提高A组模拟2018.8.15】 做运动
5818. [NOIP提高A组模拟2018.8.15] 做运动 (File IO): input:running.in output:running.out Time Limits: 2000 ms ...
- 学习python第二天 流程判断
while循环age_of_Jim = 56 count = 0 #开始计数while True: #循环代码 if count ==3:#如果次数=3 break#退出 guess_age = in ...
- Qt中修改QtoolTip的样式
Qt中的QtoolTip有几个需要注意的: 1.不能直在堆或栈中生成QToolTip对象.因为其构造函数为私有.2.从widget获取的tooltip不是tooltip对象,而是tooltip中的文本 ...
- B1023 组个最小数 (20分)
B1023 组个最小数 (20分) 给定数字 0-9各若干个.你可以以任意顺序排列这些数字,但必须全部使用.目标是使得最后得到的数尽可能小(注意 0 不能做首位).例如:给定两个 0,两个 1,三个 ...
- 笔记-python-内存管理
笔记-python-内存管理 1. 内存使用 1.1. 对象的内存使用 a = 1 1是一个对象,a是引用,指向1. >>> id(a) 1951821280 这个数 ...
- day14 前端基础 HTML
从今天开始,学习前端基础. 前端,就是HTML CSS JS 等 对于我们这种初学者,并不知道这些专业术语都是什么,给大家举一个形象的例子: HTML 就是一个人,赤裸裸的人 CSS 就是衣服 ...