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特征本身并不复杂,就是用图中黑色矩形所有像素值的和减去 ...
随机推荐
- 人工智能一之TensorFlow环境配置
1.安装pip:sudo apt-get install python-pip python-dev 2.定义仅支持CPU的python2.7环境下TensorFlow安装包地址:export TF_ ...
- NoSQL概述
- IE6中浮动双边距bug
想要创建出漂亮的网页设计, 除了要认真学习每一个html和CSS代码之外,不可能不去了解一下臭名昭著的IE6和更早的那些IE浏览器的坏脾气,因为你本来写出的规规矩矩的代码, 漂亮的设计就此就要完成了, ...
- while 用法 for 循环的总结
格式化输出.%s %d # name = input('请输入名字:') # age = input('请输入年龄:') # sex = input('请输入性别:') # # msg = '我的名字 ...
- day70-oracle PLSQL_01基本语法
PLSQL是一种程序,和java一样都是一种程序. sql developer是基于java的jdbc连接数据库.根据java的jdbc,只要有数据库的驱动,就可以连接这个数据库.这个工具默认不需要任 ...
- 激光SLAM Vs 视觉SLAM
博客转载自:https://www.leiphone.com/news/201707/ETupJVkOYdNkuLpz.html 雷锋网(公众号:雷锋网)按:本文作者SLAMTEC(思岚科技公号sla ...
- ZROI2018普转提day2t4
传送门 分析 考场上暴力水过好评... 然后我的st表查询似乎是log的,然后log三方跑的比log方快,qwq. 我们发现如果一个区间的最小值就是这个区间的gcd,则这个区间合法.所以我们二分区间长 ...
- WOJ 10 精英选拔
神仙dp,膜Claris 题意:给一个长度为$n$的数列,求出不超过k次交换后的最大连续子区间和. 发现交换后的最优答案一定是这样的(0和2的长度可以为0) 0 ...
- Entity Framework Tutorial Basics(28):Concurrency
Concurrency in Entity Framework: Entity Framework supports Optimistic Concurrency by default. In the ...
- 数据结构_stack
问题描述 一天,小 L 发现了一台支持一下操作的机器:IN x:将整数 x 入栈POP:将栈顶元素出栈ASUB:出栈两个数,将两数差的绝对值入栈COPY:将栈顶元素(如果有的话)复制一份,入栈现在小 ...