LA-3644-X-Plosives 解题报告
2014-10-11 14:39
218 查看
简单并查集题。题意:现在有一些都是只由两种元素组成的化合物,你把它们按顺序装进货车里。但是,考虑到一个安全隐患:如果车上有K个化合物(K大于2),这K个化合物刚好包含K种元素,那么它们就会组成一个易爆的混合物。为了安全起见,每当你拿到一个化合物准备装车时,你就要判断它是否会和已经装车的化合物构成易爆的混合物,如果是,那么这个化合物就不装车。请你最后输出有多少个化合物没有装车。
我的解题思路:K个化合物刚好包含K种元素且每个化合物只含有两种元素的情况下只能说明假设每个元素看成一个点,每个化合物看成一条边,那么这K个化合物刚好构成一个环,由K个点和K条边构成的环。所以判断一个化合物装车后会不会形成易爆混合物只需要判断会不会构成一个环即可。不用多说了,并查集解题。
我的解题代码:
我的解题思路:K个化合物刚好包含K种元素且每个化合物只含有两种元素的情况下只能说明假设每个元素看成一个点,每个化合物看成一条边,那么这K个化合物刚好构成一个环,由K个点和K条边构成的环。所以判断一个化合物装车后会不会形成易爆混合物只需要判断会不会构成一个环即可。不用多说了,并查集解题。
我的解题代码:
#include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <algorithm> #include <queue> #include <stack> #include <list> using namespace std; #define N 100001 int father ; int ans; void Init(); int Find(int x); void Union(int x, int y); int main() { int a, b; Init(); while (~scanf("%d", &a)) { if (a == -1) { printf("%d\n", ans); Init(); continue; } scanf("%d", &b); if (Find(a) != Find(b)) Union(a, b); else ans++; } return 0; } void Init() { ans = 0; for (int i=0; i<N; ++i) father[i] = i; return; } int Find(int x) { int z, y = father[x]; while (y != father[y]) { y = father[y]; } while (x != father[x]) { z = father[x]; father[x] = y; x = z; } return y; } void Union(int x, int y) { int fx = Find(x); int fy = Find(y); father[fx] = fy; return; }
相关文章推荐
- Live Archive 3644 X-Plosives 解题报告
- LA 3644 X-Plosives
- LA 3644 X-Plosives 易爆物
- LA 3644 X-Plosives
- LA -3644 X-Plosives -并查集
- LA 3485 自适应Simpson积分 解题报告
- LA-3938-"Ray, Pass me the dishes!" 解题报告
- X-Plosives问题, LA 3644, 并查集算法
- (DS 《算法竞赛入门经典》)LA 3644 X-Plosives(并查集)
- 并查集(图论) LA 3644 X-Plosives
- LA-3415 & POJ-2771 Guardian of Decency 解题报告
- LA-2523 & POJ-1325 & HDOJ-1150 Machine Schedule 解题报告
- LA 3644 X-Plosives (并查集)
- LA 3644 X-Plosives
- LA 3644 - X-Plosives,并查集
- LA 3644 - X-Plosives ( 也即UVA 1160)
- LA 6856 Circle of digits 解题报告
- LA 3644 X-Plosives
- LA - 3644 - X-Plosives
- LA 3644 X-Plosives [并查集]