Disjoint-set examples 02. Weighted disjoint-set (poj1182)

Description

There are three types of animals in the animal kingdom A, B, C, three types of animal food chain constitute an interesting ring. A food B, B eat C, C eat A.
Animals prior N, number to 1-N. Each animal is A, B, C in kind, but we do not know it in the end is what kind of.
It was carried out with two versions of this trophic animals consisting of N Description:
The first argument is "1 X Y", represents the X and Y are similar.
The second argument is "2 X Y", X represents eat Y.
This person for N animals, with the above two statements, one sentence by sentence to say K, K this sentence some true, some false. When one of the following three words, this sentence is a lie, the truth is otherwise.
1) if the current true, then some of the previous conflicts, is lie;
2), then the current X or Y is larger than N, is lie;
3) X represents eat, then the current X, is lie.
Your job is given according to N (1 <= N <= 50,000) and K words (0 <= K <= 100,000 ), the total number of output lies.

Input

The first line of two integers N and K, separated by a space.
K The following three lines each is a positive integer D, X, Y, separated by a space between the two numbers, where D indicates the type of argument.
If D = 1, it indicates that X and Y are similar.
If D = 2, then X represents eat Y.

Output

Only one integer representing the number of lies.

Sample Input

100 7
1 101 1 
2 1 2
2 2 3 
2 3 3 
1 1 3 
2 3 1 
1 5 5

Sample Output

3

Ideas:

  This question is a classic example weighted disjoint-set, the relative relationship between animals, there are three possibilities: the same, eating, being eaten

  Can then create an array relation [], while the configuration of the array pre [], pre [i] represents the parent node i. relation [i] = 0 and i represents the pre [i] the same, i = 1 represents eat pre [i], = 2 represents the pre [i] i eat.

 

  About Path Compression: (find function), we can find the weight (a value corresponding relationship) is not directly accumulated, but to find the law can be found A-> C = (A-> B + B-> C)% 3, thereby updating the accumulated value relationship requires modulo 3. find function is as follows:

1 int find(int x) {
2     if (x != pre[x]) {
3         int px = find(pre[x]);
4         relation[x] = (relation[x] + relation[pre[x]]) % 3;
5         pre[x] = px;
6     }
7     return pre[x];
8 }

  As for the merging process, it can be seen from the above law, but modulo a consolidated basis weight typically disjoint-set with the

General and with the right to check the combined set of ideas:

------>

 

1 if (fx != fy) {
2     pre[fx] = fy;
3     value[fx] = judge + value[y] - value[x]
4 }

 

Overall, c language codes are as follows:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define maxn 50010
 4 int relation[maxn], pre[maxn];
 5 // relation[i] =    0: i与pre[i]为同类   1: i吃pre[i]   2: pre[i]吃i
 6 
 7 void init() {
 8     for (int i = 0; i < maxn; i++) {
 9         relation[i] = 0;
10         pre[i] = i;
11     }
12 }
13 
14 int find(int x) {
15     if (x != pre[x]) {
16         int px = find(pre[x]);
17         relation[x] = (relation[x] + relation[pre[x]]) % 3;
18         pre[x] = px;
19     }
20     return pre[x];
21 }
22 
23 int jion(int x, int y, int judge) {
24     int fx = find(x);
25     int fy = find(y);
26     if (fx == fy) {
27         if ((relation[x] - relation[y] + 3) % 3 != judge)   return 1;
28         else    return 0;
29     }
30     else {
31         pre[fx] = fy;
32         relation[fx] = (relation[y] + judge - relation[x] + 3) % 3;
33     }
34     return 0;
35 }
36 
37 int main() {
38     int N, K, x, y, judge, ans = 0;
39     scanf("%d%d", &N, &K);
40     init();
41     for (int i = 0; i < K; i++) {
42         scanf("%d%d%d", &judge, &x, &y);
43         if (x > N || y > N || (x == y && judge == 2)) {
44             ans++;
45             continue;
46         }
47         if (jion(x, y, judge-1)) {
48             ans++;
49         }
50     }
51     printf("%d\n", ans);
52     return 0;
53 }

 

Guess you like

Origin www.cnblogs.com/y2ek/p/12654234.html