Test instructions: Give an image without a direction to determine whether it is a Hamiltonian diagram.
Hamilton Road: A pathway that passes through each point and only once.
Method: Each time to find the minimum number of points as a starting point, and then dfs the entire graph, see can traverse to how many points can reach N.
1#include <iostream>2#include <cstdio>3#include <ctime>4#include <cmath>5#include <cstring>6#include <algorithm>7#include <queue>8#include <vector>9#include <climits>Ten#include <Set> One using namespacestd; A Const intmaxn=1100, inf=1E9; -vector<int>G[MAXN]; - intn,vis[maxn],dep,cnt; the BOOLDfsintu) - { - if(DEP = =N) - return true; + for(intI=0; I<g[u].size (); i++) - { + intv =G[u][i]; A if(Vis[v]) at Continue; -VIS[V] =true; -dep++; - if(Dfs (v)) - return true; -DEP-- ; inVIS[V] =0; - } to return false; + } - intMain () the { * intu,v; $ while(SCANF ("%d", &n)! =-1)Panax Notoginseng { - for(intI=1; i<=n; i++) the g[i].clear (); + for(intI=1; i<=n; i++) A { thescanf"%d%d",&u,&v); + G[u].push_back (v); - g[v].push_back (u); $ } $ intHead=0, cnt=0; - for(intI=1; i<=n; i++) - if(g[i].size () = =1) the { -Head=i;///find a starting pointWuyicnt++; the } - if(cnt>2)///If there are more than two points with a degree of 1 Wu { -Puts"NO"); About Continue; $ } -memset (Vis,0,sizeof(Vis)); - if(Head = =0) -Head=1;///no degree is 1, which is a ring Adep=1; + if(Dfs (head)) thePuts"YES"); - ElsePuts"NO"); $ } the}
View Code
Determine if it is a Hamiltonian figure--hdu 5424