Test instructions: A graph with n points m-bars, the number of clusters of which the ball is composed of s points. A regiment is a complete sub-graph.
There is no good way, only violence deep search, but here is a magical operation: the non-direction diagram into a graph: when the two-point number u<v when the Edge u->v, the advantage is a full picture only from the minimum number to start deep search only when the full picture ( There is no difference between a full picture from any of these nodes, because if V1 < v2 and v1,v2 are in the same complete picture, then V2 will only go to a larger numbered point, so that the full graph of size S has only one fixed path, so it will not repeat. DFS starts at each point from the 1~n point, so that even if the point set of the two complete graphs is intersected, it is not omitted because the point with the smallest number of each full graph is traversed.
Then the question is how to search, consider such a situation, v1,v2...vn constitute a complete picture, and V0 and V1 connected with a forward edge. That's not going to happen. 1: Starting from V0 DFS, found v1~n this complete picture; 2: Starting with Dfs from V1, and finding v1~n this picture. Isn't that a repetition?
In order to solve this problem, we need to ensure that the search point is on the complete map, that is, using the path array to record the current path, when the Dfs next point v to determine whether V and the front path all points have the edge (so still need to record the two-way edge of the graph), if the number of edges or not all directly back Prove that this point is not in this complete picture.
1#include <iostream>2#include <string.h>3#include <cstdio>4#include <queue>5#include <Set>6#include <stack>7#include <math.h>8#include <string>9#include <algorithm>Ten One #defineSigma_size 26 A #definePII pair<int,int> - #defineLson rt<<1 - #defineRson rt<<1|1 the #defineLowbit (x) (x&-x) - #defineFode (I, A, b) for (int i=a; i>=b; i--) - #defineFoe (I, A, b) for (int i=a; i<=b; i++) - #defineFOD (i, A, b) for (int i=a; i>b; i--) + #defineFo (i, A, b) for (int i=a; i<b; i++) - //#pragma warning (disable:4996) + A using namespacestd; attypedefLong LongLL; -inline ll LMax (ll A, ll b) {returnA>b?a:b;} -inline ll LMin (ll A, ll b) {returnA>b?b:a;} -inline ll LGCD (ll A, ll b) {returnb = =0? A:LGCD (b, a%b); } -inline ll LLCM (ll A, ll b) {returnA/LGCD (A, b) *b; }//a*b = GCD*LCM -InlineintMax (intAintb) {returnA>b?a:b;} inInlineintMin (intAintb) {returnA>b?b:a;} -InlineintgcdintAintb) {returnb = =0? A:GCD (b, a%b); } toInlineintLcmintAintb) {returnA/GCD (A, b) *b; }//a*b = GCD*LCM + ConstLL INF =0x3f3f3f3f3f3f3f3f; - ConstLL mod = 1e9+7; the Const DoubleEPS = 1e-8; * Const intINF =0x3f3f3f3f; $ Const intMAXK = 1e5+5;Panax Notoginseng Const intMAXM =510*510; - Const intMAXN = the; the + intN, M, S, ans; A intPATH[MAXN]; the BOOLG[MAXN][MAXN]; +vector<int>VV[MAXN]; - $ voidInit () $ { -CIN >> n >> M >>s; - the intx, y; Ans =0; -Memset (G,0,sizeof(g));Wuyimemset (Path,0,sizeof(path)); theFoe (I,1, N) vv[i].clear (); -Foe (I,1, M) Wu { -scanf"%d%d", &x, &y); AboutG[x][y] = g[y][x] =true; $ vv[min (x, y)].push_back (Max (x. )); - } - } - A voidDfsintXintd) + { the if(d = = s) {ans++;return; } - //if (vv[x].size () +d < s) return; $ the for(inti =0; I < vv[x].size (); i++ ) the { the intv =Vv[x][i]; the BOOLFlag =false; - in for(inti = x; I i =Path[i]) the { the if(!g[v][i]) flag =true; About if(Vv[v].size () +1+d < s) flag =true; the if(flag) Break; the } the + if(!flag) - { thePATH[V] =x;BayiDFS (V, d+1); the } the } - } - the intMain () the { the the #ifndef Online_judge -Freopen ("Input.txt","R", stdin); the #endif the the intT CIN >>T;94 while(t--) the { the init (); theFoe (I,1, N)98 { AboutPath[i] =0; -DFS (I,1);101 }102printf"%d\n", ans);103 }104 the return 0;106}
View Code
hdu5952 Counting cliques Tips Dfs