Food Chain
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:48390 |
|
Accepted:14109 |
Description
The animal kingdom contains three types of animals A, B, and C. The food chains of these three types constitute an interesting ring. A eats B, B eats C, and C eats.
There are n animals numbered 1-n. Every animal is one of A, B, and C, but we don't know which one it is.
There are two ways to describe the relationship between the food chains of the N animals:
The first statement is "1 x Y", indicating that X and Y are similar.
The second statement is "2 x Y", which indicates that X eats y.
This person speaks K sentences one by one for N animals in the preceding two statements. These K sentences are true or false. When one sentence meets the following three conditions, this sentence is a lie, otherwise it is the truth.
1) The current statement conflicts with some of the preceding actual statements;
2) In the current statement, X or Y is greater than N, which is false;
3) The current statement indicates that X eats X, which is a lie.
Your task outputs the total number of false statements based on the given n (1 <= n <= 50,000) and K statements (0 <= k <= 100,000.
Input
The first line is two integers N and K, separated by a space.
Each row in the following K rows contains three positive integers, D, X, and Y, which are separated by a space. D indicates the type of the statement.
If D = 1, X and Y are of the same type.
If D = 2, X eats y.
Output
Only one integer indicates the number of false statements.
Sample Input
100 71 101 1 2 1 22 2 3 2 3 3 1 1 3 2 3 1 1 5 5
Sample output
3
Source
Noi 01
And check the application of the Set ~
Here is a very detailed solution report, but his code is a bit lengthy.
Query sets with weights.
First, the weight array is relation [I], indicating the relationship between I and my father.
= 0 is similar
= 1 is eaten by the father
= 2 is eating father
To facilitate processing, read the data and run d -- (to facilitate later processing)
First consider f [I] = f [f [I], how to change the relation [I?
Relation [I] = (relation [I] + relation [f [I]) % 3 (this is obtained through enumeration ...)
If you want to read 2 x Y, a = getfather (x), B = getfather (Y), and f [B] = A, how can we change relation [B?
Note that the path has been compressed, and the height is only two layers.
There are three steps:
1. x becomes y's father, relation [y] = d
2. Then let y become the father of B and use enumeration to know relation [B] = (3-relation [y]) % 3.
3. then use the previously mentioned relation [I] = (relation [I] + relation [f [I]) % 3, you can obtain the value of relation [B] = (D + 3-relation [y] + relation [x]) % 3.
In this way, the relation can be transferred.
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#define M 100005using namespace std;int f[M],relation[M],n;void read(int &tmp){tmp=0;char ch=getchar();int fu=1;for (;ch<'0'||ch>'9';ch=getchar())if (ch=='-') fu=-1;for (;ch>='0'&&ch<='9';ch=getchar())tmp=tmp*10+ch-'0';tmp*=fu;}int Getfather(int x){if (x!=f[x]){int fx=Getfather(f[x]);relation[x]=(relation[x]+relation[f[x]])%3;f[x]=fx;}return f[x];}int Union(int x,int y,int k){int fx=Getfather(x),fy=Getfather(y);if (fx==fy){if ((relation[y]+3-relation[x])%3!=k) return 1;return 0;}f[fy]=fx;relation[fy]=(k+3-relation[y]+relation[x])%3;return 0;}int main(){int n,k; read(n),read(k);for (int i=1;i<=n;i++)f[i]=i,relation[i]=0;int ans=0;while (k--){int d,x,y;read(d),read(x),read(y);if (x>n||y>n||(x==y&&d==2))ans++;else if (Union(x,y,d-1))ans++;}cout<<ans<<endl;return 0;}
Perception:
1. The key to checking the set with the weight is to find the transfer method of the weight array during path compression.
[Poj 1182] Food Chain