Counting Cliques

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 184    Accepted Submission(s): 56

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

思路:构造一个团,如果一个点与这个团的所有点都有边,则将其加入团中,统计含s个点的团的个数。关于优化,可以建单向边来减少搜索量。

代码:

 #include<bits/stdc++.h>
//#include<regex>
#define db double
#include<vector>
#define ll long long
#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define MP make_pair
#define PB push_back
#define inf 0x3f3f3f3f3f3f3f3f
#define fr(i,a,b) for(int i=a;i<=b;i++)
const int N=1e3+;
const int mod=1e9+;
const int MOD=mod-;
const db eps=1e-;
using namespace std;
bool d[][];
int n,m,s,t;
int ans;
vector<int> g[N];
void dfs(int u,int *a,int cnt)
{
if(cnt==s){
ans++;
return;
}
bool ok;
for(int i=;i<g[u].size();i++)
{
ok=;
int v=g[u][i];
for(int j=;j<=cnt;j++){
if(!d[a[j]][v]) {ok=;break;}
}
if(ok)
{
a[++cnt]=v;//加点
dfs(v,a,cnt);//继续搜
a[cnt--]=;
}
}
}
int main(){
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
ci(t);
while(t--)
{
ci(n),ci(m),ci(s);
ans=;
for(int i=;i<=n;i++) g[i].clear();
memset(d,,sizeof(d));
for(int i=;i<m;i++){
int u,v;
ci(u),ci(v);
if(u>v) swap(u,v);
g[u].push_back(v);
d[u][v]=d[v][u]=;
}
for(int i=;i<=n;i++){
if(g[i].size()>=s-){
int a[];
a[]=i;//构建团
int cnt=;
dfs(i,a,cnt);
}
}
pi(ans);
}
return ;
}

hdu 5952 连通子图的更多相关文章

  1. HDU - 5952 Counting Cliques

    Counting Cliques HDU - 5952 OJ-ID: hdu-5952 author:Caution_X date of submission:20191110 tags:dfs,gr ...

  2. 最大半连通子图 bzoj 1093

    最大半连通子图 (1.5s 128MB) semi [问题描述] 一个有向图G = (V,E)称为半连通的(Semi-Connected),如果满足:∀ u, v ∈V,满足u->v 或 v - ...

  3. BZOJ1093 [ZJOI2007]最大半连通子图

    Description 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u ...

  4. BZOJ 1093 [ZJOI2007] 最大半连通子图(强联通缩点+DP)

    题目大意 题目是图片形式的,就简要说下题意算了 一个有向图 G=(V, E) 称为半连通的(Semi-Connected),如果满足图中任意两点 u v,存在一条从 u 到 v 的路径或者从 v 到 ...

  5. BZOJ1093 最大半连通子图

    Description 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意 两点u,v,存在一条u到v的有向路径或者从v到 ...

  6. BZOJ 1093 [ZJOI2007]最大半连通子图

    1093: [ZJOI2007]最大半连通子图 Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 1986  Solved: 802[Submit][St ...

  7. bzoj 1093 [ZJOI2007]最大半连通子图(scc+DP)

    1093: [ZJOI2007]最大半连通子图 Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 2286  Solved: 897[Submit][St ...

  8. BZOJ 1093: [ZJOI2007]最大半连通子图( tarjan + dp )

    WA了好多次... 先tarjan缩点, 然后题意就是求DAG上的一条最长链. dp(u) = max{dp(v)} + totu, edge(u,v)存在. totu是scc(u)的结点数. 其实就 ...

  9. [BZOJ]1093 最大半连通子图(ZJOI2007)

    挺有意思的一道图论. Description 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:∀u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v ...

随机推荐

  1. Android持续集成之Jenkins 部署

    Android持续集成之Jenkins 部署 [TOC] 0x00安装 准备工作如下: Tomcat8.5下载地址 Jenkins下载链接 1 将下载的jenkins.war包放至tomcat下的we ...

  2. ascii codec can't decode byte 0xe8 in position 0:ordinal not in range(128) python代码报错

    import sys reload(sys) sys.setdefaultencoding('utf-8')

  3. jmeter-fileupload操作使用说明

    前言:在http请求过程中上传附件(图片.安装包.视频文件等)虽然基本上Content-Type为:multipart/form-data,但Content-Type也有不一样的,如:图片Conten ...

  4. 生命游戏 Java

    本程序由四个类组成:其中Init_data,用于初始化各个活细胞的状态judge_state,用于判断下一代的细胞状态,并进行更新.set_color,用于给GUI界面中各个细胞涂色set_frame ...

  5. MariaDB体验2----CSV文件导入

    之前已经安装好MariaDB,现在需要将一份从Sql Server数据库里面导出的CSV文件导入进MariaDB,期间碰到了各种坑,这里记录一下. HeidiSQL的导入CSV文件的地方在“工具”栏, ...

  6. 通过官网找到spring的jar包

    1.官网为:https://spring.io/ 2.打开之后,点击:PROJECTS,如图所示: 3.点击第三个:SPRING FRAMEWORK,如图所示: 4.进入之后,找到features,点 ...

  7. 打开safari开发者选项

    1.点击Safari启动浏览器 2.点击左上Safari标志,选择偏好设置 3.选择高级,勾选下方的在菜单栏显示开发菜单. 如此,Safari就出现了开发菜单,右键网页元素也会出现查看元素功能了.

  8. sql in 和 exist的区别

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp41 select * from A where id in(select ...

  9. Linux下设置Tomcat虚拟路径

    问题描述:我在上传图片的位置不在Tomcat服务器下,用户无法访问 解决方案:配置Tomcat虚拟路径使用户可以访问图片 配置Tomcat # cd /usr/local/apache-tomcat- ...

  10. ES6块级作用域

    块级作用域的优点 避免变量冲突,比如程序中加载了多个第三方库的时候,如果没有妥善地将内部私有函数或变量隐藏起来,就很容易引发变量冲突: 可以方便的进行模块管理: 利于内存回收:(块级作用域里声明的变量 ...