UVA 10129 (图论 欧拉回路 13.07.11)

Play on Words

Some of the secret doorscontain a very interesting word puzzle. The team of archaeologists has tosolve it to open that doors. Because there is no other way to open the doors,the puzzle is very important for us.

There is a largenumber of magnetic plates on every door. Every plate has one word written onit. The plates must be arranged into a sequence in such a way that every wordbegins with the same letter as the previous word ends. For example, the word``acm'' can be followed by the word ``motorola''. Your task is towrite a computer program that will read the list of words and determine whetherit is possible to arrange all of the plates in a sequence (according tothe given rule) and consequently to open the door.

Input Specification

The inputconsists of T test cases. The number of them (T) is givenon the first line of the input file. Each test case begins with a linecontaining a single integer numberNthat indicates the numberof plates (1 <= N <= 100000). Then exactlyNlinesfollow, each containing a single word. Each word contains at least two andat most 1000 lowercase characters, that means only letters 'a'through 'z' will appear in the word. The same word mayappear several times in the list.

Output Specification

Yourprogram has to determine whether it is possible to arrange all the plates ina sequence such that the first letter of each word is equal to the lastletter of the previous word. All the plates from the list must be used, eachexactly once. The words mentioned several times must be used that number oftimes.

If there exists such anordering of plates, your program should print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened.".

Sample Input

3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok

Output for the SampleInput

The door cannot be opened.
Ordering is possible.
The door cannot be opened.

看到题目就应该想到只读取字符串的首尾
然后利用欧拉路的存在条件 就是奇数度点只能是0个或2个 另外还要查连通, 以应付 ab ba cd dc 这样 可形成两个环的数据 下面是另外的测试样例: mm mm mm 可行 ab ba cd dc 不可AC代码如下: 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int T, N, mark;
int map[30][30];
int in[30], out[30];
int tree[30];
int vis[30];
char date[1005];

//查连通
int check() {
    int count = 0;
    for(int i = 0; i < 30; i++)
        if(vis[i] && tree[i] == i) {
            count ++;
        }
    return count;
}

int find(int x) {
    return x == tree[x]? x: find(tree[x]);
}

void unit(int a, int b) {
    int p1, p2;
    p1 = find(a);
    p2 = find(b);
    if(p1 != p2)
        tree[p1] = p2;
}

//查度数:
void judge_degree() {
    int num = 0;
    mark = 1;
    for(int i = 0; i < 30; i++) {
        if(in[i] == out[i])
            continue;
        else if (in[i] != out[i]){
            if(out[i] - in[i] == 1 || in[i] - out[i] == 1)
                num++;
            else
                mark = 0;
        }
    }
    if(num != 0 && num != 2)
        mark = 0;
}

//初始化以及输入:
void init() {
    scanf("%d", &N);
    getchar();
    memset(in, 0, sizeof(in));
    memset(out, 0,sizeof(out));
    memset(vis, 0, sizeof(vis));
  
    for(int i = 0; i < 30; i++)
        tree[i] = i;  

    for(int i = 0; i < 30; i++)
        for(int j = 0; j < 30; j++)
            map[i][j] = 0;
        
    for(int i = 0; i < N; i++) {
        int t1, t2, len;
        gets(date);
        len = strlen(date);
        t1 =date[0]-'a';
        t2 =date[len-1]-'a';
        map[t1][t2] = 1;
        in[t2] = in[t2] + 1;
        out[t1] = out[t1] + 1;
        vis[t1] = 1;
        vis[t2] = 2;
        unit(t1, t2);
    }
}

int main() {
    scanf("%d", &T);
    while(T--) {
        init();
        judge_degree();
        if(mark && check() == 1)
            printf("Ordering is possible.\n");
        else
            printf("The door cannot be opened.\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值