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: ...
随机推荐
- Linux企业生产环境用户权限集中管理项目方案案例
企业生产环境用户权限集中管理项目方案案例: 1 问题现状 当前我们公司里服务器上百台,各个服务器上的管理人员很多(开发+运维+架构+DBA+产品+市场),在大家登录使用Linux服务器时,不同职能的员 ...
- makefile学习(1)
GNU Make / Makefile 学习资料 GNU Make学习总结(一) GNU Make学习总结(二) 这篇学习总结,从一个简单的小例子开始,逐步加深,来讲解Makefile的用法. 最后用 ...
- [USACO]奶牛博览会(DP)
Description 奶牛想证明他们是聪明而风趣的.为此,贝西筹备了一个奶牛博览会,她已经对N头奶牛进行了面试,确定了每头奶牛的智商和情商. 贝西有权选择让哪些奶牛参加展览.由于负的智商或情商会造成 ...
- WTForm
Flask-WTForm: from flask import Flask,render_template,request,redirect from wtforms.fields import co ...
- Dsamain
TechNet 库 Windows Server Windows Server 2008 R2 und Windows Server 2008 Windows Server 命令.参考和工具 Comm ...
- WampServer配置说明
注意:所有的修改操作都要重启WampServer服务器,部分需要重启WampServer软件 1.修改默认端口 1)打开文件:C:\wamp\bin\apache\apache2.4.9\conf\h ...
- 设计模式之第6章-迭代器模式(Java实现)
设计模式之第6章-迭代器模式(Java实现) “我已经过时了,就不要讲了吧,现在java自带有迭代器,还有什么好讲的呢?”“虽然已经有了,但是具体细节呢?知道实现机理岂不美哉?”“好吧好吧.”(迭代器 ...
- 我对于js注入的理解
资料:http://blog.csdn.net/gisredevelopment/article/details/41778671 js注入就是在前端利用使用js的地方 在这其中注入你写的js代码 使 ...
- Python/PHP 远程文件/图片 下载
php 实现远程图片下载并保存到本地 /* *功能:php完美实现下载远程图片保存到本地 *参数:文件url,保存文件目录,保存文件名称,使用的下载方式 *当保存文件名称为空时则使用远程文件原来的名称 ...
- sqlserver释放内存
create procedure sp_clearmemasbegin dbcc freeproccache dbcc freesessioncache dbcc freesystemcache('a ...