UVA10129 POJ1386 HDU1116 ZOJ2016 Play on Words【欧拉回路+并查集】

708 篇文章 19 订阅
509 篇文章 9 订阅

Play on Words

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8998    Accepted Submission(s): 3102


Problem Description
Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve 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 large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word ``acm'' can be followed by the word ``motorola''. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door.
 

Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters 'a' through 'z' will appear in the word. The same word may appear several times in the list.
 

Output
Your program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. All the plates from the list must be used, each exactly once. The words mentioned several times must be used that number of times.
If there exists such an ordering 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
 

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

Source


问题链接:UVA10129 POJ1386 HDU1116 ZOJ2016 Play on Words

问题简述

  先输入测试用例数T,每个测试用例包括整数N和N个单词数,问这些单词首尾字母能否接成一条龙?

问题分析

  这是一个单词接龙问题,一个单词的尾字母和另外一个单词的首字母相同则可以接成龙。其中,每个单词只包含小写字母。单词的首字母和尾字母可以作为图的一个结点,从单词的首字母到尾字母画一条有向边,单词接龙问题就变成一笔画问题,或称为欧拉路径问题。

  欧拉路径问题有两个条件,一是图是联通的;二是所有结点出入度相同,或者只有两个结点出入度差为1。

程序说明

  用并查集来判定图是否是联通的。

  程序提交后,除了POJ1386出现TLE,其他3个在线评判都AC了。在高人指点之下,程序中增加了69行的代码“ios::sync_with_stdio(false);”,就都可以通过了。这是由于C++的输入输出比较慢的原因,通过这行代码,改善了I/O的速度。

  这个语句也可以写为“std::ios::sync_with_stdio(false);”。担心C++输入输出慢,可以使用该语句改善速度。

  代码不够简洁,又写了一个简洁版。


AC的C++语言程序(简洁版)如下:

/* UVA10129 POJ1386 HDU1116 ZOJ2016 Play on Words */

#include <iostream>
#include <string.h>

using namespace std;

const int N = 26;
int f[N], cnt;

void UFInit(int n)
{
    for(int i = 0; i < n; i++)
        f[i] = i;
    cnt = n;
}

int Find(int a) {
    return a == f[a] ? a : f[a] = Find(f[a]);
}

void Union(int a, int b)
{
    a = Find(a);
    b = Find(b);
    if (a != b) {
        f[a] = b;
        cnt--;
    }
}

int degreeout[N];
int degreein[N];

// 出入度检查:return nopathflag
bool degreeincheck()
{
    int startcount = 0, endcount = 0;

    for(int i=0; i<N; i++)
        if(degreeout[i] != degreein[i]) {
            if((degreeout[i] - degreein[i]) == 1) {
                if(++startcount > 1)
                    return true;
            } else if((degreeout[i] - degreein[i]) == -1) {
                if(++endcount > 1)
                    return true;
            } else
                return true;
        }
    return false;
}

int main()
{
    int t, n, src, dest;

    ios::sync_with_stdio(false);

    cin >> t;
    while(t--) {
        UFInit(N);

        memset(degreeout, 0, sizeof(degreeout));
        memset(degreein, 0, sizeof(degreein));

        // 输入测试用例的数据,构建并查集,统计各个结点的度
        cin >> n;
        string s;
        while(n--) {
            cin >> s;

            src = s[0] - 'a';
            dest = s[s.size() - 1] - 'a';

            degreeout[src]++;
            degreein[dest]++;

            Union(src, dest);
        }

        // 判断图的联通性
        for(int i=0; i<N; i++)
            if(degreeout[i] || degreein[i]) {
                ;
            } else
                cnt--;
        bool nopathflag = (cnt != 1);

        // 判定是否存在欧拉路径:出入度检查
        if(!nopathflag)
            nopathflag = degreeincheck();

        // 输出结果
        if(nopathflag)
            cout << "The door cannot be opened." << endl;
        else
            cout << "Ordering is possible." << endl;

    }

    return 0;
}



AC的C++语言程序如下:

/* UVA10129 Play on Words */

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

const int N = 26;

// 并查集类
int v[N];
class UF {
private:
    int length;
public:
    UF(int n) {
        length = n;
        for(int i=0; i<=n; i++)
            v[i] = i;
    }

    // 压缩
    int Find(int x) {
        if(x == v[x])
            return x;
        else
            return v[x] = Find(v[x]);
    }

    bool Union(int x, int y) {
        x = Find(x);
        y = Find(y);
        if(x == y) {
            return false;
        } else {
            v[x] = y;
            return true;
        }
    }
};

int degreeout[N];
int degreein[N];

// 出入度检查:return nopathflag
bool degreeincheck()
{
    int startcount = 0, endcount = 0;

    for(int i=0; i<N; i++)
        if(degreeout[i] != degreein[i]) {
            if((degreeout[i] - degreein[i]) == 1) {
                if(++startcount > 1)
                    return true;
            } else if((degreeout[i] - degreein[i]) == -1) {
                if(++endcount > 1)
                    return true;
            } else
                return true;
        }
    return false;
}

int main()
{
    int t, n, src, dest;

    ios::sync_with_stdio(false);

    cin >> t;
    while(t--) {
        UF uf(N+1);

        memset(degreeout, 0, sizeof(degreeout));
        memset(degreein, 0, sizeof(degreein));

        // 输入测试用例的数据,构建并查集,统计各个结点的度
        cin >> n;
        string s;
        while(n--) {
            cin >> s;

            src = s[0] - 'a';
            dest = s[s.size() - 1] - 'a';

            degreeout[src]++;
            degreein[dest]++;

            uf.Union(src, dest);
        }

        // 判断图的联通性
        bool nopathflag = false;
        int root = -1;
        for(int i=0; i<N; i++) {
            if(degreeout[i] || degreein[i]) {
                if(root == -1)
                    root = uf.Find(i);
                else if(uf.Find(i) != root) {
                    nopathflag = true;
                    break;
                }
            }
        }

        // 判定是否存在欧拉路径:出入度检查
        if(!nopathflag)
            nopathflag = degreeincheck();

        // 输出结果
        if(nopathflag)
            cout << "The door cannot be opened." << endl;
        else
            cout << "Ordering is possible." << endl;

    }

    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值