Topic Links:
Http://poj.org/problem?
id=2513
Colored sticks
Time Limit: 5000MS |
|
Memory Limit: 128000K |
Total Submissions: 30955 |
|
Accepted: 8159 |
Description You are given a bunch of wooden sticks. Each endpoint of all stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints so touch is of the same col Or?Input Input is a sequence of lines, each line contains and words, separated by spaces, giving the colors of the endpoints of one Stick. A word is a sequence of lowercase letters no longer than characters. There is no more than 250000 sticks.Output If The sticks can is aligned in the desired-line, 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.Source The UofA Local 2000.10.14 |
[
problem_id=2513 "style=" Text-decoration:none ">submit" [Go back] [
problem_id=2513 "style=" Text-decoration:none ">status" [discuss]
topic Meaning:
There are n sticks. There are two colors on each end of the stick, to see if the sticks can be together so that the back end color of the previous stick is the same as the front color of the previous one.
Problem Solving Ideas:
Euler's Path + dictionary tree + and check set
Note that there is no forward edge. Note that you want to infer connectivity. Mapping directly with map will time out and write your own dictionary tree.
Code:
#include <CSpreadSheet.h> #include <iostream> #include <cmath> #include <cstdio> #include <sstream> #include <cstdlib> #include <string> #include <string.h> #include <cstring># include<algorithm> #include <vector> #include <map> #include <set> #include <stack># include<list> #include <queue> #include <ctime> #include <bitset> #include <cmath># Define EPS 1e-6#define INF 0x3f3f3f3f#define PI acos ( -1.0) #define LL __int64#define ll long long#define Lson l,m, (RT<&L t;1) #define Rson m+1,r, (rt<<1) |1#define m 1000000007//#pragma comment (linker, "/stack:1024000000,1024000000") using namespace std; #define MAXN 550000int dei[maxn],a[maxn],cnt,la;struct node{struct Node * next[30]; int nu;} Node[maxn*15],root;int Insert (char *a) {Node * p=&root; while (*a) {if (p->next[*a-' a ' +1]==null) {node[++la].nu=0; memset (node[la].next,null,sizeof (Node[la].next)); p->next[*a-' A ' +1]=&node[la]; } p=p->next[*a-' a ' +1]; a++; } if (!p->nu) p->nu=++cnt; return p->nu;} int Find (int x) {int temp=x; while (X!=fa[x]) x=fa[x]; while (fa[temp]!=x) {int cur=fa[temp]; Fa[temp]=x; Temp=cur; } return x;} void Unio (int a,int b) {A=find (a), b=find (b); if (a!=b) fa[a]=b;} int main () {memset (dei,0,sizeof (dei)); String A, B; Char sa[15],sb[15]; cnt=la=0; for (int i=1;i<=maxn-5000;i++) fa[i]=i; memset (root.next,null,sizeof (Root.next)); root.nu=0; while (~SCANF ("%s%s", sa+1,sb+1)) {int A=insert (sa+1); int B=insert (sb+1); dei[a]++,dei[b]++; Unio (A, b); } bool Ans=true; int nui=0,nuo=0,la=-1; for (int i=1;i<=cnt;i++) {if (la==-1) la=find (i); else if (La!=find (i)) {ans=false; Break } if (dei[i]&1) nui++; } if (!ans| | Nui>2| | nui==1) printf ("impossible\n"); else printf ("possible\n"); System ("pause"); return 0;}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
[Euler] POJ 2513 colored sticks