Now the blog update is less, as I am still alive
Rikka with Graph
Topic Transfer: Hdu-5422-rikka with Graph
AC Code:
#include <map>#include <set>#include <list>#include <cmath>#include <deque>#include <queue>#include <stack>#include <bitset>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <complex>#include <cstdlib>#include <cstring>#include <fstream>#include <sstream>#include <utility>#include <iostream>#include <algorithm>#include <functional>#define LL Long Long#define INF 0x7fffffffusing namespace STD;intN, M;intMain () { while(scanf("%d%d", &n, &m)! = EOF) {intU, v;intFlag =0; for(inti =0; I < m; i + +) {scanf("%d%d", &u, &v);if(U = =1&& v = = N) | | (U = = N && v = =1)) flag =1; }if(Flag = =1)printf("1%d\n", n * (N-1) /2);Else printf("1 1\n"); }return 0;}
Rikka with Tree
Topic Delivery: Hdu-5423-rikka with Tree
AC Code:
#include <map>#include <set>#include <list>#include <cmath>#include <deque>#include <queue>#include <stack>#include <bitset>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <complex>#include <cstdlib>#include <cstring>#include <fstream>#include <sstream>#include <utility>#include <iostream>#include <algorithm>#include <functional>#define LL Long Long#define INF 0x7fffffffusing namespace STD;Const intMAXN =1005;intN vector<int>G[MAXN];BOOLJudge1 () {if(g[1].size () <=0|| g[1].size () >=2) {return false; }intPre =1;intv = g[1][0]; while(g[v].size () = =2) { for(inti =0; I <2; i + +) {if(G[v][i]! = Pre) {pre = V; v = g[v][i];if(g[v].size () = =1)return true; Break; } } }return false;}BOOLJudge2 () {if(g[1].size ()! = N-1)return false;return true;}BOOLJudge3 () {if(g[1].size ()! =1) {return false; }intCNT =1;intPre =1;intv = g[1][0]; while(g[v].size () = =2) { for(inti =0; I <2; i + +) {if(G[v][i]! = Pre) {pre = V; v = g[v][i]; CNT + +; Break; } }if(G[v].size ()! =2) Break; }//cout << n << "" << cnt << Endl; if(g[v].size () = = n-cnt)return true;return false;}intMain () { while(scanf("%d", &n)! = EOF) { for(inti =0; i < MAXN; i + +) g[i].clear ();intU, v; for(inti =1; I < n; i + +) {scanf("%d%d", &u, &v); G[u].push_back (v); G[v].push_back (U); }if(Judge1 () | | judge2 () | | judge3 ()) {printf("yes\n"); }Else printf("no\n"); }return 0;}
Rikka with Graph II
Topic Transfer: Hdu-5424-rikka with Graph II
Official:
If the graph is connected, it can be found that if there is a Hamiltonian path, there must be one end of the Hamiltonian path with the lowest degree point, starting from that point the direct DFS lookup Hamiltonian path complexity is O (n). Be aware of the case where the diagram is disconnected first.
AC Code:
#include <map>#include <set>#include <list>#include <cmath>#include <deque>#include <queue>#include <stack>#include <bitset>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <complex>#include <cstdlib>#include <cstring>#include <fstream>#include <sstream>#include <utility>#include <iostream>#include <algorithm>#include <functional>#define LL Long Long#define INF 0x7fffffffusing namespace STD;Const intMAXN =1005;intN vector<int>G[MAXN]; map<pair<int, int>,int> MP;intDEG[MAXN];intVIS[MAXN];BOOLDfsintUintCNT) {Vis[u] =1;if(cnt = = N)return true;intD = g[u].size (); for(inti =0; I < D; i + +) {intv = g[u][i];if(!vis[v]) {if(Dfs (V, CNT +1))return true; VIS[V] =0; } }return false;}intCntvoidJudgeintu) {Vis[u] =1; CNT + +;intD = g[u].size (); for(inti =0; I < D; i + +) {intv = g[u][i];if(!vis[v]) {judge (v); } }}intMain () { while(scanf("%d", &n)! = EOF) { for(inti =0; I <= N; i + +) g[i].clear (); Mp.clear ();memset(DEG,0,sizeof(deg));intU, v; for(inti =0; I < n; i + +) {scanf("%d%d", &u, &v);if(U = = v)Continue;if(Mp.find (Make_pair (U, v))! = Mp.end () | | mp.find (Make_pair (V, u))! = Mp.end ()) {Continue; } g[u].push_back (v); G[v].push_back (U); Deg[u] + +; DEG[V] + +; Mp[make_pair (U, v)] =1; }intMi = INF;intId for(inti =1; I <= N; i + +) {if(Deg[i] < mi) {mi = deg[i]; id = i; } }memset(Vis,0,sizeof(VIS)); CNT =0; Judge1);//The case of non-connectivity, because if the direct Dfs is not connected due to backtracking too many times to time out if(cnt! = N) {printf("no\n");Continue; }memset(Vis,0,sizeof(VIS));//cout<< ID << endl; if(Dfs (ID,1)) {printf("yes\n"); }Else printf("no\n"); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Bestcoder Round #53