poj 2513(trie树+并查集+欧拉回路条件)(记得要初始化指针数组)

Colored Sticks
Time Limit: 5000MS Memory Limit: 128000K
Total Submissions: 28302 Accepted: 7481

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 red
red violet
cyan blue
blue magenta
magenta cyan

Sample Output

Possible

Hint

Huge input,scanf is recommended.

Source

The UofA Local 2000.10.14

非常沮丧的 RE 多次。
trie树用链表来实现吧:
结构体中的 next指针用来寻找下一个字母node。
isdict变量表示这个结点是不是一个词典中的单词。(必须有!)
struct node {
    node * next[26];
    bool isdict;
};

然后遍历输入串,在树上行走。发现NULL指针就new一个。一定要注意 new后要把新结点的next指针都初始化为NULL。否则可能是野指针,导致RE。
    while(s[i]) {
        int a = s[i] - 'a';
        assert(a < 26);
        if (cur->next[a] == NULL) {
            cur->next[a] = new node;
            memset(cur->next[a]->next, NULL, sizeof(cur->next[a]->next));
            cur->next[a]->f = false;
        }   
        cur = cur->next[a];
        i++;
    }   

首先要判断图的联通(用并查集后,所有点的root相同)
其次要判断入度为奇数的点,或者为0,或者为2。
以上两个条件都满足时,则必存在欧拉回路,否则必不存在欧拉回路。


提交记录:
1-N、RE。 记得 要初始化指针。否则结果不可预知。
N+1、AC。

/*Source Code
Problem: 2513		User: 775700879
Memory: 87916K		Time: 1391MS
Language: G++		Result: Accepted

    Source Code*/

    #include 
    
    
     
     
    #include 
     
     
      
      
    #include 
      
      
       
       
    #include 
       
       
        
        
    #include 
        
        
          #include 
          #include 
          
            #include 
           
             #include 
            
              #include 
             
               #define oo 0x3f3f3f3f #define N 1000000 using namespace std; int icount[N]; int par[N]; int rank[N]; int color_num = 0; struct node { bool f; int num; struct node * next[30]; }; node * head = new node; int find(int a) { int x = par[a]; if (x == a) return x; else return par[a] = find(x); } void unite(int a, int b) { int x = find(a); int y = find(b); if (x == y) return ; if (rank[x] < rank[y]) { par[x] = y; } else { par[y] = x; if (rank[x] == rank[y]) rank[x] ++; } } int find_num(char s[]) { int i = 0; node * cur = head; while(s[i]) { int a = s[i] - 'a'; assert(a < 26); if (cur->next[a] == NULL) { cur->next[a] = new node; memset(cur->next[a]->next, NULL, sizeof(cur->next[a]->next)); cur->next[a]->f = false; } cur = cur->next[a]; i++; } if (cur->f == false) { cur->f = true; cur->num = color_num++; } return cur->num; } int main() { char a[30], b[30]; int i, j, k; int numa, numb; for (i = 0; i < N; i++) {par[i] = i; rank[i] = 0;} memset(head->next, NULL, sizeof(head->next)); while (EOF != scanf("%s%s", a, b)) { numa = find_num(a); numb = find_num(b); assert(numa < N); assert(numb < N); icount[numa]++; icount[numb]++; unite(numa, numb); } bool flag = true; int num = 0; if (color_num >= N) return 0; for (i = 0; i < color_num; i++) { if (icount[i] % 2 != 0) num++; if (num > 2) { flag = false; break; } } if (num == 1) flag = false; if (flag) { int root = par[0]; for (i = 0; i < color_num; i++) { if (par[i] != root) { flag = false; break; } } } if (flag) printf("Possible\n"); else printf("Impossible\n"); return 0; } 
              
             
            
           
        
       
       
      
      
     
     
    
    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值