码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2337 && ZOJ 1919--Catenyms 【有向图 && 欧拉路判断 && 欧拉路径】

时间:2015-07-31 18:22:52      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

Catenyms
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10427   Accepted: 2726

Description

A catenym is a pair of words separated by a period such that the last letter of the first word is the same as the last letter of the second. For example, the following are catenyms: 
dog.gopher

gopher.rat

rat.tiger

aloha.aloha

arachnid.dog

A compound catenym is a sequence of three or more words separated by periods such that each adjacent pair of words forms a catenym. For example, 

aloha.aloha.arachnid.dog.gopher.rat.tiger 

Given a dictionary of lower case words, you are to find a compound catenym that contains each of the words exactly once.

Input

The first line of standard input contains t, the number of test cases. Each test case begins with 3 <= n <= 1000 - the number of words in the dictionary. n distinct dictionary words follow; each word is a string of between 1 and 20 lowercase letters on a line by itself.

Output

For each test case, output a line giving the lexicographically least compound catenym that contains each dictionary word exactly once. Output "***" if there is no solution.

Sample Input

2
6
aloha
arachnid
dog
gopher
rat
tiger
3
oak
maple
elm

Sample Output

aloha.arachnid.dog.gopher.rat.tiger
***


有向连通图D是欧拉图,当且仅当该图为连通图且D中每个结点的入度=出度;有向连通图D含有欧拉通路,当且仅当该图为连通图且D中除两个结点外,其余每个结点的入度 出度。

欧拉路是欧拉回路的一种情况


有向图存在欧拉路的充要条件为:

①     图是连通的;

②     每个结点的入度=出度,或者 除两个结点外(一个节点的出 = 入度 + 1,一个节点的入度 = 出度 + 1),其余每个结点的入度 出度。


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

int head[30], cnt;

struct node {
    int u ,v, id, next; // 纪录边的编号
    bool flag;//标记这条别是否用过。
};

node edge[2020];


int ans;
string str[1010];
int path[1010];//纪录路径;
int in[30], out[30];

void init(){
    cnt = 0;
    memset(head, -1, sizeof(head));
    memset(in, 0, sizeof(in));
    memset(out, 0, sizeof(out));
}

void add(int u ,int v, int id){
    edge[cnt] = {u, v, id, head[u]};
    head[u] = cnt++;
}

void dfs(int u){
    for(int i = head[u]; i != -1; i = edge[i].next){
        if(!edge[i].flag){
            edge[i].flag = true;
            dfs(edge[i].v);
            path[ans++] = edge[i].id;
        }
    }
}

int main (){
    int T, n;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        for(int i = 0 ; i < n ;++i)
            cin >> str[i];
        sort(str, str + n);
        init();
        int st = 1000;
        for(int i = n - 1; i >= 0; i--){
            int len = str[i].length();
            int u = str[i][0] - 'a';
            int v = str[i][len - 1] - 'a';
            add(u, v, i);
            in[v]++, out[u]++;
            st = min(st, u);
            st = min(st, v);//纪录字典序最小的点作为起点,对应欧拉回路出现的这种情况
        }
        int numst, numed, num;
        numst = numed = num = 0;
        for(int i = 0 ; i < 26; ++i){
            if(!in[i] && !out[i]) continue;
            if(in[i] != out[i]) num++;
            if(out[i] - in[i] == 1){//如果有一个出度比入度大1的点,就从这个点出发,否则从最小的点出发
                numst++;
                st = i;
            }
            else if(out[i] - in[i] == -1)
                numed++;
        }
        if(num > 0){
            if(!(num ==2 && numst == 1 && numed == 1)){//不是欧拉路也不是欧拉回路;
                cout <<"***"<<endl;
                continue;
            }
        }
        ans = 0;
        dfs(st);
        if(ans != n){ //不连通,注意这种判连通的方式
            cout <<"***"<<endl;
            continue;
        }
        for(int i = ans - 1; i >= 0; --i){
             cout<<str[path[i]];
             if(i > 0)
                printf(".");
             else
                printf("\n");
        }
    }
    return 0;
}


在有向图中,判断是不是欧拉路的代码

        int numst, numed, num;
        numst = numed = num = 0;
        for(int i = 0 ; i < 26; ++i){
            if(!in[i] && !out[i]) continue;
            if(in[i] != out[i]) num++;
            if(out[i] - in[i] == 1){//如果有一个出度比入度大1的点,就从这个点出发,否则从最小的点出发
                numst++;
                st = i;
            }
            else if(out[i] - in[i] == -1)
                numed++;
        }
        if(num > 0){
            if(!(num ==2 && numst == 1 && numed == 1)){
                cout <<"***"<<endl;
                continue;
            }
        }



在网上看到另一种判断有向图存在欧拉路的代码,可以参考一下

    int cc1 = 0, cc2 = 0;
    for(int i = 0;i < 26;i++){
        if(out[i] - in[i] == 1){
            cc1++;
            st = i;//如果有一个出度比入度大1的点,就从这个点出发,否则从最小的点出发
        }
        else if(out[i] - in[i] == -1)
            cc2++;
        else if(out[i] - in[i] != 0)
            cc1 = -1;
    }
    if(! ( (cc1 == 0 && cc2 == 0) || (cc1 == 1 && cc2 == 1) )){
        printf("***\n");
        continue;
    }



版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 2337 && ZOJ 1919--Catenyms 【有向图 && 欧拉路判断 && 欧拉路径】

标签:

原文地址:http://blog.csdn.net/hpuhjh/article/details/47172945

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!