(DS "Algorithmic Competition Primer Classic") LA 3644 x-plosives (and collection)

Source: Internet
Author: User

Problem Solving Ideas:

and check Set



A Secret Service developed a new kind of explosive that attain it volatile property only when a specific
Association of products occurs. Each product was a mix of 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 pairs a+b, B+c, A+c (three pairs, three
Compounds) result in a explosive, while A+b, B+c, a+d (three 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 sequential 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 that
Might produce an explosion with some of the pairs already in 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 both integers (each integer lies between 0 and 105
) separated by
A single space, representing a binding pair.
Each test case is ends in a and the number '-1 '. Assume that 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


The code is as follows:

/* * LA3644.cpp * *  Created on:2014 December 30 *      author:administrator * * #include <iostream> #include <cstdi O> #include <cstring>using namespace std;const int maxn = 100000;int Father[maxn + 10];int Find (int x) {if (x = = Fat Her[x]) {return x;} return father[x] = find (Father[x]);} int main () {int x,y;while (scanf ("%d", &x) = = 1) {int result = 0;int i;for (i = 0; I <= maxn; ++i) {father[i] = i;} while (x! =-1) {scanf ("%d", &y), int fx = FIND (x), int fy = find (y), if (FX! = FY) {//join (fx,fy); father[fy] = FX;} else{result++;} scanf ("%d", &x);} printf ("%d\n", result);} return 0;}




(DS "Algorithmic Competition Primer Classic") LA 3644 x-plosives (and collection)

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.