uva 10054 - The Necklace

点击打开链接uva 10054

思路: 欧拉回路
分析:
1 对于一个无向图来说如果这个图是一个欧拉图,那么必须满足该图是连通的并且每个点的度数都是偶数
2 题目给定n条边的无向图问我们是否是一个欧拉图,是的话输出欧拉图的一条路径
3 首先我们先判断是否所有点的度数都是偶数,然后我们去判断当前图是否是只有一个连通分支,那么这个利用并查集即可
4 如果都满足的话直接去搜索并且输出路径即可

代码:

#include<stack>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 55;
int father[MAXN] , degree[MAXN];
int n , mat[MAXN][MAXN];

void init(){
    memset(mat , 0 , sizeof(mat));
    memset(degree , 0 , sizeof(degree));
    for(int i = 0 ; i < MAXN ; i++)
        father[i] = i;
}

int find(int x){
    if(x != father[x])
       father[x] = find(father[x]);
    return father[x];
}

bool isOk(){
    int root = -1;
    for(int i = 0 ; i < MAXN ; i++){
       if(degree[i]&1)
          return false;
       if(degree[i]){
          if(root == -1)
             root = find(i);
          else{
             if(root != find(i))
                return false;
          }
       }
    }
    return true;
}

void dfs(int cur){
    for(int i = 1 ; i < MAXN ; i++){
       if(mat[cur][i]){
          mat[cur][i]--;
          mat[i][cur]--;
          dfs(i);
          printf("%d %d\n" , i , cur);
       }
    }
}

int main(){
    int Case = 1 , T , x , y , start;
    bool isFirst = true;
    scanf("%d" , &T);
    while(T--){
         scanf("%d" , &n);
         init();
         for(int i = 0 ; i < n ; i++){
            scanf("%d%d", &x , &y);
            start = x;
            mat[x][y]++;
            mat[y][x]++;
            degree[x]++;
            degree[y]++;
            father[find(x)] = find(y);
         }
         if(isFirst)
            isFirst = false;
         else
            printf("\n");
         printf("Case #%d\n" , Case++);
         if(!isOk())
             printf("some beads may be lost\n");
         else
             dfs(start);
    }
    return 0;
}
时间: 2024-11-05 14:53:30

uva 10054 - The Necklace的相关文章

UVa 10054:The Necklace, 欧拉回路+打印路径

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=995 题目类型: 欧拉回路 题目: My little sister had a beautiful necklace made of colorful beads. Two successive beads in the necklace sha

UVa 10596:Morning Walk, 赤裸裸的欧拉回路

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=1537 题目类型: 欧拉回路, 并查集, dfs 题目: Kamal is a Motashota guy. He has got a new job in Chittagong. So, he has moved to Chittagong fr

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:UVa 11461 Square Numbers (简单数学)

11461 - Square Numbers Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=24 56 A square number is an integer number whose square root is also an integer.

UVa 10183 How Many Fibs? (统计斐波那契数个数&amp;amp;高精度)

10183 - How Many Fibs? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1124 Recall the definition of the Fibonacci numbers:    f1 := 1    f2 := 2    fn :

UVa 701 The Archeologists&#039; Dilemma: 数学及枚举

701 - The Archeologists' Dilemma Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=642 An archeologist seeking proof of the presence of extraterrestrials i