【ICPC-72】hdu 1116 Play on Words

点击打开链接hdu1116

思路: 欧拉回路
分析:
1 题目给定n个字符串,判断是否可以按照题目的要求可以组成一个长串
2 很明显题目是要求有向图是否有欧拉回路或者是欧拉道路
3 首先我们先判断有向图是否是单连通,那么这里面我们可以用并查集去求单连通性
4 如果是单连通的话,我们知道对于有向图来说如果存在欧拉道路的话那么最多有两个点的入度不等于长度并且入度和出度的绝对值为1,其它所有点的入度等于出度
5 注意n=1的时候都认为是可以满足条件

代码:


#include<set>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int N = 30;
int n , inDegree[N] , outDegree[N];
int mat[N][N] , father[N];
bool vis[N];

void init(){
    memset(vis , false , sizeof(vis));
    memset(inDegree , 0 , sizeof(inDegree));
    memset(outDegree , 0 , sizeof(outDegree));
    memset(mat , 0 , sizeof(mat));
    for(int i = 0 ; i < N ; i++)
        father[i] = i;
}

int find(int x){
    if(father[x] != x)
       father[x] = find(father[x]);
    return father[x];
}

bool isOk(){
    set<int>s;
    for(int i = 0 ; i < N ; i++)
       if(vis[i])
           s.insert(find(i));
    if(s.size() > 1)//如果有多个连通分支即不满足
       return false;
    int cnt = 0;
    for(int i = 0 ; i < N ; i++){
       if(abs(inDegree[i]-outDegree[i]) > 1) //如果绝对值大于1则不满足
          return false;
       if(abs(inDegree[i]-outDegree[i]) == 1) 
          cnt++;
    }
    return cnt <= 2;//最多2个点的出度和入度绝对值为1
}

int main(){
    int Case , x , y;
    char str[1010];
    scanf("%d" , &Case);
    while(Case--){
         scanf("%d" , &n); 
         init();
         for(int i = 0 ; i < n ; i++){
             scanf("%s" , str);  
             x = str[0]-'a';
             y = str[strlen(str)-1]-'a';
             vis[x] = vis[y] = true;
             inDegree[y]++;
             outDegree[x]++;
             mat[x][y]++;
             father[find(x)] = find(y);
         }
         if(n == 1 || isOk()) 
             puts("Ordering is possible.");
         else
             puts("The door cannot be opened.");
    }
    return 0;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值