sgu101:Domino

来源:互联网 发布:我的世界js手机版视频 编辑:程序博客网 时间:2024/05/22 03:12

完结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
原创粉丝点击