poj 2513 连接火柴 以及 hdu1116 欧拉通路 好题

来源:互联网 发布:千兆端口和百兆端口 编辑:程序博客网 时间:2024/04/28 02:15
Colored Sticks
Time Limit: 5000MS Memory Limit: 128000KTotal Submissions: 27134 Accepted: 7186

Description

You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?

Input

Input is a sequence of lines, each line contains two words, separated by spaces, giving the colors of the endpoints of one stick. A word is a sequence of lowercase letters no longer than 10 characters. There is no more than 250000 sticks.

Output

If the sticks can be aligned in the desired way, output a single line saying Possible, otherwise output Impossible.

Sample Input

blue redred violetcyan blueblue magentamagenta cyan

Sample Output

Possible

Hint

Huge input,scanf is recommended.

Source

The UofA Local 2000.10.14
 
题意: 有很多个火彩 每头都有颜色   相同的颜色可以和另外的火柴相接  问这些火柴能不能连接成一条线 
 
思路:  很明显这是一个欧拉通路问题      一笔画画完整个通路 
用字典树标记字符串对应的id
用map超时
#include<stdio.h>#include<string>#include<string.h>#include<map>#include<malloc.h>using namespace std;#define N 250000+10char s1[100],s2[100];int rank[N],fa[N],num[N],co=0;struct haha{    int id;    struct haha *next[26];}*root;struct haha * creat(){    int i;    struct haha *p;    p=(struct haha *)malloc(sizeof(struct haha));    p->id=-1;    for(i=0;i<26;i++) p->next[i]=NULL;    return p;};int update(char *s){     int d,pos,i;     struct haha *p;     p=root;d=strlen(s);     for(i=0;i<d;i++)     {         pos=s[i]-'a';         if(p->next[pos]==NULL)         {             p->next[pos]=creat();             p=p->next[pos];         }         else         {             p=p->next[pos];         }     }     if(p->id==-1)     p->id=co++;     return p->id;}int  find(int n){    return n==fa[n]?n:fa[n]=find(fa[n]);}void join(int a,int b){        if(rank[a]>rank[b])        {            fa[b]=a;            rank[a]+=rank[b];        }        else        {            fa[a]=b;            rank[b]+=rank[a];        }}int main(){    int i,j,k,cnt=0,a,b,rem;    for(i=0;i<N;i++)    {        rank[i]=1;fa[i]=i;    }    memset(num,0,sizeof(num));    root=creat();    while(scanf("%s %s",s1,s2)!=EOF)    {         a=update(s1);         b=update(s2);         num[a]++;num[b]++;         a=find(a);b=find(b);         if(a==b) continue;          join(a,b);    }    cnt=0;    int temp=find(0);    for(i=0;i<N;i++)    {        if(num[i]%2==1) cnt++;        if(cnt>2) {printf("Impossible\n");return 0;}        if(num[i]!=0&&find(i)!=temp) {printf("Impossible\n");return 0;}    }    printf("Possible\n");    return 0;}




Play on Words

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


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
32acmibm3acmmalformmouse2okok
 

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

Source
Central Europe 1999
 

Recommend
Eddy

题意   输入n个单词 如果单词a第一个字符和另外一个单词b的最后一个字符相同  a就可以连接到b上      问这么多单词 是否可以连成一条线



思路: 欧拉通路 

注意度的问题      
#include<stdio.h>#include<string.h>char s[1111];int pa[30],used[30],rank[30];struct odd{    int numin;    int numout;}q[100];int find(int n){    return pa[n]==n?n:pa[n]=find(pa[n]);}void add(int a,int b){    if(rank[a]>rank[b])    {         rank[a]+=rank[b];         pa[b]=a;    }    else    {        rank[b]+=rank[a];        pa[a]=b;    }}int in[30],out[30];int main(){    int i,cas,n;    scanf("%d",&cas);    while(cas--)    {        scanf("%d",&n);        for(i=0;i<30;i++)        {            pa[i]=i;            rank[i]=1;            used[i]=0;            in[i]=out[i]=0;        }        for(i=0;i<n;i++)        {            scanf("%s",s);            int len=strlen(s);            int a=s[0]-'a';            int b=s[len-1]-'a';           // printf("%c %c\n",a+'a',b+'a');            in[a]++;out[b]++;            used[a]=used[b]=1;            a=find(a);            b=find(b);            if(a==b) continue;            else add(a,b);        }        int tree=0,flag=1,cnt=0;        for(i=0;i<30;i++)        {        //    printf("i=%d  in=%d out=%d \n",i,in[i],out[i]);             if(used[i]&&pa[i]==i) tree++;             if(tree>1) {flag=0;break;}             if(used[i]&&in[i]!=out[i])             {                   q[cnt].numin=in[i];                   q[cnt].numout=out[i];                   cnt++;             }        }        if(flag==0)        {printf("The door cannot be opened.\n"); continue;}        if(cnt==0)        {           printf("Ordering is possible.\n");continue;        }        if(cnt==2)        {            //printf("ddd");             if(                (q[0].numin-q[0].numout==1&&q[1].numout-q[1].numin==1)||                (q[1].numin-q[1].numout==1&&q[0].numout-q[0].numin==1)                )             {printf("Ordering is possible.\n");continue;}        }        printf("The door cannot be opened.\n");    }    return 0;}




原创粉丝点击