POJ 2513 Colored Sticks(trie 歐拉通路 並查集)

來源:互聯網
上載者:User

這個題目只要想是歐拉通路那麼基本上就搞定了

判斷偶拉通路就是度為奇數的點為0個或者2個

用trie樹來做檢索為顏色編號工作

然後用並查集判斷一是否聯通就基本上沒問題了!

#include <iostream>#include <stdio.h>#include <string.h>using namespace std;struct trie{    trie *next[26];    int num;    int singial;//標號}re_root,po[4000000];int pos;int global_num;int now_single;int init(trie *root){    root->num=0;    memset(root->next,0,sizeof(root->next));    return 0;}int insert_trie(trie *root,char *name){    if(name[0]==0)    {        if(root->num==0)        root->singial=global_num,global_num++;        root->num++;        now_single=root->singial;        return 0;    }    if(root->next[name[0]-'a'])    insert_trie(root->next[name[0]-'a'],name+1);    else    {        root->next[name[0]-'a']=&po[pos];        init(&po[pos]);        pos++;        insert_trie(root->next[name[0]-'a'],name+1);    }    return 0;}int even,single;int tri(trie *root){    if(root->num%2==1)    single++;    for(int i=0;i<26;i++)    if(root->next[i])    tri(root->next[i]);    return 0;}int next[800000];int find_root(int a){    int k=a,b;    while(next[a]!=a)    {        a=next[a];    }    while(next[k]!=a)    {        b=next[k];        next[k]=a;        k=b;    }    return a;}int union_set(int a,int b){    a=find_root(a);    b=find_root(b);    if(a < b)    next[b]=a;    else    next[a]=b;    return 0;}int is_one(int len){    int i;    for(i=1;i<len;i++)    if(find_root(i)!=1)    return 0;    return 1;}int main(){    int a,b,i;    char beg[200],end[200];    pos=single=even=0;    global_num=1;    init(&re_root);    for(i=0;i<551000;i++)    next[i]=i;    while(scanf("%s%s",beg,end)!=EOF)    {        insert_trie(&re_root,beg);        a=now_single;        insert_trie(&re_root,end);        b=now_single;        union_set(a,b);    }    tri(&re_root);    if((single==0 || single==2) && is_one(global_num))    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.