hdu 1116 Play on Words

http://acm.hdu.edu.cn/showproblem.php?pid=1116

这个题目要运用到欧拉路得相关知识,并且也要并查集,题目说的是:给你n个单词,要你判断这些单词能不能首尾相连。

理解题目意思后,进行转化,输入字符串,提取首位字母作为下标来表示两节点的出现,以及相对应节点入度和出度的增加,
转化为并查集的应用即可。那么从可以想象一幅由首位字母节点构成的图,当且仅当图是一条欧拉回路或者欧拉通路的时候,
才能满足题目的要求,至于欧拉回路和欧拉通路的判定可以总结为如下:
1)所有的点联通
2)欧拉回路中所有点的入度和出度一样。

3)欧拉通路中起点的入度 - 出度 = 1,终点的 初度 - 入度 = 1, 其他的所有点入度 = 出度;

/*
2011-9-13
author:BearFly1990
*/
package acm.hdu.tests;

import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.Scanner;

public class HDU_1116 {
    private static int N = 40;
    private static int[] father = new int[N];
    private static boolean[] visited = new boolean[N];
    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedInputStream(System.in));
        int[] sin = new int[N];
        int[] out = new int[N]; 
        int[] p = new int[N];
        int T;
        T = in.nextInt();
        while(T-- > 0){
            int n = in.nextInt();
            Arrays.fill(sin, 0);
            Arrays.fill(out, 0);
            Arrays.fill(p, 0);
            Arrays.fill(visited, false);
            for(int i=0; i<26; i++)  
               father[i]=i; 
            while(n-- > 0){
                String str = in.next();
                int a = str.charAt(0) - 'a';
                int b = str.charAt(str.length() - 1) - 'a';
                merge(a,b);
                out[a]++;
                sin[b]++;
                visited[a] = true;
                visited[b] = true;
            }
            
            for(int i = 0; i < 26; i++){
                father[i] = find(i);
            }
            
            int count = 0;
            for(int i = 0; i < 26; i++){
                if(visited[i] && father[i] == i){
                    count ++;
                }
            }
            
           
            if(count > 1){
                System.out.printf("The door cannot be opened.\r\n");  
                continue;
            }
            int j  = 0 ;
            for(int i = 0; i < 26 ; i++){
                if(visited[i] && sin[i] != out[i]){
                    p[j++] = i;
                }
            }
            if(j == 0){
                System.out.printf("Ordering is possible.\r\n");   
                continue;
            }
            if(j==2 && ( out[p[0]]-sin[p[0]]==1 && sin[p[1]]-out[p[1]]==1
                    || out[p[1]]-sin[p[1]]==1 && sin[p[0]]-out[p[0]]==1 ) ) {
               System.out.printf("Ordering is possible.\r\n");  
               continue;
            }
            
            System.out.printf("The door cannot be opened.\r\n");  
            
        }
    }
    public static int find(int x){
        if(father[x] != x){
            father[x] = find(father[x]);
        }
        return father[x];
    }
    public static void merge(int a,int b){
        int x,y;
        x = find(a);
        y = find(b);
        if(x != y)father[x] = y;
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值