[POJ2513 Colored Sticks]

[题目来源]:POJ2513

[关键字]:字典树 并查集 欧拉路

[题目大意]:给定许多根木棒,两边分别涂有不同颜色,问能否将他们连成一条直线。规定只能将相同颜色的两端相连。

//=====================================================================================================

[分析]:把木棒两端看成节点,将木棒看成边便构成了一个无向图,题目要求便转换成了问这个图里是否有一条欧拉路。可以先用字典树求出各个颜色出现的次数,即节点的度。然后判断欧拉路成立的前提条件:奇数度的节点只能有0或2个。再用并查集判断是否是一个连通图,即所有点在并查集的祖先唯一。根据这些即可判断欧拉路是否存在。

[代码]:

View Code
  1 program Project1;
2 type
3 rec = record
4 t: boolean;
5 num: longint;
6 next: array['a'..'z'] of longint;
7 end;
8 arr = array[0..250000] of string[10];
9 arrr = array[0..500000] of longint;
10 var
11 numx, numy, f, r: arrr;
12 x, y: arr;
13 tree: array[0..1000000] of rec;
14 tn, tot, n: longint;
15
16 procedure print(x: longint);
17 begin
18 case x of
19 1: writeln('Possible');
20 -1: writeln('Impossible');
21 end;
22 halt;
23 end;
24
25 function get(k: longint):longint;
26 begin
27 if f[k] = k then exit(k);
28 f[k] := get(f[k]);
29 get := f[k];
30 end;
31
32 procedure init;
33 var
34 s: string;
35 i: longint;
36 begin
37 n := 0;
38 while not seekeof do
39 begin
40 inc(n);
41 readln(s);
42 x[n] := copy(s,1,pos(' ',s)-1);
43 delete(s,1,pos(' ',s));
44 y[n] := s;
45 end;
46 if n = 0 then print(1);
47 //for i := 1 to n do writeln(x[i],' ',y[i]);
48 end;
49
50 procedure work;
51 var
52 i, j, now, temp, xx, yy, root: longint;
53 begin
54 fillchar(numx,sizeof(numx),0);
55 fillchar(numy,sizeof(numy),0);
56 for i := 1 to n do
57 begin
58 now := 1;
59 for j := 1 to length(x[i]) do
60 if tree[now].next[x[i][j]] <> 0 then now := tree[now].next[x[i][j]]
61 else
62 begin
63 inc(tn);
64 tree[now].next[x[i][j]] := tn;
65 now := tn;
66 end;
67 if tree[now].t then
68 begin
69 numx[i] := tree[now].num;
70 inc(r[numx[i]]);
71 end
72 else
73 begin
74 tree[now].t := true;
75 inc(tot);
76 numx[i] := tot;
77 tree[now].num := tot;
78 r[tot] := 1;
79 end;
80 end;
81 for i := 1 to n do
82 begin
83 now := 1;
84 for j := 1 to length(y[i]) do
85 if tree[now].next[y[i][j]] <> 0 then now := tree[now].next[y[i][j]]
86 else
87 begin
88 inc(tn);
89 tree[now].next[y[i][j]] := tn;
90 now := tn;
91 end;
92 if tree[now].t then
93 begin
94 numy[i] := tree[now].num;
95 inc(r[numy[i]]);
96 end
97 else
98 begin
99 tree[now].t := true;
100 inc(tot);
101 numy[i] := tot;
102 tree[now].num := tot;
103 r[tot] := 1;
104 end;
105 end;
106 {writeln(tot);
107 for i := 1 to n do writeln(numx[i],' ',numy[i]);
108 for i := 1 to tot do writeln(r[i]);}
109 //=========================================
110 temp := 0;
111 for i := 1 to tot do
112 if odd(r[i]) then inc(temp);
113 if not ((temp = 0) or (temp = 2)) then print(-1);
114 for i := 1 to tot do f[i] := i;
115 for i := 1 to n do
116 begin
117 xx := get(numx[i]);
118 yy := get(numy[i]);
119 if xx <> yy then f[xx] := yy;
120 end;
121 //=========================================
122 root := get(1);
123 for i := 1 to tot do
124 if get(i) <> root then print(-1);
125 print(1);
126 end;
127
128 begin
129 //assign(input,'d:\1.in');reset(input);
130 //assign(output,'d:\1.out');rewrite(output);
131 init;
132 work;
133 //close(input);
134 //close(output);
135 end.



你可能感兴趣的:(color)