Links: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3332
Test instructions
Give you an n, representing a race chart with n points, followed by n * (N-1)/2 lines two points u, V, representing the presence of a forward edge <u,v>, asking if a Hamiltonian pathway can be constructed.
Ideas:
The competition chart must contain Hamiltonian paths, which do not necessarily contain Hamiltonian loops. It is impossible to have a situation that does not exist, directly constructs can, as to the method can see my another article: http://www.cnblogs.com/Ash-ly/p/5452580.html.
Attention:
When constructing, it must be found in the order of the current sequence, rather than in the order of the points themselves, when looking for 1 or 0, and WA a few times here.
Code:
1#include <iostream>2#include <cmath>3#include <cstdio>4#include <cstring>5#include <cstdlib>6#include <algorithm>7#include <queue>8#include <stack>9#include <vector>Ten One using namespacestd; AtypedefLong LongLL; - Const intMAXN = $; - the //the arv[] length is len, insert key befor arv[index] -InlinevoidInsert (intArv[],int&len,intIndexintkey) { - if(Index > Len) index =Len; -len++; + for(inti = len-1; I >=0; --i) { - if(I! = index && i) arv[i] = arv[i-1]; + Else{Arv[i] = key;return;} A } at } - - voidHamilton (intANS[MAXN +7],intMAP[MAXN +7][MAXN +7],intN) { - intANSI =1; -ans[ansi++] =1; - for(inti =2; I <= N; i++){ in if(Map[i][ans[ansi-1]] ==1) -ans[ansi++] =i; to Else{ + intFlag =0; - for(intj = ANSI-2; J >0; --j) {//Find 0/1 in the current sequence the if(Map[i][ans[j]] = =1){ *Flag =1; $Insert (ans, ANSI, j +1, i);Panax Notoginseng Break; - } the } + if(!flag) Insert (ans, ANSI,1, i); A } the } + } - $ intMain () $ { - //freopen ("Input.txt", "R", stdin); - intT; thescanf"%d", &t); - while(t--){Wuyi intN; thescanf"%d", &N); - intM = n * (n-1) /2; Wu intMAP[MAXN +7][MAXN +7] = {0}; - for(inti =0; i < M; i++){ About intu, v; $scanf"%d%d", &u, &v); - if(U < v) Map[v][u] =1; //Jiantu, map[v][u] = 1, representing the presence of the edge <u, V>, and U < v. - } - intANS[MAXN +7] = {0}; A Hamilton (ans, map, N); + for(inti =1; I <= N; i++) theprintf (i = =1?"%d":"%d", Ans[i]); -printf"\ n"); $ } the return 0; the}
ZOJ 3332 Strange Country II (race chart construction Hamilton pathway)