1. 程式人生 > >HDU 5952 Counting Cliques(無向圖定向搜尋)

HDU 5952 Counting Cliques(無向圖定向搜尋)

Counting Cliques

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

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

Source
2016ACM/ICPC亞洲區瀋陽站-重現賽(感謝東北大學)

Recommend
jiangzijing2015

題目大意

  有一張最多100個點的無向圖,求大小為S的團有多少個。

解題思路

  最大團極大團問題是NP問題,所以這題也很自然地想到搜尋。不過如果直接暴搜的話可以發現對於每個大小為S的團會被搜到S次。考慮怎麼優化這裡,考慮對於任一一個團一定有且僅有一條按照頂點序號走過所有點的路徑。所以我們就可以對於每條邊定向為從編號小的點到編號大的點。這樣再暴搜的時候就優化了非常多,就可以通過這道題了。

AC程式碼

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f

const int MAXV=100+3;

int V, E, S;
vector<int> G[MAXV];
bool maze[MAXV][MAXV];
int ans;
int save[MAXV];

void init()
{
    for(int i=0;i<=V;++i)
        G[i].clear();
    for(int i=0;i<=V;++i)
        for(int j=0;j<=V;++j)
            maze[i][j]=false;
    ans=0;
}

void dfs(int u, int n)
{
    if(n==S)
    {
        ++ans;
        return ;
    }
    for(int i=0;i<G[u].size();++i)
    {
        int v=G[u][i];
        bool ok=true;
        for(int j=0;j<n;++j)
            if(!maze[save[j]][v])
            {
                ok=false;
                break;
            }
        if(!ok)
            continue;
        save[n]=v;
        dfs(v, n+1);
    }
}

int main()
{
    int T_T;
    scanf("%d", &T_T);
    while(T_T--)
    {
        scanf("%d%d%d", &V, &E, &S);
        init();
        for(int i=0;i<E;++i)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            if(u<=v)
            {
                G[u].push_back(v);
                maze[u][v]=true;
            }
            else
            {
                G[v].push_back(u);
                maze[v][u]=true;
            }
        }
        for(int u=0;u<V;++u)
            if(G[u].size()+1>=S)
            {
                save[0]=u;
                dfs(u, 1);
            }
        printf("%d\n", ans);
    }

    return 0;
}

相關推薦

HDU 5952 Counting Cliques定向搜尋

Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3480

HDU 5952 Counting Cliquesdfs

lap ont there ins -- icpc output stream script Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja

HDU 4280 Island Transport最大流

clear ofa pri img size http algorithm caller end HDU 4280:http://acm.hdu.edu.cn/showproblem.php?pid=4280 題意:   比較裸的最大流題目,就是這是個無向圖,並且比較卡

18.11.08 HDU 4738 Caocao's Bridges求橋

描述 Caocao was defeated by Zhuge Liang and ZhouYu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good at water battles, so he

HDU 5952 Counting Cliques暴搜

Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

:最小生成樹:prim演算法 普里姆演算法 ,的實現

#ifndef _GRAPH_H #define _GRAPH_H #define  DEFAULT_VERTEX_SIZE 10 typedef struct {     int x;     int y;     int  cost; }edge; template&l

HDU - 6311 Cover的最少路徑邊覆蓋 歐拉路徑

algorithm 奇數 detail cos log csdn puts true its 題意 給個無向圖,無重邊和自環,問最少需要多少路徑把邊覆蓋了。並輸出相應路徑 分析 首先聯通塊之間是獨立的,對於一個聯通塊內,最少路徑覆蓋就是 max(1,度數為奇數點的個

HDU1845Jimmy’s Assignment,最大匹配

comment 最大匹配 tex dfs asc ddc repr freopen ces 題意:就是求最大匹配 #include<cstdio> #include<iostream> #include<algorit

hdu 6041 I Curse Myself 找環+優先隊列

ger update cst scan ges mst des sim search I Curse Myself Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/

[HDOJ6081] 度度熊的王國戰略最小割,數據水

eof printf ret pri sin %d logs ems ++ 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=6081 無向圖求割點,應該是個論文題,16年有一篇SW算法+斐波那契堆優化的論文。 但是這數據怎麽這!

The Necklace UVA - 10054 的歐拉回路

n) 兩個 logs nec get dfs lap none view The Necklace UVA - 10054 題意:每個珠子有兩個顏色,給n個珠子,問能不能連成一個項鏈,使得項鏈相鄰的珠子顏色相同。 把顏色看做點,珠子內部連一條邊,無向圖求歐拉回路。 這

UVA 315 :Network 求割頂

time_t long == i++ while 只有一個 ext scu pri 題目鏈接 題意:求所給無向圖中一共有多少個割頂 用的lrj訓練指南P314的模板 #include<bits/stdc++.h> using namespace std; ty

HDU - 5952 Counting Cliques (dfs)

-- wap open col set cst play bsp sca 題目鏈接: Counting Cliques 題意:一個有N個點M條邊的圖,球其中由S個點構成的團的個數。一個團是一個完全子圖。 題解:拿到這題想了好久。。沒想到dfs就完事了。就dfs一下,回溯一下

HDU - 5952 Counting Cliques(DFS)

maximum you memset dfs ons let iostream 記錄 tween A clique is a complete graph, in which there is an edge between every pair of the vertic

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 co

藍書演算法競賽進階指南刷題記錄——POJ1734 Sightseeing trip最小環

題目:poj1734. 題目大意:給定一張無向圖,求這張無向圖邊權和最小的節點大於3個的環,若有解輸出任意一個方案,否則輸出“No solution.”. 這就是一個較為簡單的floyd應用. 我們可以先把floyd模板寫下來看看floyd有什麼特殊的性質: void floyd

實驗六:的鄰接矩陣儲存方式

源程式: # include<iostream> using namespace std; const int MAXSIZE=10; int visited[MAXSIZE]={0}; class MGraph { public: MGraph(char a[],

How Many Maos Does the Guanxi Worth最短路徑的最大值

Guanxi" is a very important word in Chinese. It kind of means "relationship" or "contact". Guanxi can be based on friendship, but also can be built on

六度分離 最短路徑問題

1967年,美國著名的社會學家斯坦利·米爾格蘭姆提出了一個名為“小世界現象(small world phenomenon)”的著名假說,大意是說,任何2個素不相識的人中間最多隻隔著6個人,即只用6個人就可以將他們聯絡在一起,因此他的理論也被稱為“六度分離”理論(six degrees of sepa

hdu-5952 Counting Cliques(dfs&&優化)

完全圖概念 題意: 給一個無向圖,N(0-100)個頂點,M(0-1000)條邊,求圖中頂點數為S(1-10)的完全圖(任意兩個頂點都有一條邊相連)的個數。 思路: 最開始還以為是環,這鍋sls得背啊哈哈哈哈。 題解都在註釋裡了。 #include <ios