Ultraviolet A 1160-X-plosives (query set)

Source: Internet
Author: User

Link: Ultraviolet A 1160-X-plosives

Each compound is composed of two simple elements. Now there are N compounds to be loaded. If it appears, K compounds are just composed of K tuples, A chemical issue occurs. During each loading, the worker will check whether the issue may occur. If yes, the worker will give up loading. The last few compounds were not loaded.

Solution: Check the set and regard each tuples as a node and a compound as an edge. If an edge is added to form a ring, it cannot be added. Just count the number of sides that are not added.

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 1e5;int f[maxn+5];int getfar(int x) {    return x == f[x] ? x : f[x] = getfar(f[x]);}int main () {    int x, y;    while (scanf("%d", &x) == 1) {        int ret = 0;        for (int i = 0; i <= maxn; i++)            f[i] = i;        while (x != -1) {            scanf("%d", &y);            x = getfar(x);            y = getfar(y);            if (x == y)                ret++;            else                f[y] = x;            scanf("%d", &x);        }        printf("%d\n", ret);    }    return 0;}

Ultraviolet A 1160-X-plosives (query set)

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.