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. LeetCode——707 设计链表

    题目: 总而言之就是要用C++手撸链表,我的代码: class MyLinkedList { public: /** Initialize your data structure here. */ M ...

  2. CSP2019 —— 今年欢笑复明年,不知退役在眼前

    关于2019CSP-J/-S的一些体会 又是一年退役季,想起在群里看到大佬的一句诗,感慨万千. 今年欢笑复明年,不知退役在眼前 于是便心生文意,随便写点东西来给自己康康. 先说说这次的成绩吧.大家应该 ...

  3. 002/Node.js(Mooc)--Http知识

    1.什么是Http 菜鸟教程:http://www.runoob.com/http/http-tutorial.html 视频地址:https://www.imooc.com/video/6713 h ...

  4. .net日志的用法

    public class Logs { private static Logger logger = LogManager.GetCurrentClassLogger(); //初始化日志类 /// ...

  5. CentOS7 修复boot目录

    这里为了达到实验目的,首先删除boot目录下所有内容 重启后发现系统进不去了,这正是我们想要的 进入系统救援模式,以重新引导系统 进入救援模式后,输入以下命令进行修复boot目录 重启后,能正常引导系 ...

  6. jdk 1.7 新增

    二进制整数 JDK7提供了二进制整数的类型,只要以0b开头即可.int a = 0b0101; 下划线分隔符 针对特别长的数字,读懂它令人头疼,这时候用下划线分割数字,可增加代码可读性.long a ...

  7. 2019牛客暑期多校训练营(第五场) - C - generator 2 - BSGS

    https://ac.nowcoder.com/acm/contest/885/C 这个跟平时那种求离散对数的bsgs不一样,虽然可以转化成离散对数来做不过会T掉.展开递推式然后合并具有a的项,发现其 ...

  8. 回溯--- Permutations

    46.Permutations (Medium)](https://leetcode.com/problems/permutations/description/) [1,2,3] have the ...

  9. 加秘钥的SSH

    import paramiko private_key = paramiko.RSAKey.from_private_key_file('id_rsa31.txt') # 创建SSH对象 ssh = ...

  10. MATLAB:非线性规划fmincon

    1.非线性规划的形式: 其中x是一个列向量,st中前两项为线性约束条件,后两项为非线性约束条件. 在MATLAB中fmincon是用于求解非线性多远函数的最小值的函数,这里介绍fmincon的其中一种 ...