LA 3644 X-Plosives

最简单的并查集

多做做水题,加深一下理解

 1 //#define LOCAL
 2 #include <cstdio>
 3
 4 const int maxn = 100000 + 10;
 5 int parent[maxn];
 6
 7 int GetParent(int a) { return parent[a] == a ? a : parent[a] = GetParent(parent[a]); }
 8
 9 int main(void)
10 {
11     #ifdef LOCAL
12         freopen("3644in.txt", "r", stdin);
13     #endif
14
15     int x, y;
16     while(scanf("%d", &x) == 1)
17     {
18         for(int i = 0; i < maxn; ++i)    parent[i] = i;
19         int cnt = 0;
20         while(~x)
21         {
22             scanf("%d", &y);
23             x = GetParent(x); y = GetParent(y);
24             if(x == y)    ++cnt;
25             else        parent[x] = y;
26             scanf("%d", &x);
27         }
28         printf("%d\n", cnt);
29     }
30     return 0;
31 }

代码君

时间: 2024-10-05 03:51:03

LA 3644 X-Plosives的相关文章

[LA] 3644 - X-Plosives [并查集]

A secret service developed a new kind of explosive that attain its volatile property only when a specicassociation of products occurs. Each product is a mix of two di?erent simple compounds, to which wecall a binding pair. If N > 2, then mixing N di?

(DS 《算法竞赛入门经典》)LA 3644 X-Plosives(并查集)

解题思路: 并查集 A secret service developed a new kind of explosive that attain its volatile property only when a specificassociation of products occurs. Each product is a mix of two different simple compounds, to which wecall a binding pair. If N > 2, then

并查集(图论) LA 3644 X-Plosives

题目传送门 题意:训练指南P191 分析:本题特殊,n个物品,n种元素则会爆炸,可以转移到图论里的n个点,连一条边表示u,v元素放在一起,如果不出现环,一定是n点,n-1条边,所以如果两个元素在同一个集合就会爆炸. #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; struct DSU { int rt[N], rk[N]; void init(void) { memset (rt, -1, sizeof

[并查集]LA 3644 X-Plosives

#include <cstdio> #include <cstring> const int INF=0x3f3f3f3f; const int MAXN=1e5+10; int parent[MAXN]; int findset(int a){ return parent[a]!=a ? parent[a] = find(parent[a]) :a ; } int main() { int a,b,ans=0,x,y; while(scanf("%d",&am

LA 3644 易爆物

https://vjudge.net/problem/UVALive-3644 简单的并查集题目. 1 #include<iostream> 2 using namespace std; 3 4 const int maxn = 100000 + 5; 5 6 int p[maxn]; 7 8 int find(int x) 9 { 10 return p[x] != x ? p[x] = (find(p[x])) : x; 11 } 12 13 int main() 14 { 15 //fr

3644 - X-Plosives(水题,并差集)

3644 - X-Plosives A secret service developed a new kind of explosive that attain its volatile property only when a specificassociation of products occurs. Each product is a mix of two different simple compounds, to which wecall a binding pair. If N >

hdu 5745 la vie en rose

这道题的官方题解是dp,但是可以暴力出来.改天再研究怎么dp. 暴力的时候,如果计算sum的时候,调用strlen函数会超时,可见这个函数并不是十分的好.以后能不用尽量不用. La Vie en rose Time Limit: 14000/7000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 861    Accepted Submission(s): 461 Problem

让MAC OS也能使用LL LA L等LS的别名

linux下默认ll是ls -l的别名.OS X下默认不支持.习惯了linux下使用ll,我们同样也可以将习惯搬到os x下的shell中. 再当前用户家目录下新建.bash_profile文件.根据你的习惯,添加下面格式内容即可. 1 2 3 alias ll='ls -l' alias la='ls -a' alias l='ls -la' 然后执行:source .bash_profile你还可以添加你喜欢的其他别名.

ZOJ 3644 Kitty&#39;s Game (图上DP 约数)

哎-这一场就做了三个题目,全队倒数第一,简直是太弱了. A Kitty's Game (ZOJ 3644) 题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3644 题意: 给出一个有向图,每个节点有一个权值pi, 有一个人从1节点出发(其权值为1节点的权值),前往n号节点,每经过一个节点,他的权值就变成了他经过这个节点前的权值和这个节点权值的最小公倍数,如果他经过这个节点后权值不发生变化则他就不能经过这个节点