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)