zoukankan      html  css  js  c++  java
  • ACM: SGU 101 Domino- 欧拉回路-并查集

    sgu 101 - Domino
    Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u

    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 -

    //这个题目做了我一个晚上,有点头晕,还是要让自己保持冷静啊。。。



    //多米诺骨牌有两个面,每个面有一个数字,相同的数字只能推倒相同数字面的骨牌,骨牌可以转向,给出的数字是严格的正面反面,问是否能推到所有的骨牌。

    //如果可以输出骨牌的顺序和是否需要反转骨牌。如果不可以全部推倒,输出"No solution"

    //AC代码:
    
    
    #include"algorithm"
    #include"iostream"
    #include"cstring"
    #include"cstdlib"
    #include"string"
    #include"cstdio"
    #include"vector"
    #include"cmath"
    #include"queue"
    using namespace std;
    #define memset(x,y) memset(x,y,sizeof(x))
    #define memcpy(x,y) memcpy(x,y,sizeof(x))
    #define MX 401
    
    bool vis[MX];
    int Head[MX],cnt;
    int indegree[MX],p[MX],siz,bin;
    int fid[7];
    
    struct Edge {
    	int v,nxt;
    	int flag;
    } E[MX];
    
    int find(int x) {
    	return fid[x]==x?x:(fid[x]=find(fid[x]));
    }
    
    void union_root(int st,int ed) {
    	int rt1=find(st);
    	int rt2=find(ed);
    	if(rt1!=rt2)fid[rt2]=rt1;
    }
    
    void edge_init() {
    	cnt=0;
    	siz=0;
    	memset(p,0);
    	memset(E,0);
    	memset(vis,0);
    	memset(Head,-1);
    	memset(indegree,0);
    	for(int i=0; i<=6; i++) {
    		fid[i]=i;
    	}
    }
    
    void edge_add(int st,int ed,int flag) {
    	E[cnt].v=ed;
    	E[cnt].flag=flag;
    	E[cnt].nxt=Head[st];
    	Head[st]=cnt++;
    }
    
    bool check() {
    	bin=-1;
    	int tot=0;
    	for(int i=0; i<=6; i++) {
    		if(vis[i]) {
    			if(bin==-1)bin=i;
    			if(indegree[i]%2) {
    				tot++;
    				if(tot>2)return 0;//判断奇数有多少个,超过两个奇数就判断不是 
    				bin=i;
    			}
    		}
    	}
    	for(int i=0; i<=6; i++) {
    		if(bin!=-1)if(indegree[i]&&find(i)!=find(bin))return 0;  //如果不是联通块就判断不是 
    	}
    	if(bin==-1)return 0;
    	if(tot==1)return 0;	//如果奇数只有一个判断不是 
    	else return 1;
    }
    
    
    void Fleury(int u,int e) {
    	for(int i=Head[u]; ~i; i=E[i].nxt) {
    		int v=E[i].v;
    		if(vis[i|1])continue;
    		vis[i|1]=1;
    		Fleury(v,E[i].flag);
    	}
    	if(e)p[siz++]=e;	//标记是哪一条边。 
    }
    
    int main() {
    	int m,st,ed;
    	while(~scanf("%d",&m)) {
    		edge_init();
    		for(int i=1; i<=m; i++) {
    			scanf("%d%d",&st,&ed);
    			vis[st]=vis[ed]=1;
    			edge_add(st,ed,i);		//正向 
    			edge_add(ed,st,-i);		//记录多米诺骨牌是否需要转向  反向 
    			indegree[st]++;
    			indegree[ed]++;
    			union_root(st,ed);		//合成联通块 
    		}
    		bool flag=check();
    		if(!flag)puts("No solution");
    		else {
    			memset(vis,0);	//再次清空标记数组 
    			Fleury(bin,0);
    			for(int i=siz-1; i>=0; i--) {
    				printf("%d %c
    ",p[i]>0?p[i]:-p[i],p[i]>0?'+':'-');
    			}
    		}
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    Blazor 服务端组件 Render, RenderFragment ,RenderTreeBuilder, CascadingValue/CascadingParameter
    Git Submodule管理项目子模块
    使用Try.NET创建可交互.NET文档
    开包即食的教程带你浅尝最新开源的C# Web引擎 Blazor
    搞懂Xamarin.Forms布局,看这篇应该就够了吧
    【译】.NET 跨平台界面框架和为什么你首先要考虑再三
    Roslyn 语法树中的各种语法节点及每个节点的含义
    数组的子集
    查找字符串中的最长无重复字符的子串
    双通道无限蓝屏
  • 原文地址:https://www.cnblogs.com/HDMaxfun/p/5744927.html
Copyright © 2011-2022 走看看