欧拉道路

    xiaoxiao2026-02-28  8

    判断的条件不必多说了。

    关键点在于在dfs输出欧拉道路的时候要倒着输出。

    sgu 101

    Description

    Dominoes – game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or pips, on its face. The blocks usually are called bones, dominoes, or pieces and sometimes men, stones, or even cards. The face of each piece is divided, by a line or ridge, into two squares, each of which is marked as would be a pair of dice...

    The principle in nearly all modern dominoes games is to match one end of a piece to another that is identically or reciprocally numbered.

    ENCYCLOPÆDIA BRITANNICA

    Given a set of domino pieces where each side is marked with two digits from 0 to 6. Your task is to arrange pieces in a line such way, that they touch through equal marked sides. It is possible to rotate pieces changing left and right side.

    Input

    The first line of the input contains a single integer N (1 ≤ N ≤ 100) representing the total number of pieces in the domino set. The following N lines describe pieces. Each piece is represented on a separate line in a form of two digits from 0 to 6 separated by a space.

    Output

    Write “No solution” if it is impossible to arrange them described way. If it is possible, write any of way. Pieces must be written in left-to-right order. Every of N lines must contains number of current domino piece and sign “+” or “-“ (first means that you not rotate that piece, and second if you rotate it).

    Sample Input

    5 1 2 2 4 2 4 6 4 2 1

    Sample Output

    2 - 5 + 1 + 3 + 4 -

    典型的欧拉路径判断。

    #include<algorithm> #include<iostream> #include<cstdio> #include<cstring> #include<climits> #include<cstdlib> #include<cmath> #include<queue> using namespace std; typedef long long LL; const int MAXN=110; int a[MAXN],b[MAXN],fa[MAXN]; int num[10],ex[10]; int n; int l=0; int findset(int x) { if(x==fa[x]) return x; else return (fa[x]=findset(fa[x])); } int vis[MAXN],ansOut[MAXN],flagOut[MAXN]; void dfs(int x) { for(int i=1;i<=n;i++) if(!vis[i]) { if(a[i]==x) { vis[i]=1; dfs(b[i]); ansOut[l]=i; flagOut[l]=0; l++; } else if(b[i]==x) { vis[i]=1; dfs(a[i]); ansOut[l]=i; flagOut[l]=1; l++; } } } int main(void) { #ifdef LOCAL freopen("in.txt","rb",stdin); //freopen("out.txt","wb",stdout); #endif scanf("%d",&n); memset(num,0,sizeof(num)); memset(ex,0,sizeof(ex)); for(int i=0;i<=6;i++) fa[i]=i; for(int i=1;i<=n;i++) { scanf("%d%d",&a[i],&b[i]); int aa=a[i],bb=b[i]; num[aa]++; num[bb]++; if(findset(aa)!=findset(bb)) fa[findset(aa)]=findset(bb); ex[aa]=ex[bb]=1; } int sum=0; for(int i=0;i<=6;i++) if(ex[i] && fa[i]==i) sum++; if(sum!=1) {printf("No solution\n"); exit(0);} int tot=0; for(int i=0;i<=6;i++) if(num[i]%2) tot++; if(tot!=0 && tot!=2) {printf("No solution\n"); exit(0);} if(tot==0) dfs(a[1]); else { int st=0; for(int i=0;i<=6;i++) if(num[i]%2) {st=i;break;} l=0; memset(vis,0,sizeof(vis)); dfs(st); } for(int i=0;i<l;i++) { if(flagOut[i]) printf("%d +\n",ansOut[i]); else printf("%d -\n",ansOut[i]); } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-1307456.html
    最新回复(0)