zoukankan      html  css  js  c++  java
  • UVA 10129 Play on Words

    欧拉回路

    以字母为结点,单词为边;注意两个相同的单词表示两条边。

    并查集判断是否连通,出度,入度判断是否是欧拉回路

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 
     6 int map[100010];
     7 int g[30][30];
     8 int num[26][2];
     9 int v[30],f[30];
    10 
    11 int father (int i){
    12     f[i]=f[i]==i?f[i]:father (f[i]);
    13     return f[i];
    14 }
    15 
    16 int main (){
    17     int t,n;
    18     scanf ("%d",&t);
    19     while (t--){
    20         cin>>n;
    21         char s[2000];
    22         memset (v,0,sizeof v);
    23         memset (g,0,sizeof g);
    24         for (int i=0;i<n;i++){
    25             scanf ("%s",s);
    26             int len=strlen (s);
    27             g[s[0]-'a'][s[len-1]-'a']++;
    28             v[s[0]-'a']=v[s[len-1]-'a']=1;
    29         }
    30         int flag=1;
    31         for (int i=0;i<30;i++)  f[i]=i;
    32         for (int i=0;i<26;i++)
    33             for (int j=0;j<26;j++)
    34                 if (g[i][j]||g[j][i])
    35                     f[father(i)]=father(j);
    36         for (int i=0;i<26;i++){
    37             for (int j=0;j<26;j++){//cout<<i<<j<<endl;
    38                 if (v[i]&&v[j]){
    39                     if (father(i)!=father(j)){
    40                             flag=0;break ;
    41                     }
    42                 }
    43             }
    44             if (!flag)
    45                 break ;
    46         }
    47         if (!flag){
    48             printf ("The door cannot be opened.
    ");
    49             continue ;
    50         }
    51         int c,r;//cout<<"error"<<endl;
    52         c=1;r=1;
    53         for (int i=0;i<26;i++){
    54             int temp=0;
    55             for (int j=0;j<26;j++){
    56                 temp+=g[i][j]-g[j][i];
    57             }
    58             if (temp==1&&c)
    59                 c=0;
    60             else if (temp==-1&&r)
    61                 r=0;
    62             else if (temp==0) ;
    63             else {
    64                 flag=0;
    65                 break ;
    66             }
    67         }
    68         if (flag)
    69             printf ("Ordering is possible.
    ");
    70         else printf ("The door cannot be opened.
    ");
    71     }
    72     return 0;
    73 }
  • 相关阅读:
    Longest Palindromic Substring问题
    twosum问题
    longest substring问题
    特殊的ARP
    【转】人肉搜索技巧
    ARP攻击
    Linux kali安装及常用命令收集
    【转】ICMP协议
    SpringBoot集成Mybatis-XML方式通用Mapper
    springMVC的controller中insert()多次,记优惠券被多次领取
  • 原文地址:https://www.cnblogs.com/gfc-g/p/3859006.html
Copyright © 2011-2022 走看看