Topic Link: Click to open the link
Test instructions: n beads, the two halves of each bead are made up of different colors. Only the same color can be joined together, asked whether to form a necklace.
Idea: If a bead is regarded as a non-forward edge connecting two vertices, then the subject becomes the existence of the Euler circuit for the non-direction graph. For the graph, if the number of degrees are even and the graph is connected, then there is a Euler loop. Then start at any point and walk all the way back to the beginning.
See the code for details:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include < cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue># Define MAX (a) > (b)? ( A):(B) #define MIN (a) < (b) ( A):(B)) using namespace Std;typedef long Long ll;const double PI = ACOs ( -1.0); const double EPS = 1e-6;const int mod = 10000 00000 + 7;const int INF = 1000000000;const int MAXN = 55;int T,n,m,kase=0,p[maxn],maxv,root,cnt[maxn],g[maxn][maxn];struc T node {int A, B;} a[1005];void dfs (int u) {for (int v = 1; v <= maxv; v++) if (G[u][v]) {g[u][v]--; g[v][u]--; DFS (v); printf ("%d%d\n", V, U); }}int _find (int x) {return p[x] = = x X:p[x] = _find (P[x]);} int main () {scanf ("%d", &t); while (t--) {scanf ("%d", &n); memset (CNT, 0, sizeof (CNT)); memset (g, 0, sizeof (g)); MAXV = 0; for (int i=1;i<=50;i++) p[i] = i; if (Kase) printf ("\ n"); for (int i=0;i<n;i++) {scanf ("%d%d", &a[i].a,&a[i].b); cnt[a[i].a]++; cnt[a[i].b]++; g[a[i].a][a[i].b]++; g[a[i].b][a[i].a]++; MAXV = Max (MAXV, Max (A[I].A, a[i].b)); int x = _find (A[I].A); int y = _find (a[i].b); if (x! = y) p[x] = y; } bool OK = true; for (int i=1;i<=maxv;i++) {if (cnt[i] = = 0) continue; if ((Cnt[i] & 1) | | (_find (i)! = _find (MAXV))) {OK = false; Break }} printf ("Case #%d\n", ++kase); if (OK) for (int i=1;i<=maxv;i++) {DFS (i); } else printf ("Some beads may be lost\n"); } return 0;}
UVA 10054-the Necklace (Euler circuit)