Poj 2513 many RE, many WA

Source: Internet
Author: User

Many errors have been handed over dozens of times. Haha ~

Summary:

RE: array out of bounds

WA: spelling of letters

Create a trie tree and add a query set to query graph connectivity and easily record the degree of each vertex.

View Code

// Runtime error cause: values are assigned from 1 to MAX during initialization. The array is out of bounds. Remember this !!!
// WA cause: Impossible misspelling .. Success!
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <iostream>
Using namespace std;
Const int MAX = 500010;
Class trie {
Public:
Bool flag;
Int id;
Trie * child [27];
Trie ()
{
Flag = false;
Id = 0;
Memset (child, 0, sizeof (child ));
}
} Root;
Int color = 0;
Int degree [MAX];
Int p [MAX];
Int find (int x) {return p [x] = x? X: p [x] = find (p [x]);}
Void unio (int a, int B)
{
Int x = find ();
Int y = find (B );
If (x! = Y)
P [x] = y;
}
Int insert (char * s)
{
Class trie * cur = & root;
Int len = strlen (s );
For (int I = 0; I <len; I ++)
{
Int id = s [I]-'A ';
If (! Cur-> child [id])
Cur-> child [id] = new trie;
Cur = cur-> child [id];
}
If (cur-> flag) return cur-> id;
Else
{
Cur-> flag = true;
Cur-> id = ++ color;
Return cur-> id;
}
}
Int main ()
{
Int I, j;
Char s1 [11], s2 [11];
For (I = 1; I <= 500000; I ++)
{
P [I] = I;
Degree [I] = 0;
}
While (scanf ("% s", s1, s2 )! = EOF)
{
I = insert (s1 );
J = insert (s2 );
Degree [I] ++;
Degree [j] ++;
Unio (I, j );
}
Int r = find (1 );
Int num = 0;
For (j = 1; j <= color; j ++)
{
If (degree [j] % 2 = 1) num ++;
If (num> 2)
{
Printf ("Impossible \ n ");
Return 0;
}
If (find (j )! = R)
{
Printf ("Impossible \ n ");
Return 0;
}
}
If (num = 1) printf ("Impossible \ n ");
Else printf ("Possible \ n ");
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.