測試賽A - Colored Sticks(並查集+字典樹+歐拉迴路)

來源:互聯網
上載者:User

標籤:des   style   blog   color   使用   os   strong   io   

A - Colored SticksTime Limit:5000MS     Memory Limit:128000KB     64bit IO Format:%I64d & %I64uSubmit Status

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.使用字典樹來給每個顏色定一個序號,並統計出現的次數,使用並查集判斷是否全部的點聯通,在用歐拉迴路,看整個圖是不是可以一次走完,判斷方法,如果奇數度數的點為0或2個,那麼一定可以走通
#include <cstdio>#include <cstring>int p[520000] , top , q[520000];struct node{    int flag ;    node *next[27] ;} *head;node *newnode(){    node *p = new node ;    p->flag = 0;    for(int i = 0 ; i < 27 ; i++)        p->next[i] = NULL ;    return p;}int gettree(node *head,char *s){    int i , l = strlen(s) ;    node *p = head ;    for(i = 0 ; i < l ; i++)    {        int k = s[i] - 'a' ;        if(p->next[k]==NULL)            p->next[k] = newnode();        p = p->next[k] ;    }    if( p->flag == 0 )        p->flag = top++ ;    return p->flag ;}int f(int x){    int r , k , l ;    r = x ;    while( r != p[r] )        r = p[r] ;    k = x ;    while( k != r )    {        l = p[k] ;        p[k] = r ;        k = l ;    }    return r ;}void add(int u,int v){    u = f(u) ;    v = f(v) ;    if( u != v )        p[u] = v ;}char s1[11] , s2[11] ;int main(){    int i , n , k1 , k2 ;    memset(q,0,sizeof(q));    head = newnode() ;    for(i = 0 ; i <= 520000 ; i++)        p[i] = i ;        top = 1 ;    while( scanf("%s %s", s1, s2 ) !=EOF )    {        k1 = gettree(head,s1);        k2 = gettree(head,s2);        q[k1]++ ;        q[k2]++ ;        add(k1,k2) ;    }    int k = f(1) ;    for(i = 2 ; i < top ; i++)        if( k != f(i) )            break;    if( i < top )        printf("Impossible\n");    else    {        int ji = 0 ;        for(i = 1 ; i < top ; i++)            if( q[i]%2 )                ji++ ;        if(ji ==0 || ji == 2)            printf("Possible\n");        else            printf("Impossible\n");    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.