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.
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.
题意:
给出一个图有n个点、m条边,给出子图的大小s,要求求出子图是一个完全图,而且图中需要有s个点。
PS:
我竟然把这个题目读成了求图中点数为s的环的个数,这个锅背的很强,,,,
首先回顾一下完全图的性质:完全图中任意一个点与其他的所有的点都有连边。如下:

所以有n个点的完全图会有n*(n-1)/2条边。
思路:这个题如果双向建图,然后遍历求点数为s的完全图,铁定TLE,因为同一个图会被多次搜索。要避免重复搜索,可以单向建图,从小到大dfs遍历,同时在path数组中记录已经在完全图中的点,只要当前的点和之前path中的点都有边(这样path中所有的点才能构成完全图),就将该点记录到path中,点数达到s,答案ans++。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;
const int maxn = +;
int m,n,s,ans;
int mp[maxn][maxn];
int path[maxn];
vector<int> G[maxn]; int read()
{
int res = ;
char op = getchar();
if(op>='' && op<='')
{
res = op-'';
op = getchar();
}
while(op>='' && op<='')
{
res = res* + op-'';
op = getchar();
}
return res;
} void init()
{
ans = ;
memset(mp,,sizeof(mp));
memset(path,,sizeof(path));
for(int i = ; i<maxn; i++)
G[i].clear();
} void dfs(int i,int cnt)
{
if(cnt == s)
{
ans++;
return;
}
for(int j = ; j<G[i].size(); j++)
{
int u = G[i][j],f = ;
for(int k = ; k<=cnt; k++)//看点u是否和path中的点是否都有边相连
{
if(mp[u][path[k]] == )
{
f = ;
break;
}
}
if(!f)//都想连就存入path继续遍历
{
path[cnt+] = u;
dfs(u, cnt+);
path[cnt+] = ;
}
}
} int main()
{
int T;
T = read();
//scanf("%d",&T);
while(T--)
{
init();
n = read(),m = read(),s= read();
//scanf("%d%d%d",&n,&m,&s);
for(int i = ; i<m; i++)
{
int st,en;
st = read(),en = read();
//scanf("%d%d",&st,&en);
if(st>en)//单向建图,避免重复遍历
G[en].push_back(st);
else
G[st].push_back(en);
mp[st][en] = ,mp[en][st] = ;//在遍历的时候用来检查是否能构成完全图
}
for(int i = ; i<=n; i++)
{
path[] = i;//记录拿出的可以构成完全图的点
dfs(i,);
path[] = ;
}
printf("%d\n",ans);
}
return ;
}
HDU - 5952 Counting Cliques(dfs搜索)的更多相关文章
- 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 ...
- 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
Counting Cliques HDU - 5952 OJ-ID: hdu-5952 author:Caution_X date of submission:20191110 tags:dfs,gr ...
- hdu 5952 Counting Cliques 求图中指定大小的团的个数 暴搜
题目链接 题意 给定一个\(n个点,m条边\)的无向图,找出其中大小为\(s\)的完全图个数\((n\leq 100,m\leq 1000,s\leq 10)\). 思路 暴搜. 搜索的时候判断要加进 ...
- Counting Cliques HDU - 5952 单向边dfs
题目:题目链接 思路:这道题vj上Time limit:4000 ms,HDU上Time Limit: 8000/4000 MS (Java/Others),且不考虑oj测评机比现场赛慢很多,但10月 ...
- hdu 3887 Counting Offspring dfs序+树状数组
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 2952 Counting Sheep(DFS)
题目链接 Problem Description A while ago I had trouble sleeping. I used to lie awake, staring at the cei ...
- HDU 1241 Oil Deposits DFS搜索题
题目大意:给你一个m*n的矩阵,里面有两种符号,一种是 @ 表示这个位置有油田,另一种是 * 表示这个位置没有油田,现在规定相邻的任意块油田只算一块油田,这里的相邻包括上下左右以及斜的的四个方向相邻的 ...
随机推荐
- bzoj3550: [ONTAK2010]Vacation&&bzoj3112: [Zjoi2013]防守战线
学了下单纯形法解线性规划 看起来好像并不是特别难,第二个code有注释.我还有...*=-....这个不是特别懂 第一个是正常的,第二个是解对偶问题的 #include<cstdio> # ...
- bzoj 1753: [Usaco2005 qua]Who's in the Middle【排序】
--这可能是早年Pascal盛行的时候考排序的吧居然还是Glod-- #include<iostream> #include<cstdio> #include<algor ...
- 清北考前刷题da5下午好
/* (4,1)*(3,1)*(2,1)的话1变成2然后一直是2 2变成1然后变成3 3变成1然后变成4 4变成1 */ #include<iostream> #include<cs ...
- event.target 属性返回哪个 DOM 元素触发了事件。
<ul> <li>list <strong>item 1</strong></li> <li><span>list ...
- 洛谷P3383 【模板】线性筛素数 (埃拉托斯特尼筛法)
题目描述 如题,给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内) 输入输出格式 输入格式: 第一行包含两个正整数N.M,分别表示查询的范围和查询的个数. 接下来M行每行 ...
- java dom4j xml生成,解析
1. 用Java代码生成xml文档 package com.test.dom; import java.io.FileOutputStream; import java.io.IOException; ...
- android序列化(2)Parcelable与Parcel
1.简介 Parcel : 包裹 Android采用这个它封装消息数据.这个是通过IBinder通信的消息的载体.需要明确的是Parcel用来存放数据的是内存(RAM),而不是永久性介质(Nand等 ...
- 236 Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先
给定一棵二叉树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tre ...
- linux centos7 安装nginx并启动
Linux下安装Nginx完整教程及常见错误解决方案:https://blog.csdn.net/chenxiaochan/article/details/63688346 CentOS 7 安装Ng ...
- git reset作用
git reset: 1. 文件从暂存区回退到工作区,撤销add 2. 版本回退 一:文件从暂存区回退到工作区,撤销add 如果想取消某个add的文件,可以使用该命令来进行撤销操作 撤消add:gi ...