zoukankan      html  css  js  c++  java
  • 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;
    }

      

  • 相关阅读:
    33 函数参数的传递方式
    33 函数递归调用
    32 头文件
    31 函数
    30 枚举
    centos6.5升级默认的Mysql到5.5方法
    Centos6.5命令行快捷键
    redhat 安装lamp
    CentOS6.5中的vsftpd安装配置
    添加一个用户并且让用户获得root权限
  • 原文地址:https://www.cnblogs.com/rainydays/p/2160794.html
Copyright © 2011-2022 走看看