Poj 1182 food chain (type and query set)

Source: Internet
Author: User

Food Chain
Time Limit:1000 MS Memory Limit:10000 K
Total Submissions:41805 Accepted:12160

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


The idea comes from and detailed explanations can be found in the drifting mavericks


Ideas:

Check the set and maintain an array of pre-root nodes and an array of links (offsets) from rel-root nodes to this point. Rel has only three values: 0, 1, and 2. 0 indicates the same relationship, 1 indicates that the root node eats the node, and 2 indicates that the root node is eaten by the node.

When op, x, and y are input, fx = Find (x), fy = Find (y); If fx! = Fy, merge the fx set and fy set. pre [fy] = fx; fx-> fy = fx-> x + x-> y + y-> fy; x-y is the op-1, that is, rel [fy] = (rel [x] + op-1-rel [y] + 3) % 3; If fx = fy, check whether there is a conflict, x-> y = x-> fx + fx-> y, that is, test (-rel [x] + rel [y] + 3) Whether % 3 is equal to the op-1.

Update the rel array of each vertex when searching. Update the rel Array Based on the previous rel and the current rel of its father. rel [x] = (rel [px] + rel [x]) % 3;

Ps: This question is very difficult. It only requires a single set of data. WA is required for multiple groups.


Code:

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Pragma comment (linker, "/STACK: 102400000,102400000") # define maxn 50005 # define MAXN 100005 # define OO (1 <31) -1 # define mod 90003 # define INF 0x3f3f3f3f # define pi acos (-1.0) # define eps 1e-6typedef long ll; using namespace std; int n, m, ans, tot, flag, cnt; int pre [maxn], rel [maxn]; int Find (int x) {if (x = pre [x]) return x; int tmp = pre [x]; pre [x] = Find (pre [x]); rel [x] = (rel [tmp] + rel [x]) % 3; return pre [x];} bool Merge (int op, int x, int y) {int fx = Find (x), fy = Find (y); if (fx = fy) // determine whether a conflict exists in the same set {if (-rel [x] + rel [y] + 3) % 3! = Op-1) return true;} else // merge {pre [fy] = fx; rel [fy] = (rel [x] + op-1-rel [y] + 3) % 3;} return false;} int main () {int I, j, t, op, x, y; scanf ("% d", & n, & m); for (I = 1; I <= n; I ++) pre [I] = I, rel [I] = 0; ans = 0; for (I = 1; I <= m; I ++) {scanf ("% d", & op, & x, & y ); if (x> n | y> n | op = 2 & x = y) {ans ++; continue;} if (Merge (op, x, y) ans ++;} printf ("% d \ n", ans); return 0 ;}
          
         
        
       
     
    
   
  
 






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.