sgu101:Domino

完结usaco之后,终于踏上了sgu的征程,High aim,high demand !

第一题(忽略A+B)是一道图论题

以多米诺骨牌为边,数字抽象为点(0到6)
如果奇点个数为0个,求解欧拉回路
如果奇点个数为2个,求解欧拉路径
非以上情况No Solution
如果图不是连通的那么也是No Solution

不是难题,题解也不会很详细,大牛勿喷...

#include <stdio.h>
#include <string.h>

int N;
int Edge[105][2] = {0}; 
int map[105][105] = {0};
int degree[105] = {0};
int E[105][105][55] = {0};
int path[105][2] = {0}, top = 0;

void dfs(int cur)
{
  int i;	
  for(i = 0; i <= 6; ++i)
    if(map[cur][i])
    {
      map[cur][i]--;
	  map[i][cur]--;
	  dfs(i);
	  path[N - top][0] = cur;
	  path[N - top++][1] = i;	
    }
}

void print()
{
  int i, x, y;
  for(i = 1; i <= top; ++i)
  {
    x = path[i][0];
    y = path[i][1];
	if(E[x][y][0])
	  printf("%d +\n", E[x][y][E[x][y][0]--]);	
	else if(E[y][x][0])
	  printf("%d -\n", E[y][x][E[y][x][0]--]);
  }
} 

int main()
{
  int i, j;
  int x, y; 
  int odd = 0;
  scanf("%d", &N);
  for(i = 1; i <= N; ++i)
  {
    scanf("%d%d", &x, &y);
    Edge[i][0] = x, Edge[i][1] = y;
    degree[x]++, degree[y]++;
    map[x][y]++, map[y][x]++;
    E[x][y][++E[x][y][0]] = i;
  }
  
  for(i = 0; i <= 6; ++i)
    if(degree[i] & 1) odd++;
  
  if(odd == 0)
  {
    for(i = 0; i <= 6; ++i) 
	  if(degree[i]) break;
	dfs(i);	
	if(top < N) puts("No solution");
	else print();
  }
  else if(odd == 2)
  {
    for(i = 0; i <= 6; ++i)
	  if(degree[i] & 1) break;
	dfs(i);	
	if(top < N) puts("No solution");
	else print();
  }
  else puts("No solution");
  return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值