# Bzoj 1016

View Code
  1 //bzoj 1016 [JSOI2008] zuixiaoshengchengshujishu
2 const
3         maxm=1111;
4         maxn=111;
5         inf='1.txt';
6         def=31011;
7 type
8         type_edge=record
9           a, b: longint;
10           cost: qword;
11         end;
12 var
13         edge: array[0..maxm]of type_edge;
14         f, f_2:array[0..maxn]of longint;
15         n, m: longint;
16         cnt, ans, key: qword;
17 procedure qsort(b, e: longint);
18 var
19         i, j, x: longint;
20         k: type_edge;
21 begin
22   i := b; j := e; x := edge[(i+j)>>1].cost;
23   repeat
24     while edge[i].cost<x do inc(i);
25     while edge[j].cost>x do dec(j);
26     if i<=j then begin
27       k := edge[i]; edge[i] := edge[j]; edge[j] := k;
28       inc(i); dec(j);
29     end;
30   until i>j;
31   if j>b then qsort(b, j);
32   if i<e then qsort(i, e);
33 end;
34
35 function getf(x: longint): longint;
36 begin
37   if f[x] = x then exit(x);
38   f[x] := getf(f[x]);
39   exit(f[x]);
40 end;
41
42 function getfex(x: longint): longint;
43 begin
44   if x=f_2[x] then exit(x);
45   getfex := getfex(f_2[x]);
46 end;
47
48 procedure init;
49 var
50         i: longint;
51 begin
53   for i := 1 to m do with edge[i] do readln(a, b, cost);
54   for i := 1 to n do begin
55     f[i] := i; f_2[i] := i;
56   end;
57   qsort(1, m);
58   ans := 1; key := 0;
59 end;
60
61 procedure dfs(st, ed, num: longint);
62 var
63         a, b: longint;
64 begin
65   if num=0 then begin
66     inc(cnt); exit;
67   end;
68   if st>ed then exit;
69   a := getfex(edge[st].a); b := getfex(edge[st].b);
70   if a<>b then begin
71     f_2[a] := b;
72     dfs(st+1, ed, num-1);
73     f_2[a] := a;
74   end;
75   dfs(st+1, ed, num);
76 end;
77
78 function find(st, ed, num: longint): qword;
79 begin
80   cnt := 0;
81   dfs(st, ed, num);
82   if cnt=0 then find := 1
83     else find := cnt;
84 end;
85
86 procedure main;
87 var
88         i, j, now, num, a, b: longint;
89 begin
90   now := 1; num := 0;
91   for i := 1 to m do begin
92     if (i<>1)and(edge[i].cost<>edge[i-1].cost) then begin
93       ans := ans * find(now, i-1, num) mod def;
94       f_2 := f;
95       num := 0;
96       now := i;
97     end;
98     a := getf(edge[i].a); b := getf(edge[i].b);
99     if a<>b then begin
100       inc(key); inc(num);
101       f[a] := b;
102     end;
103     if key=n-1 then break;
104   end;
105   while (i<m)and(edge[i].cost=edge[i+1].cost) do inc(i);
106   ans := ans * find(now, i, num) mod def;
107 end;
108
109 procedure print;
110 begin
111   if key=n-1 then writeln(ans)
112     else writeln(0);
113 end;
114
115 begin
116   assign(input,inf); reset(input);
117   init;
118   main;
119   print;
120 end.
posted @ 2012-04-25 12:26  F.D.His.D  阅读(...)  评论(...编辑  收藏