Original question:
A Secret Service developed a new kind of explosive that attain it volatile property only when a specific association of P Roducts occurs. Each product is a mix of the different simple compounds, to which we call a binding pair. If n > 2, then mixing n different binding pairs containing n simple compounds creates a powerful explosive. For example, the binding of pairs a+b, B+c, A+c (three pairs, three compounds) result in a explosive, while A+b, B+c, A+d (t Hree pairs, four compounds) does not. You aren't a secret agent but only a guy in a delivery agency with one dangerous problem:receive binding pairs in Sequen Tial order and place them in a cargo ship. However, must avoid placing in
The same is a explosive association. So, after placing a set of pairs, if you receive one pair this might produce an explosion with some of the pairs already I N-Stock, you must refuse it, otherwise, you must accept it. An example. Lets assume you receive the following sequence:a+b, G+b, D+f, A+e, E+g, F+h. You would accept the first four pairs and then refuse e+g since it would is possible to make the following explosive with The previous pairs:a+b, G+b, A+e, e+g (4 pairs with 4 simple compounds). Finally, you would accept the last pair, f+h. Compute the number of refusals given a sequence of binding pairs.
Input
The input would contain several test cases, each of the them as described below. Consecutive
Test cases is separated by a single blank line. Instead of letters we'll use integers to represent compounds. The input contains several lines. Each line (except) consists of the integers (each integer lies between 0 and 5) separated by a single space, Representing a binding pair. Each test case is ends in a and the number '-1 '. You may assume this no repeated binding pairs appears in the input.
Output
For each test case, the output must follow the description below. A single line with the number of refusals.
Sample Input
1 2
3 4
3 5
3 1
2 3
4 1
2 6
6 5
-1
Sample Output
3
English:
Give you a few groups of a and B, indicating that A and B will form a compound, and if there are more than 3 items forming a compound, it will explode. Such items can not be loaded into the car, if you are given the order to load these items in the car, how many groups can not be loaded into the car.
#include <bits/stdc++.h>
using namespace std;
int father[100001];
int Find (int x)
{
if (father[x]==x)
return x;
Return Father[x]=find (Father[x]);
}
int main ()
{
Ios::sync_with_stdio (false);
int a,b,ans=0;
for (int i=1;i<=100000;i++)
father[i]=i;
while (Cin>>a)
{
if (a!=-1)
{
cin>>b;
int X=find (a);
int Y=find (b);
if (x==y)
ans++;
else
father[x]=y;
}
else
{
cout<<ans<<endl;
ans=0;
for (int i=1;i<=100000;i++)
father[i]=i;
}
}
return 0;
}
Solution:
The naked and check set judgment ring can be.