pku2351 Colored Sticks

题目自己去看,网上的题解也是一堆一堆的,但这绝对是练习tire,欧拉路径和并查集的一道经典题目,还是重复一下老掉牙的流程

               1.用tire给每一个颜色字符串编号。

               2.先判断是否是欧拉路径(没有或只有两个奇点)。

               3.用并查集判断图是否是连通的。

过了样例之后,先测一下这组数据:

a b

b c

c a

d e

e f

f d

答案是impossible,如果正确的话,没有大错误就应该AC了。

这题很BT的卡常数,如果编号时用nlogn的SBT,就会超时,想来NlogN和NL,logN≈20,L是10,才差两倍,无语,个人是这么想的~~~~

附上代码:

View Code
  1 program sticks(input,output);
2 var
3 f : array[0..500000] of longint;
4 x,y : array[0..250000] of string[10];
5 numberx,numbery : array[0..500000] of longint;
6 out : array[0..500000] of longint;
7 tire : array[0..1000000,'a'..'z'] of longint;
8 v : array[0..1000000] of boolean;
9 r : array[0..1000000] of longint;
10 n,node,color : longint;
11 function getfather(x: longint ):longint;
12 begin
13 if f[x]=x then
14 exit(x);
15 f[x]:=getfather(f[x]);
16 exit(f[x]);
17 end; { getfather }
18 procedure insect_tire(s :string );
19 var
20 i,now : longint;
21 begin
22 now:=1;
23 for i:=1 to length(s) do
24 begin
25 if tire[now,s[i]]<>0 then
26 now:=tire[now,s[i]]
27 else
28 begin
29 inc(node);
30 tire[now,s[i]]:=node;
31 now:=node;
32 end;
33 end;
34 v[now]:=true;
35 inc(color);
36 r[now]:=color;
37 end; { insect_tire }
38 function find_tire(s :string ):boolean;
39 var
40 now,i : longint;
41 begin
42 now:=1;
43 for i:=1 to length(s) do
44 begin
45 if tire[now,s[i]]=0 then
46 exit(false);
47 now:=tire[now,s[i]];
48 end;
49 if v[now] then
50 exit(true)
51 else
52 exit(false);
53 end; { find_tire }
54 function getnumber_tire(s :string ):longint;
55 var
56 now,i : longint;
57 begin
58 now:=1;
59 for i:=1 to length(s) do
60 now:=tire[now,s[i]];
61 exit(r[now]);
62 end; { getnumber_tire }
63 procedure init;
64 var
65 ch : char;
66 begin
67 n:=0;
68 while not eof do
69 begin
70 read(ch);
71 inc(n);
72 x[n]:='';
73 while ch<>'' do
74 begin
75 x[n]:=x[n]+ch;
76 read(ch);
77 end;
78 readln(y[n]);
79 end;
80 end; { init }
81 procedure previous;
82 var
83 i : longint;
84 begin
85 node:=1;
86 color:=0;
87 for i:=1 to n do
88 begin
89 if not find_tire(x[i]) then
90 insect_tire(x[i]);
91 numberx[i]:=getnumber_tire(x[i]);
92 if not find_tire(y[i]) then
93 insect_tire(y[i]);
94 numbery[i]:=getnumber_tire(y[i]);
95 end;
96 end; { previous }
97 procedure main;
98 var
99 i : longint;
100 tmp,xx,yy : longint;
101 begin
102 fillchar(out,sizeof(out),0);
103 for i:=1 to n do
104 begin
105 inc(out[numberx[i]]);
106 inc(out[numbery[i]]);
107 end;
108 tmp:=0;
109 for i:=1 to color do
110 if odd(out[i]) then
111 inc(tmp);
112 if (tmp<>0)and(tmp<>2) then
113 begin
114 writeln('Impossible');
115 halt;
116 end;
117 for i:=1 to color do
118 f[i]:=i;
119 for i:=1 to n do
120 begin
121 xx:=getfather(numberx[i]);
122 yy:=getfather(numbery[i]);
123 if xx<>yy then
124 f[yy]:=xx;
125 end;
126 tmp:=getfather(1);
127 for i:=1 to color do
128 if getfather(i)<>tmp then
129 begin
130 writeln('Impossible');
131 halt;
132 end;
133 writeln('Possible');
134 end; { main }
135 begin
136 init;
137 previous;
138 main;
139 end.



posted @ 2011-11-10 14:27  Codinginging  阅读(233)  评论(0编辑  收藏  举报