zoukankan      html  css  js  c++  java
  • UVA10054-The Necklace(无向图欧拉回路——套圈算法)

    Problem UVA10054-The Necklace

    Time Limit: 3000 mSec

    Problem Description

    Input

    The input contains T test cases. The first line of the input contains the integer T. The first line of each test case contains an integer N (5 ≤ N ≤ 1000) giving the number of beads my sister was able to collect. Each of the next N lines contains two integers describing the colors of a bead. Colors are represented by integers ranging from 1 to 50.

    Output

    For each test case in the input first output the test case number as shown in the sample output. Then if you apprehend that some beads may be lost just print the sentence “some beads may be lost” on a line by itself. Otherwise, print N lines with a single bead description on each line. Each bead description consists of two integers giving the colors of its two ends. For 1 ≤ i ≤ N1, the second integer on line i must be the same as the first integer on line i + 1. Additionally, the second integer on line N must be equal to the first integer on line 1. Since there are many solutions, any one of them is acceptable. Print a blank line between two successive test cases.
     

    Sample Input

    2 5 1 2 2 3 3 4 4 5 5 6 5 2 1 2 2 3 4 3 1 2 4

    Sample Output

    Case #1
    some beads may be lost
     
    Case #2 
    2 1
    1 3
    3 4
    4 2
    2 2
     
    题解:比较经典的欧拉回路的题目,将颜色看作节点,珠子相当于连接两个颜色的边,由于珠子可以翻转,因此是无向图。
       无向图欧拉回路存在的充要条件:连通并且所有点的度数均为偶数。
       此题数据直接保证了连通,检查连通性很容易,并查集即可。为了输出一条欧拉回路,采用套圈算法,从任意一个节点出发dfs,走不下去了就回溯,回溯过程                    中逆向输出节点即可。
     
     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 #define REP(i, n) for (int i = 1; i <= (n); i++)
     6 #define sqr(x) ((x) * (x))
     7 
     8 const int maxn = 50 + 10;
     9 const int maxm = 30 + 10;
    10 const int maxs = 10000 + 10;
    11 
    12 typedef long long LL;
    13 typedef pair<int, int> pii;
    14 typedef pair<double, double> pdd;
    15 
    16 const LL unit = 1LL;
    17 const int INF = 0x3f3f3f3f;
    18 const LL mod = 1000000007;
    19 const double eps = 1e-14;
    20 const double inf = 1e15;
    21 const double pi = acos(-1.0);
    22 
    23 int n, iCase;
    24 int deg[maxn], gra[maxn][maxn];
    25 
    26 void dfs(int u)
    27 {
    28     for (int i = 1; i <= 50; i++)
    29     {
    30         if (gra[u][i])
    31         {
    32             gra[u][i]--;
    33             gra[i][u]--;
    34             dfs(i);
    35             cout << i << " " << u << endl;
    36         }
    37     }
    38 }
    39 
    40 int main()
    41 {
    42     ios::sync_with_stdio(false);
    43     cin.tie(0);
    44     freopen("input.txt", "r", stdin);
    45     //freopen("output.txt", "w", stdout);
    46     int T;
    47     cin >> T;
    48     while (T--)
    49     {
    50         cin >> n;
    51         cout << "Case #" << ++iCase << endl;
    52         memset(deg, 0, sizeof(deg));
    53         memset(gra, 0, sizeof(gra));
    54         int u, v;
    55         for (int i = 0; i < n; i++)
    56         {
    57             cin >> u >> v;
    58             gra[u][v]++, gra[v][u]++;
    59             deg[u]++, deg[v]++;
    60         }
    61         bool ok = true;
    62         for (int i = 1; i <= 50; i++)
    63         {
    64             if (deg[i] % 2)
    65             {
    66                 ok = false;
    67                 break;
    68             }
    69         }
    70         if (!ok)
    71         {
    72             cout << "some beads may be lost" << endl;
    73         }
    74         else
    75         {
    76             dfs(v);
    77         }
    78         if (T)
    79             cout << endl;
    80     }
    81     return 0;
    82 }
  • 相关阅读:
    hdu 1016 Prime Ring Problem (dfs)
    微信小程序尝鲜一个月现状分析
    创新大师Steve Blank: 你真的知道什么是真正的精益创业吗?
    android studio 开发经常使用快捷键使用分享
    LeetCode:Partition List
    2016 博客导读总结 &amp; 个人感悟
    poj
    Android开之在非UI线程中更新UI
    浅谈数据库生命周期
    从AdventureWorks学习数据库建模——保留历史数据
  • 原文地址:https://www.cnblogs.com/npugen/p/10746240.html
Copyright © 2011-2022 走看看