#include <iostream> #include <cstring> #include <cstdio> #include <string> #include < algorithm> #include <vector> #include <map>using namespace std; #define MAXN (1<<5) +5#define inf 0x3f3f3f3fint fa[maxn];void inital () {for (int i=0;i<maxn;i++) { fa[i]=i; }} int finds (int x) { if (fa[x]==x) return fa[x]; else return Fa[x]=finds (Fa[x]);} BOOL Unit (int x,int y) { int a=finds (x); int b=finds (y); if (a==b) return false; else{ fa[a]=b; } return true;} int main () { int u,v; Freopen ("In.txt", "R", stdin); while (~SCANF ("%d%d", &u,&v)) { int sum=0; Inital (); Unit (U,V); while (~SCANF ("%d", &u)!=eof&&u!=-1) { scanf ("%d", &v); if (unit (u,v) ==0) sum++; } printf ("%d\n", sum);} }
3644x-plosivesa Secret Service developed a new kind of explosive that attain it volatile property only when a Specificass Ociation of products occurs. Each product is a mix of the different simple compounds, to which wecall a binding pair. If n > 2, then mixing n different binding pairs containing n simple compoundscreates a powerful explosive. For example, the binding pairs a+b, B+c, A+c (three pairs, threecompounds) result in a explosive, while A+b, B+c, a+d (th REE pairs, four compounds) does not. You aren't a secret agent but only a guy in a delivery agency with one dangerous problem:receivebinding pairs in sequent Ial order and place them in a cargo ship. However, you must avoid placing inthe same, a explosive association. So, after placing a set of pairs, if you receive one pair thatmight produce an explosion with some of the pairs already in Stock, you must refuse it, otherwise, youmust 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 thefollowing explosive with T He 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. Inputthe input would contain several test cases, each of them as described below. Consecutivetest 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 both integers (each integer lies between 0 and +) separated bya single space, rep Resenting a binding pair. Each test case is ends in a and the number '-1 '. You may assume this no repeated binding pairsappears in the input. Outputfor Each test case, the output must follow the description below. A single line with the number of refusals. Sample INPUT1-5-1sample OUTPUT3
In fact, and check the set of the ring, determine the total number of rings, can also be implemented with DFS
uva3644 x-plosives