码迷,mamicode.com
首页 > 其他好文 > 详细

Uva10054 The Necklace

时间:2016-02-20 19:00:35      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:

Uva10054 欧拉回路

题意:给n个珠子,每个珠子的两端的颜色不同。问能否组成一条项链,两颗相邻的珠子相邻的两端颜色相同。

思路:把颜色看做点。珠子看做边。每一个珠子在两种颜色之间连边。无向图中任意一点的度数都为偶数,则该图具有欧拉回路。然后通过euler函数来输出即可。

技术分享
/*
ID: onlyazh1
LANG: C++
TASK: The Necklace
*/
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
#include<cctype>
#include<vector>
#include<cstring>
#include<fstream>
#include<iomanip>
#include<algorithm>
using namespace std;
typedef unsigned long long ll;
int deg[70];
int G[70][70];

void euler(int u){
    for(int i=1;i<=50;i++)
        if(G[u][i]){
            G[u][i]--;G[i][u]--;
            euler(i);
            cout<<i<<" "<<u<<endl;
        }
}

int main(){
    //ifstream cin("in.txt");
    int T,n,u,v,icase=0;
    cin>>T;
    while(T--){
        memset(deg,0,sizeof(deg));
        memset(G,0,sizeof(G));
        cin>>n;
        while(n--){
            cin>>u>>v;
            G[u][v]++;G[v][u]++;
            deg[u]++;deg[v]++;
        }

        if(icase) cout<<endl;
        cout<<"Case #"<<++icase<<endl;
        bool flag=true;
        for(int i=1;i<=50;i++)
            if(deg[i]%2==1){
                cout<<"some beads may be lost"<<endl;
                flag=false;break;
            }
        if(flag)
            euler(u);
    }
    return 0;
}
View Code

 

Uva10054 The Necklace

标签:

原文地址:http://www.cnblogs.com/onlyAzha/p/5203623.html

(1)
(3)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!