HDU5952 Counting Cliques计算完全图的个数 巧妙构图+dfs
题目大意:给出n个点,m条无向边,让你计算这幅母图中有几个大小为s的完全图。
完全图的意思是任意一个点都和其他点直接相连,完全图的大小指的就是完全图点的个数。
思路:比较巧妙的构图方式。我们会很自然地想到用dfs来找环,然后记录路径,判断是否成完全图,但是由于题目给的是双向边,如果直接构图的话,就会导致出现很多没有必要的情况,重复计算,爆栈超时。
所以在建图的时候只建从小的点到大的点的单向边,然后对n个点从小到大进行dfs,这样可以既可以保证不会有遗留的情况,也不会重复计算(因为每次走都是从小的点走到大的点,由于是单向边,所以不会回头)。dfs记录路径后,每次放入一个点,把之前路径上的所有点和这个点比较一下,是否是可达的,如果是,则继续dfs下一层,如果不是,则代表这个点无法和路径上的点一起构成完全图,舍弃,找下一条路。
我存图的方式是链式前向星,这样可以减少遍历的次数,而判断完全图的方式是直接用邻接矩阵,节约时间。(也就是我存了两幅一样的图)。
反思:还是一道赛后猛如虎的题目啊,比赛的时候想到用单向边构图,但是当时以为单向边判不了完全图(a能到b,我怎么知道b能不能到a),然后就没想下去了,实际上和答案只差一点点了呀!!
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#define CLR(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
inline int rd(void) {
int x=0; int f=1;char s=getchar();
while(s<'0'||s>'9') { if(s=='-')f=-1; s=getchar();}
while(s>='0'&&s<='9') { x=x*10+s-'0';s=getchar();}
x*=f;return x;
}
int n,m,s,ans;
int head[1010],tot,lu[20];
struct edge{
int v,Next;
}edge[1010];
int mp[110][110];
void init(){
CLR(head,-1);
tot=0;
ans=0;
CLR(mp,0);
}
void addv(int u,int v){
edge[++tot].v=v;
edge[tot].Next=head[u];
head[u]=tot;
}
void dfs(int x,int deep){
if(deep==s){
ans++;
return;
}
lu[deep]=x;
for(int i=head[x];i!=-1;i=edge[i].Next){
int flag=1;
int v=edge[i].v;
for(int j=1;j<deep;j++){
if(mp[lu[j]][v]==0){
flag=0;
break;
}
}
if(flag){
dfs(v,deep+1);
}
}
return ;
}
int main(){
int T;
cin>>T;
while(T--){
scanf("%d%d%d",&n,&m,&s);
init();
int a,b;
for(int i=1;i<=m;i++){
scanf("%d%d",&a,&b);
if(a>b)swap(a,b);
mp[a][b]=1;
addv(a,b);
}
for(int i=1;i<=n;i++){
dfs(i,1);
}
printf("%d\n",ans);
}
}
Counting Cliques
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3880 Accepted Submission(s): 1387
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
HDU5952 Counting Cliques计算完全图的个数 巧妙构图+dfs的更多相关文章
- HDU5952 Counting Cliques (暴力深搜+剪枝) (2016ACM/ICPC亚洲赛区沈阳站 Problem E)
题目链接:传送门 题目: Counting Cliques Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total S ...
- hdu5952 Counting Cliques 技巧dfs
题意:一个有N个点M条边的图,球其中由S个点构成的团的个数.一个团是一个完全子图. 没有什么好办法,只有暴力深搜,但是这里有一个神奇的操作:将无向图转为有向图:当两个点编号u<v时才有边u-&g ...
- HDU5952 Counting Cliques 暴搜优化
一.前言 这题看上去相当唬人(NPC问题),但是 因为限制了一些条件,所以实际上并没有太唬人. 二.题目 给你一个图,要求你找出数量为S的团的数量. 三.题解 暴搜,再加上一些玄学优化. 优化1:使用 ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 【算法系列学习】巧妙建图,暴搜去重 Counting Cliques
E - Counting Cliques http://blog.csdn.net/eventqueue/article/details/52973747 http://blog.csdn.net/y ...
- 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)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- for循环计算li的个数
今天有一段代码 在ie6下面显示 我检查了一下代码,发现每for循环一次,就会重新计算li的个数,会拖慢运行速度,所以改成以下代码,ie6就正常了
- 【图像处理】计算Haar特征个数
http://blog.csdn.net/xiaowei_cqu/article/details/8216109 Haar特征/矩形特征 Haar特征本身并不复杂,就是用图中黑色矩形所有像素值的和减去 ...
随机推荐
- Javascript面向对象(三):非构造函数的继承
这个系列的第一部分介绍了"封装",第二部分介绍了使用构造函数实现"继承". 今天是最后一个部分,介绍不使用构造函数实现"继承". 一.什么是 ...
- MySQL在cmd和python下的常用操作
环境配置1:安装mysql,环境变量添加mysql的bin目录 环境配置2:python安装MySQL-Python 请根据自身操作系统下载安装,否则会报c ++ compile 9.0,import ...
- sizeof总结
1.sizeof常用总结 ①与strlen比较 strlen 计算字符串的字符数,以"\0"为结束判断,但不统计结束符. sizeof 计算数据(数组.变量.类型. ...
- 爬取google的搜索结果并保存
demo: #coding:utf- import requests from bs4 import BeautifulSoup import bs4 import re def getHTMLTex ...
- linux中创建图片服务器减轻传统服务器的压力
1.1. 传统项目中的图片管理 传统项目中,可以在web项目中添加一个文件夹,来存放上传的图片.例如在工程的根目录WebRoot下创建一个images文件夹.把图片存放在此文件夹中就可以直接使用在 ...
- (转)嵌入式C开发人员的最好笔试题目
约定: 1) 下面的测试题中,认为所有必须的头文件都已经正确的包含了 2)数据类型 char 一个字节 1 byte int 两个字节 2 byte ( ...
- Windows Live Writer 使用指南
一.简介 Windows Live Writer 是一个强大的离线博客编辑工具,通过它可以离线编辑内容丰富的博文,除了自身强大的编辑功能之外,还提供了接口,让其它开发人员通过插件提供工具自身没有提供的 ...
- R语言输出pdf时,中文乱码处理
本文转载自:https://blog.csdn.net/hongweigg/article/details/47907555 1.使用基础包,使用函数pdf()输出 在使用pdf()函数时,要输出中文 ...
- 在windows中安装OpenSSH,无密码登录,永远不断线
到这里下载最新安装包:https://github.com/PowerShell/Win32-OpenSSH/releases下载下来解压,然后用管理员打开服务器自带的PowerShell,运行下列命 ...
- string为什么是final?源码分析
http://blog.csdn.net/zhangjg_blog/article/details/18319521