HDU 5952 Counting Cliques(dfs)

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

2016ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

Recommend

jiangzijing2015

题意:

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

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

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

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

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <algorithm>
 6 #include <cmath>
 7 #include <vector>
 8 #include <queue>
 9 #include <map>
10 #include <stack>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 #define ms(a, b) memset(a, b, sizeof(a))
15 #define pb push_back
16 #define mp make_pair
17 const LL INF = 0x7fffffff;
18 const int inf = 0x3f3f3f3f;
19 const int mod = 1e9+7;
20 const int maxn = 100+10;
21 bool edge[maxn][maxn];
22 vector <int> E[maxn];
23 int num[maxn];
24 int p[20];
25 int ans, n, m, s, u, v;
26 void init() {
27     ms(edge, 0);
28     ms(num, 0);
29     for(int i = 0;i<maxn;i++)   E[i].clear();
30     ans = 0;
31 }
32 void dfs(int x, int len){
33     int flag = 1;
34     for(int i=0;i<len;i++){
35         if(!edge[x][p[i]]){
36             flag = 0;
37             break;
38         }
39     }
40     if(!flag){
41         return;
42     }
43     p[len] = x;
44     if(len+1==s){
45         ans++;
46         return;
47     }
48     for(int i = 0;i<E[x].size();i++){
49         dfs(E[x][i], len+1);
50     }
51 }
52 void solve() {
53     scanf("%d%d%d", &n, &m, &s);
54     for(int i = 0;i<m;i++){
55         scanf("%d%d", &u, &v);
56         E[min(u, v)].pb(max(u, v));//小的点指向大的点
57         edge[u][v] = edge[v][u] = 1;
58         num[u]++;
59         num[v]++;
60     }
61     for(int i = 1;i<=n;i++){
62         if(num[i]<s-1)  continue;
63         dfs(i, 0);
64     }
65     printf("%d\n", ans);
66 }
67 int main() {
68 #ifdef LOCAL
69     freopen("input.txt", "r", stdin);
70 //        freopen("output.txt", "w", stdout);
71 #endif
72     int T;
73     scanf("%d", &T);
74     while(T--){
75         init();
76         solve();
77     }
78     return 0;
79 }

时间: 2024-08-26 23:28:59

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

hdu 1716 排序2(dfs)

排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5817    Accepted Submission(s): 2229 Problem Description Ray又对数字的列产生了兴趣:现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡片上的数字(

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

题意: N个点形成一棵树.给出根结点P还有树结构的信息. 输出每个点的F[i].F[i]:以i为根的所有子结点中编号比i小的数的个数. 0<n<=10^5 思路: 方法一:直接DFS,进入结点x时记录一下比x小的数的个数.出来x时记录一下比x小的数的个数.相减就是F[x].结合树状数组. 方法二:写下DFS序.对DFS序列建线段树.然后从小到大对结点进行插入.用线段树统计. 代码:(方法一) int const N=1e5+5; int n,p; vector<int> G[N];

HDU 3887 Counting Offspring(DFS序)

Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3054    Accepted Submission(s): 1031 Problem Description You are given a tree, it’s root is p, and the node is numbered from 1

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 graph with N vertices and M edges, your task is to count the number of cliques with a specific size S in the graph. InputThe first line is the number

HDU 4414 Finding crosses(dfs)

Problem Description The Nazca Lines are a series of ancient geoglyphs located in the Nazca Desert in southern Peru. They were designated as a UNESCO World Heritage Site in 1994. The high, arid plateau stretches more than 80 kilometres (50 mi) between

HDU 6351 Beautiful Now(DFS)多校题解

思路:一开始对k没有理解好,k是指最多k次,不需要达到.这道题dfs暴力就行,我们按照全排列最大最小去找每一位应该和后面哪一位交换.k = 0没判断好WA了2发... 如果k >= len - 1,那么最大最小就是直接sort非前导零的答案.如果k < len - 1,那么我们交换肯定从最大位数交换,比如现在求最大值,那么我们从第一位依次判断,如果该位不是他后面最大的,那么就和后面最大的交换(如果最大的有多个,那么就每个都搜索一下),否则不消耗交换次数判断下一位.最小同理 注意前导零. 代码:

HDU 2553(N皇后)(DFS)

http://acm.hdu.edu.cn/showproblem.php?pid=2553 i表示行,map[i]表示列,然后用DFS遍历回溯 可以参考这篇文章: http://blog.csdn.net/cambridgeacm/article/details/7703739 1 #include <iostream> 2 #include <string> 3 #include <cstring> 4 #include <cstdlib> 5 #inc

HDU 5012 骰子旋转(DFS)

http://acm.hdu.edu.cn/showproblem.php?pid=5012 保存骰子的状态,然后用dfs或者bfs搜索 还是再讲一下dfs 我们的目标是找一个与b相同,且转次数最少的状态 dfs就是树状图,要明确每个状态下的分支,以及边界条件 有4种变换,所以每个状态下面有四种分支,又因为骰子转4次以上的状态没有意义,所以边界条件可以是4 每个状态起始时与b判断,如果相同,则更新结果 #include <iostream> #include <string> #i

HDU 2660 Accepted Necklace (DFS)

Accepted Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2474    Accepted Submission(s): 973 Problem Description I have N precious stones, and plan to use K of them to make a necklace f