Counting Cliques

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1855    Accepted Submission(s): 735

Problem Description
A clique is a complete graph, in which there is an edge between every pair of the vertices. Given a graph with N vertices and M edges, your task is to count the number of cliques with a specific size S in the graph. 
 
Input
The first line is the number of test cases. For each test case, the first line contains 3 integers N,M and S (N ≤ 100,M ≤ 1000,2 ≤ S ≤ 10), each of the following M lines contains 2 integers u and v (1 ≤ u < v ≤ N), which means there is an edge between vertices u and v. It is guaranteed that the maximum degree of the vertices is no larger than 20.
 
Output
For each test case, output the number of cliques with size S in the graph.
 
Sample Input
3
4 3 2
1 2
2 3
3 4
5 9 3
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
6 15 4
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6
 
Sample Output
3
7
15
 
Source
 
Recommend
jiangzijing2015

题意:

  给你n个点,m条边,要你在这些点里面找大小为s 的完全图(完全图是指这个图里任意两点都有一条边)。

  因为 N 只有100个。S最大也才10。所以我们可以爆搜来解决。然后我就T了  :(

  因为 1 2 3 如果是完全图的话,那么  2 1 3 也是。所以我们多搜了很多次。

  如果我们建边的时候是按照 小的指向 大的点来建,就可以避免了很多情况。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
const LL INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int maxn = +;
bool edge[maxn][maxn];
vector <int> E[maxn];
int num[maxn];
int p[];
int ans, n, m, s, u, v;
void init() {
ms(edge, );
ms(num, );
for(int i = ;i<maxn;i++) E[i].clear();
ans = ;
}
void dfs(int x, int len){
int flag = ;
for(int i=;i<len;i++){
if(!edge[x][p[i]]){
flag = ;
break;
}
}
if(!flag){
return;
}
p[len] = x;
if(len+==s){
ans++;
return;
}
for(int i = ;i<E[x].size();i++){
dfs(E[x][i], len+);
}
}
void solve() {
scanf("%d%d%d", &n, &m, &s);
for(int i = ;i<m;i++){
scanf("%d%d", &u, &v);
E[min(u, v)].pb(max(u, v));//小的点指向大的点
edge[u][v] = edge[v][u] = ;
num[u]++;
num[v]++;
}
for(int i = ;i<=n;i++){
if(num[i]<s-) continue;
dfs(i, );
}
printf("%d\n", ans);
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int T;
scanf("%d", &T);
while(T--){
init();
solve();
}
return ;
}

HDU 5952 Counting Cliques(dfs)的更多相关文章

  1. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. HDU - 5952 Counting Cliques(dfs搜索)

    题目: A clique is a complete graph, in which there is an edge between every pair of the vertices. Give ...

  3. HDU - 5952 Counting Cliques

    Counting Cliques HDU - 5952 OJ-ID: hdu-5952 author:Caution_X date of submission:20191110 tags:dfs,gr ...

  4. 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)

    题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...

  5. hdu 1716 排序2(dfs)

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. [HDOJ5952]Counting Cliques(DFS,剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5952 题意:求图中规模为s的团的个数. DFS+剪枝,姿势不好很容易TLE啊. #include &l ...

  7. hdu 3887 Counting Offspring(DFS序【非递归】+树状数组)

    题意: N个点形成一棵树.给出根结点P还有树结构的信息. 输出每个点的F[i].F[i]:以i为根的所有子结点中编号比i小的数的个数. 0<n<=10^5 思路: 方法一:直接DFS,进入 ...

  8. 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 ...

  9. HDU 3887 Counting Offspring(DFS序+树状数组)

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

随机推荐

  1. 剑指offer--day10

    1.1 题目:二叉搜索树的后序遍历序列:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 1.2 思路: 以{ ...

  2. SQLServer中的Merge使用

    Merge DML 作用: 数据同步 数据转换 基于源表对目标表做Insert,Update,Delete操作 Merge关键字的一些限制 使用Merge关键字只能更新一个表 源表中不能有重复的记录 ...

  3. CSU-1110 RMQ with Shifts (单点更新+区间最小值 zkw线段树)

    In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query ...

  4. 搜索---BFS

    搜索   深度优先搜索和广度优先搜索广泛的应用于树和图中,但是他们的应用远不止于此. BFS   广度优先搜索一层一层地进行遍历,每层遍历都是以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点 ...

  5. 【JAVA】增强for循环for(int a : arr)

    介绍 这种有冒号的for循环叫做foreach循环,foreach语句是java5的新特征之一,在遍历数组.集合方面,foreach为开发人员提供了极大的方便. foreach语句是for语句的特殊简 ...

  6. sftp没有关闭session导致服务器sshd进程未关闭

    项目中需要用Sftp上传下载文件,通过jsch中的sftp实现.代码上了服务器之后,发觉服务器多了很多进程没有被关闭. 连接sftp代码: protected boolean connectToSer ...

  7. 启动ZOOKEEPER之后能查看到进程存在但是查不到状态,是因为。。。

    一般我们在启动ZOOKEEPER之后能查看到进程并且能查到每个节点的状态,但是新手偶尔会遇到查不到状态的问题,这里主要说一下我自己遇到的问题. 是因为myid重复了.... 错误:总共三个节点,mas ...

  8. linux相关认证和权限配置

    [root@rsync-server-1 /]# echo 'rsync_backup:redhat' > /etc/rsync.password [root@rsync-server-1 /] ...

  9. linux配置 sudo 授权管理

    为什么使用 sudo,如果普通用户使用 su - root 切换到管理员.进行非法操作,比如 passwd root 修改 root 密码.那么系统其他用户将无法访问系统.这个普通管理员说白了,已经” ...

  10. linux 服务器与客户端异常断开连接问题

    服务器与客户端连接,客户端异常断掉之后服务器端口仍然被占用, 到最后是不是服务器端达到最大连接数就没法连接了?领导让我测试这种情况,我用自己的电脑当TCP Client,虚拟机当服务器,连接之后能正常 ...