【暑假】[实用数据结构]UVAlive 3644 X-Plosives

UVAlive X-Plosives

思路:

   “如果车上存在k个简单化合物,正好包含k种元素,那么他们将组成一个易爆的混合物”  如果将(a,b)看作一条边那么题意就是不能出现环,很容易联想到Kruskal算法中并查集的判环功能(新加入的边必须属于不同的两个集合否则出现环),因此本题可以用并查集实现。模拟装车过程即可。

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #define FOR(a,b,c) for(int a=(b);a<(c);a++)
 4 using namespace std;
 5 const int maxn= 100000 +10;
 6
 7 int p[maxn];
 8 int find_set(int u){ //寻找root+路径压缩
 9     return u==p[u]? u : p[u]=find_set(p[u]);
10 }
11 int main(){
12 int x,y,refusal;
13   while(scanf("%d",&x)==1){
14       refusal=0;   //直接用变量统计
15       FOR(i,0,maxn) p[i]=i;
16       while(x != -1){
17           scanf("%d",&y);
18           int xr=find_set(x),yr=find_set(y);
19           if(xr == yr) refusal ++;
20           else p[xr]=yr;
21           scanf("%d",&x);
22       }
23       printf("%d\n",refusal);
24   }
25   return 0;
26 }
时间: 2024-08-02 07:37:33

【暑假】[实用数据结构]UVAlive 3644 X-Plosives的相关文章

【暑假】[实用数据结构]UVAlive 3026 Period

UVAlive 3026 Period 题目: Period Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive),

【暑假】[实用数据结构]UVAlive 3942 Remember the Word

UVAlive 3942 Remember the Word 题目: Remember the Word Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Neal is very curious about combinatorial problems, and now here comes a problem about words. Know

【暑假】[实用数据结构]UVAlive 3135 Argus

UVAlive 3135 Argus Argus Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor data, Internet traffic, fin

【暑假】[实用数据结构]UVAlive 3027 Corporative Network

UVAlive 3027 Corporative Network 题目:   Corporative Network Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 3450   Accepted: 1259 Description A very big corporation is developing its corporative network. In the beginning each of the N ent

【暑假】[实用数据结构]UVAlive

题目:   Dominating Patterns Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description The archaeologists are going to decipher a very mysterious ``language". Now, they know many language patterns; each patt

[2016-03-19][UVALive][3644][X-Plosives]

时间:2016-03-19 12:26:47 星期六 题目编号:[2016-03-19][UVALive][3644][X-Plosives] 题目大意:n个物品如果含有n个元素,就会爆炸,会爆炸的话就不能放入仓库,问有多少个物品不能放入仓库 分析:每次加入一个物品,如果含有新的元素,那么元素的数目始终大于物品的数目1个,如果新的物品含有已经存在的元素,那么一定会爆炸 方法:并查集,已经加入的元素就不加 #include <cstdio> using namespace std; #defin

【暑假】[实用数据结构]KMP

KMP算法 KMP算法是字符串匹配算法,可以在O(n)的时间完成,算法包含两部分,分别是:构造适配函数与两串匹配. 失配边的使用大大提高了算法效率,可以理解为已经成功匹配的字符不在重新匹配,因为我们已经知道它是什么,对应到算法中 匹配失败后应该在最大前缀之后继续匹配,因为某后缀已与最大前缀匹配成功而不用重新比较. 以下为代码实现: 1 const int maxn = 1000 + 5; 2 3 void getFail(char* P,int* f){ //构造失配边 4 int n=strl

【暑假】[实用数据结构] AC自动机

Aho-Corasick自动机 AC自动机用于解决文本一个而模板有多个的问题. 作者所给模板如下: 1 struct AhoCorasickAutomata { 2 int ch[MAXNODE][SIGMA_SIZE]; 3 int f[MAXNODE]; // fail函数 4 int val[MAXNODE]; // 每个字符串的结尾结点都有一个非0的val 5 int last[MAXNODE]; // 输出链表的下一个结点 6 int cnt[MAXS]; 7 int sz; 8 9

【暑假】[实用数据结构]前缀树 Trie

前缀树Trie Trie可理解为一个能够快速插入与查询的集合,无论是插入还是查询所需时间都为O(m) 模板如下: 1 const int maxnode = 1000+10; 2 const int sigma_size = 26; 3 4 struct Trie{ 5 int ch[maxnode][sigma_size]; 6 int val[maxnode]; 7 int sz; 8 9 void clear(){ sz=1; memset(ch[0],0,sizeof(ch[0]));