hdu5952 Counting Cliques 技巧dfs
题意:一个有N个点M条边的图,球其中由S个点构成的团的个数。一个团是一个完全子图。
没有什么好办法,只有暴力深搜,但是这里有一个神奇的操作:将无向图转为有向图:当两个点编号u<v时才有边u->v,这样的好处是一张完全图只有从最小编号开始深搜的时候才会搜完整张完全图(本来完全图从其中任意一个节点进入都是没有区别的),因为假如v1 < v2,并且v1,v2在同一完全图中,则v2只会走到编号更大的点,这样每张大小为S的完全图只有一条固定的路,所以不会重复。从1~N个点每个点开始dfs,这样即使两个完全图的点集有交集,但因为每张完全图的编号最小的那个点都会被遍历到,所以不会遗漏。
然后问题就是怎么搜了,考虑这样的情况,v1,v2...vn构成一个完全图,并且v0与v1连了一条有向边。那会不会出现这样的情况 1:从v0开始dfs,找到了v1~n这个完全图;2:从v1开始dfs,又找到了v1~n这张图。那不是重复了吗?
为了解决这个问题,我们需要保证搜的点都在完全图上,即用path数组记录当前走过的路径,当准备dfs下一个点v时,判断v是不是和前面路径所有点有边相连(所以还是需要记录双向边的图),如果边数量不够或者没有全部相连直接返回,证明这个点不在这个完全图中。
#include <iostream>
#include <string.h>
#include <cstdio>
#include <queue>
#include <set>
#include <stack>
#include <math.h>
#include <string>
#include <algorithm> #define SIGMA_SIZE 26
#define pii pair<int,int>
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) (x&-x)
#define fode(i, a, b) for(int i=a; i>=b; i--)
#define foe(i, a, b) for(int i=a; i<=b; i++)
#define fod(i, a, b) for(int i=a; i>b; i--)
#define fo(i, a, b) for(int i=a; i<b; i++)
//#pragma warning ( disable : 4996 ) using namespace std;
typedef long long LL;
inline LL LMax(LL a, LL b) { return a>b ? a : b; }
inline LL LMin(LL a, LL b) { return a>b ? b : a; }
inline LL lgcd(LL a, LL b) { return b == ? a : lgcd(b, a%b); }
inline LL llcm(LL a, LL b) { return a / lgcd(a, b)*b; } //a*b = gcd*lcm
inline int Max(int a, int b) { return a>b ? a : b; }
inline int Min(int a, int b) { return a>b ? b : a; }
inline int gcd(int a, int b) { return b == ? a : gcd(b, a%b); }
inline int lcm(int a, int b) { return a / gcd(a, b)*b; } //a*b = gcd*lcm
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod = 1e9+;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const int maxk = 1e5+;
const int maxm = *;
const int maxn = ; int n, m, s, ans;
int path[maxn];
bool g[maxn][maxn];
vector<int> vv[maxn]; void init()
{
cin >> n >> m >> s; int x, y; ans = ;
memset(g, , sizeof(g));
memset(path, , sizeof(path));
foe(i, , n) vv[i].clear();
foe(i, , m)
{
scanf("%d %d", &x, &y);
g[x][y] = g[y][x] = true;
vv[Min(x,y)].push_back(Max(x, y));
}
} void dfs(int x, int d)
{
if (d == s) { ans++; return; }
//if (vv[x].size()+d < s) return; for ( int i = ; i < vv[x].size(); i++ )
{
int v = vv[x][i];
bool flag = false; for ( int i = x; i; i = path[i] )
{
if (!g[v][i]) flag = true;
if (vv[v].size()++d < s) flag = true;
if (flag) break;
} if (!flag)
{
path[v] = x;
dfs(v, d+);
}
}
} int main()
{ #ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif int T; cin >> T;
while(T--)
{
init();
foe(i, , n)
{
path[i] = ;
dfs(i, );
}
printf("%d\n", ans);
} return ;
}
hdu5952 Counting Cliques 技巧dfs的更多相关文章
- HDU5952 Counting Cliques计算完全图的个数 巧妙构图+dfs
题目传送门 题目大意:给出n个点,m条无向边,让你计算这幅母图中有几个大小为s的完全图. 完全图的意思是任意一个点都和其他点直接相连,完全图的大小指的就是完全图点的个数. 思路:比较巧妙的构图方式.我 ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU5952 Counting Cliques (暴力深搜+剪枝) (2016ACM/ICPC亚洲赛区沈阳站 Problem E)
题目链接:传送门 题目: Counting Cliques Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total S ...
- HDU 5952 Counting Cliques(dfs)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- [HDOJ5952]Counting Cliques(DFS,剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5952 题意:求图中规模为s的团的个数. DFS+剪枝,姿势不好很容易TLE啊. #include &l ...
- HDU5952 Counting Cliques 暴搜优化
一.前言 这题看上去相当唬人(NPC问题),但是 因为限制了一些条件,所以实际上并没有太唬人. 二.题目 给你一个图,要求你找出数量为S的团的数量. 三.题解 暴搜,再加上一些玄学优化. 优化1:使用 ...
- HDU - 5952 Counting Cliques
Counting Cliques HDU - 5952 OJ-ID: hdu-5952 author:Caution_X date of submission:20191110 tags:dfs,gr ...
- 【算法系列学习】巧妙建图,暴搜去重 Counting Cliques
E - Counting Cliques http://blog.csdn.net/eventqueue/article/details/52973747 http://blog.csdn.net/y ...
- Counting Cliques(HDU-5952)【DFS】
题目链接:https://vjudge.net/problem/HDU-5952 题意:有一张无向图,求结点数量为S的团的数量. 思路:如果不加一点处理直接用DFS必然会超时,因为在搜索过程中会出现遍 ...
随机推荐
- SP2713 GSS4 - Can you answer these queries IV(线段树)
传送门 解题思路 大概就是一个数很少次数的开方会开到\(1\),而\(1\)开方还是\(1\),所以维护一个和,维护一个开方标记,维护一个区间是否全部为\(1/0\)的标记.然后每次修改时先看是否有全 ...
- 了解Metasploit中的Payloads(有效载荷)
什么是payload? payload又称为攻击载荷,主要是用来建立目标机与攻击机稳定连接的,可返回shell,也可以进行程序注入等.也有人把payloads称 为shellcode. Shellco ...
- hadoop 8088 看不到mapreduce 任务的执行状态
进到8088页面后,怎么看不到mapreudce的执行状态,有哪里需要配置的 解决办法: 在$HADOOP_HOME/conf/mapred-site.xml 在原来的配置文件基础之上添加: < ...
- python简介与安装
Python简介和环境搭建 于 20世纪80年代末,Guido van Rossum发明了Python,初衷据说是为了打发圣诞节的无趣.1991年首次发布,是ABC语言的继承,同时也是一种脚本语言.取 ...
- pytorch clamp 与clamp_区别
pytorch clamp 与clamp_ ,有下划线的表示修改并付给自身,无下划线的表示需要返回处理后的值,比如: h = k.clamp(min=0) #将结果存入h,k保留原值 k.clamp_ ...
- 启动 AXD 配置开发板
1. 启动 AXD 先启动 DragonICE Server 程序. 按如下步聚启动 AXD: 开始>所有程序>ARM Developer Suite v1.2>AXD De ...
- MySQL数据库之DQL(数据查询语言)
1.MySQL之DQL查询AS CONCAT LIKE的使用 (1)select 列名1,列名2,...... from 表名 [where 条件] 查询所有字段用*,不带where条件的话,就会把表 ...
- xampp只允许本地访问,禁止远程访问
远程访问phpmyadmin的时候出现错误 New XAMPP security concept: Access to the requested object is only available f ...
- VC++中的CString、char、int类型转换
1.如何将CString类型的变量赋给char*类型的变量 方法一:GetBuffer函数 使用CString::GetBuffer函数. char *p; CString str=&quo ...
- SQLite wrapper
SQLiteWrapper is a C++ wrapper for SQLite. There are some test programs that demonstrate how the SQL ...