zoukankan      html  css  js  c++  java
  • UVA 10054 The Necklace

    完全就是哭瞎的节奏···QAQ

    又是图论···

    题意:有一种项链,每个珠子上有两种颜色,相同颜色的两颗珠子的两头相连,如果能连成环输出珠子的顺序,不能连成环输出"some beads may be lost"。


    解法:DFS。将颜色看做点,珠子看做边,转化为欧拉回路问题。欧拉回路每个点的入度和出度和都是偶数。对颜色做DFS。加上回溯会T,不加度数判断会WA……

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<string.h>
    #include<math.h>
    #include<map>
    #include<queue>
    #include<set>
    #include<stack>
    #include<vector>
    #define ll long long
    using namespace std;
    int color[55][55];//记录不同颜色珠子的个数
    int n;
    struct node
    {
        int a,b;
        node(int a,int b): a(a),b(b) {}
    };//珠子
    vector<node> v;//项链的序列
    void dfs(int c)
    {
        for(int i=1; i<55; i++)
        {
            if(color[c][i])
            {
                color[c][i]--;
                color[i][c]--;
                dfs(i);
                v.push_back(node(c,i));
            }
        }
        return ;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        for(int cnt=1; cnt<=t; cnt++)
        {
            v.clear();
            int a,b;
            memset(color,0,sizeof(color));
            int d[55]= {0};//度数
            scanf("%d",&n);
            for(int i=0; i<n; i++)
            {
                scanf("%d%d",&a,&b);
                color[a][b]++;
                color[b][a]++;//一个珠子可以表示两种珠子
                d[a]++;
                d[b]++;
            }
            int flag=1;
            for(int i=0; i<55; i++)
            {
                if(d[i]&1)
                {
                    flag=0;
                    break;
                }
            }//度数判断
            printf("Case #%d
    ",cnt);
            if(flag)
            {
                dfs(a);
                if(!(n==v.size()&&v[0].b==v[v.size()-1].a))//判断首尾是否可以相连
                    flag=0;
            }
            if(flag)
                for(int i=v.size()-1; i>=0; i--)
                    printf("%d %d
    ",v[i].a,v[i].b);
            else
                puts("some beads may be lost");
            if(cnt<t)
                puts("");
        }
        return 0;
    }
    


  • 相关阅读:
    make dep
    TS流的解析
    qt ISO C++ forbids declaration of 'XXXX' with no type
    qt 中文乱码
    qt creator 快捷键 (一)
    qt 坐标变换
    Qt常用类及类方法简介之 QAction类
    QWidget背景(透明)问题
    如何在 QWidget 窗口上弹出右键菜单
    qt 事件
  • 原文地址:https://www.cnblogs.com/Apro/p/4303990.html
Copyright © 2011-2022 走看看