• poj1386


    欧拉通路

    View Code
    #include <iostream>
    #include
    <cstdlib>
    #include
    <cstring>
    #include
    <cstdio>
    using namespace std;

    int n;
    char st[1005];
    bool exist[30];
    int in[30], out[30];
    int father[30];

    int getanc(int a)
    {
    if (father[a] == a)
    return a;
    return father[a] = getanc(father[a]);
    }

    void merge(int a, int b)
    {
    father[getanc(a)]
    = getanc(b);
    }

    void input()
    {
    scanf(
    "%d", &n);
    memset(
    in, 0, sizeof(in));
    memset(
    out, 0, sizeof(out));
    memset(exist,
    0, sizeof(exist));
    for (int i = 0; i < 26; i++)
    father[i]
    = i;
    for (int i = 0 ; i < n; i++)
    {
    scanf(
    "%s", st);
    in[st[0] - 'a']++;
    out[st[strlen(st) - 1] - 'a']++;
    merge(st[
    0] - 'a', st[strlen(st) - 1] - 'a');
    exist[st[
    0] - 'a'] = exist[st[strlen(st) - 1] - 'a'] = true;
    }
    }

    bool ok()
    {
    int start =0;
    int end = 0;
    int cnt = 0;
    for (int i = 0; i < 26; i++)
    if (father[i] == i && exist[i])
    cnt
    ++;
    if (cnt > 1)
    return false;
    for (int i = 0; i < 26; i++)
    if (in[i] - out[i] == 1)
    start
    ++;
    else if (out[i] - in[i] == 1)
    end
    ++;
    else if (out[i] == in[i])
    continue;
    else
    return false;
    return start <= 1 && end <= 1;
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    int t;
    scanf(
    "%d", &t);
    while (t--)
    {
    input();
    if (ok())
    printf(
    "Ordering is possible.\n");
    else
    printf(
    "The door cannot be opened.\n");
    }
    return 0;
    }

      

  • 相关阅读:
    C语言I博客作业04
    C语言I博客作业03
    C语言I—2019秋作业02
    C语言I博客作业01
    JSP 基础语法
    JavaScript
    Hadoop简介
    用流从一个指定的网址抓取html代码
    内容采集
    SpringMVC控制器与视图的数据交换
  • 原文地址:https://www.cnblogs.com/rainydays/p/2160794.html
Copyright © 2020-2023  润新知