zoukankan      html  css  js  c++  java
  • POJ 2513, Colored Sticks

    Time Limit: 5000MS  Memory Limit: 128000K
    Total Submissions: 13696  Accepted: 3384


    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


    // POJ2513.cpp : Defines the entry point for the console application.
    //

    #include 
    <iostream>
    using namespace std;

    //Tries
    struct Trie
    {
        Trie():id(
    -1),end(false){memset(next, 0sizeof(next));}
        
    int id;
        
    bool end;
        Trie
    * next[26];
    };
    static int num = 0;
    int GetID(char *x, Trie* root)
    {
        Trie 
    *temp = root;
        
    for (int i = 0; i < strlen(x); ++i)
        {
            
    if (temp->next[x[i]-'a'== NULL)
                temp
    ->next[x[i]-'a'= new Trie;
            temp 
    = temp->next[x[i]-'a'];
        }
        
    if (temp->end)return temp->id;
        temp
    ->end = true;
        temp
    ->id = num++;
        
    return temp->id;
    }

    //Disjoint set
    int Find(int x, int f[])
    {
        
    if(x != f[x])
            
    return f[x] = Find(f[x], f);
        
    return f[x];
    };
    void Union(int x,int y, int f[])
    {
        f[Find(x, f)] 
    = f[Find(y, f)];
    };

    int main(int argc, char* argv[])
    {
        Trie root;
        
    char w[30], w1[12], w2[12];
        
    int degree[500001];
        memset(degree,
    0,sizeof(degree));
        
    int f[500001];
        
    for(int i = 0; i < 500001++i) f[i] = i;
        
        
    //create Trie tree
        while (gets(w) && w[0!= 0)
        {
            sscanf(w,
    "%s %s", w1, w2);
            
    int x = GetID(w1, &root);
            
    int y = GetID(w2, &root);
            
    ++degree[x];
            
    ++degree[y];
            Union(x,y,f);
        }

        
    //check odd points
        int odd = 0;
        
    for (int i = 0; i < num; ++i)
            
    if (degree[i] & 1 != 0++odd;

        
    //check connected
        int x = Find(1,f);
        
    for (int i = 0; i < num ; ++i)
            
    if (x != Find(i,f))
            {
                odd 
    = -1;
                
    break;
            };    

        
    if (odd == 0 || odd == 2) cout << "Possible" << endl;
        
    else cout << "Impossible" << endl;
        
    return 0;
    }

  • 相关阅读:
    ORACLE函数介绍
    msdn的javascript文章转载
    baidu的高级搜索命令
    周杰伦 青花瓷 蒲公英的约定 我不配 彩虹 歌词和下载
    谷歌百度相争 新浪渔翁得利
    tomcat+jsp web运行环境搭建
    C#2008与.NET 3.5 高级程序设计读书笔记(24) LINQ API编程
    C#2008与.NET 3.5 高级程序设计读书笔记(16) 类型反射、晚期绑定和基于特性的编程
    C#2008与.NET 3.5 高级程序设计读书笔记(15) .NET程序集入门
    C#2008与.NET 3.5 高级程序设计读书笔记(25) WCF
  • 原文地址:https://www.cnblogs.com/asuran/p/1579981.html
Copyright © 2011-2022 走看看