• Luogu 1341 无序字母对


    Solution

    找一条字典序最小的欧拉路径。

    用 $multiset$ 存储领接表。

    欧拉路径模板传送门


    Code

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<set>
     5 using namespace std;
     6 
     7 const int N = 1e3 + 5;
     8 
     9 int n, up = 'z' - 'A' + 1;
    10 int ans[N], tot, d[N], num;
    11 
    12 multiset<int> to[N];
    13 
    14 void dfs(int u) {
    15     multiset<int> :: iterator i;
    16     for (i = to[u].begin(); i != to[u].end(); i = to[u].begin()) {
    17         int nt = *i;
    18         to[nt].erase(to[nt].find(u));
    19         to[u].erase(to[u].find(nt));
    20         dfs(nt);
    21     }
    22     ans[++tot] = u;
    23 }
    24 
    25 int main()
    26 {
    27     scanf("%d", &n);
    28     for (int i = 1; i <= n; ++i) {
    29         char s[3];
    30         scanf("%s", s);
    31         to[s[0] - 'A' + 1].insert(s[1] - 'A' + 1);
    32         to[s[1] - 'A' + 1].insert(s[0] - 'A' + 1);
    33         d[s[1] - 'A' + 1]++;
    34         d[s[0] - 'A' + 1]++;
    35     }
    36     for (int i = 1; i <= up; ++i)
    37         if (d[i] & 1)
    38             num++;
    39     if (num == 1 || num > 2)
    40         return puts("No Solution"), 0;
    41     if (num == 0) {
    42         for (int i = 1; i <= up; ++i)
    43             if (d[i]) {dfs(i); break;}
    44     }
    45     else 
    46         for (int i = 1; i <= up; ++i)
    47             if (d[i] && (d[i] & 1)) {dfs(i); break;}
    48     for (int i = tot; i; i--) 
    49         putchar(ans[i] + 'A' - 1);
    50     puts("");
    51 }
    View Code
  • 相关阅读:
    高斯消元
    Luogu P2068 统计和
    Luogu P1892 [BOI2003]团伙
    Luogu P2866 [USACO06NOV]糟糕的一天Bad Hair Day
    Luogu P3916 图的遍历
    Luogu P1041 [2003NOIP提高组]传染病控制
    Luogu P3901 数列找不同
    Luogu 2951 捉迷藏Hide and Seek
    Luogu P1550 打井Watering Hole
    洛谷——P1044 栈
  • 原文地址:https://www.cnblogs.com/cychester/p/9729075.html
Copyright © 2020-2023  润新知