uva 10054 - The Necklace

简介: 点击打开链接uva 10054 思路: 欧拉回路 分析: 1 对于一个无向图来说如果这个图是一个欧拉图,那么必须满足该图是连通的并且每个点的度数都是偶数 2 题目给定n条边的无向图问我们是否是一个欧拉图,是的话输出欧拉图的一条路径 3 ...

点击打开链接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;
}




目录
相关文章
|
9月前
Uva10001 Garden of Eden
Uva10001 Garden of Eden
32 0
|
9月前
uva375 Inscribed Circles and Isosceles Triangles
uva375 Inscribed Circles and Isosceles Triangles
26 0
|
9月前
uva10152 ShellSort
uva10152 ShellSort
40 0
|
算法
UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494...
1537 0
|
机器学习/深度学习
uva 11538 Chess Queen
点击打开链接 题意:给定一个n*m的矩阵,问有多少种方法放置两个相互攻击的皇后?规定在同一行同一列和同对角线的能够相互攻击 思路: 1 先考虑同一行的情况,n行就有n种情况,每一行有m*(m-1)种,总的是n*m*(m-1); 2 考虑同...
796 0
uva 1203 Argus
点击打开链接uva 1203 思路: 优先队列 分析: 1 题目要求前k个事件的编号,我们利用优先队列来维护即可 2 优先队列保存的是一个struct,因此我们需要重载 s.
1279 0
uva 1388 - Graveyard
点击打开链接uva1388 思路:数学 分析: 1 我们把原先的n个墓碑看成是园内的正n变形,现在的n+m个墓碑看成是园内的正n+m变形。那么通过画图我们可以知道当这个两个正多边形有一个点重合的时候移动的总距离最小 2 那么我们把这个圆进...
982 0
|
C++
uva10905
题意:给定n个数字字符串,求他们能组成的最大数字字符串。 做法:用,cmp比较函数比较s+t与t+s即可。 代码: #include #include #include #include #include #define zz using namespace st...
751 0

热门文章

最新文章