Pku1182 (Food Chain) & hdu3047 zjnu Stadium

Source: Internet
Author: User

On the basis of the previous question, I have a better understanding.CodeWrite your own understanding

It is also an extended application. I personally feel that I have a deeper understanding of the query set. How can I continue to do it?

Daniel also gave a formula to maintain the offset under normal circumstances.

Thanks to this Daniel for sharing:

Here we combine the two sets and point the offset of the hanging set:
Kind [B]=(Kind [x]-Kind [y]+4)%3;
Think about whether the previous question is similar.
In fact, the formula for the previous question can also be changed
Kind [B]=(Kind [x]-Kind [y]+3)%2;
A similar conclusion can be drawn from several animal loops, so we will encounter 4, 5, 6, and 7 in the future... An animal's food chain, should you do it? Pai_^

 

View code

# Include <stdio. h>
# Define Maxn 50010
Int F [maxn], R [maxn], N;
Int Find ( Int X)
{
If (X = f [x])
Return X;
Int T = find (F [x]);
R [x] = (R [x] + R [f [x]) % 3 ;// Based on the relationship between x and the parent node and the relationship between the parent node and the root node, the relationship between x and the root node is introduced.
F [x] = T;
Return F [x];
}
Int Union ( Int X, Int Y, Int C)
{
If (X> N | Y> N)
Return 1 ;
Int A = find (x), B = find (y );
If (C = 1 )
{
If (A = B)
{
If (R [x]! = R [y]) // Situations of different classes
Return 1 ;
}
Else {
F [B] =;
R [B] = (R [x]-R [y] + 3 ) % 3 ;
// X is similar to Y. Note that the formula for dividing a into B is different from that for dividing B into.
// I can try again. I suddenly found that, as X and Y are similar, they can still be launched.
}
}
Else {
If (X = y) Return 1 ; // It's a lie that you eat yourself.
If (A = B)
{
If (R [x] + 1 ) % 3 ! = R [y])
// R [x] + 1 = R [y] indicates that X eats Y. Of course, consider the case where R [x] = 2, R [y] = 0, therefore, modulo 3
Return 1 ;
}
Else {
F [B] =;
R [B] = (R [x]-R [y] + 4 ) % 3 ;
// The formula, how to push it, the amount, has not found a great explanation, however, combined with the previousArticleIs understandable.
}
}
Return 0 ;
}
Int Main ()
{
Int I, K, C, X, Y, sum =0 ;
Scanf ( " % D " , & N, & K );
For (I = 1 ; I <= N; I ++)
{
F [I] = I;
R [I] = 0 ;
}
For (I = 1 ; I <= K; I ++)
{
Scanf (" % D " , & C, & X, & Y );
If (Union (X, Y, c )) // If the return value is true, the value is accumulated.
Sum ++;
}
Printf ( " % D \ n " , Sum );
Return 0 ;
}

 

The above part explains from this link http://www.cppblog.com/abilitytao/archive/2010/05/14/98899.html

Hdu3047zjnu Stadium

N people are known to form a circle, and now M commands are given, such as A, B, C, that is, B is in the clockwise direction of

It is wrong to judge whether there are several commands in the middle.

Analysis: it is obvious that the query set is used to store the relative location of each person to the root node using an R [] array,

In this way, each command is given. If the two are in the same set, they can calculate whether they are consistent with the command based on their distance to the root node.

If it is not in the same set, it is merged and the relative distance between the root nodes of the Two sets is calculated. The relative distance between the other nodes is calculated during the identification process, this is similar to the previous question.

A bit ignored. Crazy wa, that is, the N people are circled, and mod300 is required for all relative distance calculation.

 

View code

# Include <iostream>
# Include <algorithm>
# Define Maxn 50000 + 10
Using Namespace STD;
Int F [maxn], R [maxn], n, m;
Void Init ()
{
For ( Int I = 0 ; I <= N; I ++)
{
F [I] = I;
R [I] = 0 ;
}
}
Int Find ( Int X)
{
If (X = f [x])
Return F [x];
Int T = find (F [x]);
R [x] = (R [x] + R [f [x]) % 300 ;
F [x] = T;
Return F [x];
}
Bool Union ( Int X, Int Y, Int W)
{
Int A = find (X );
Int B = find (y );
If (A = B)
{
If (R [y]-R [x] +300 ) % 300 = W)
Return True ;
Return False ;
}
F [B] =;
R [B] = (R [x] + w-R [y] + 300 ) % 300 ;
Return True ;
}
Int Main ()
{
Int A, B, C;
While (Scanf ( " % D " , & N, & M) = 2 )
{
Int Ans = 0 ;
Init ();
While (M --)
{
Scanf ( " % D " , & A, & B, & C );
If (! Union (a, B, c ))
Ans ++;
}
Printf ( " % D \ n " , ANS );
}
Return 0 ;
}


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.