【暑假】[实用数据结构]UVAlive 3644 X-Plosives
来源:互联网 发布:淘宝客域名注册 编辑:程序博客网 时间:2024/06/05 14:28
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 }
0 0
- 【暑假】[实用数据结构]UVAlive 3644 X-Plosives
- UVALive 3644 X-Plosives
- UVALive 3644 X-Plosives
- UVALive 3644 X-Plosives
- UVALive - 3644 X-Plosives
- UVALive 3644 X-Plosives
- UVALive - 3644 X-Plosives
- uvalive 3644 X-Plosives
- UVALive 3644 X-Plosives 并查集
- UVALive - 3644 X-Plosives 并查集
- UVALive - 3644 X-Plosives 并查集
- 【暑假】[实用数据结构]UVAlive 3135 Argus
- 【暑假】[实用数据结构]UVAlive 3027 Corporative Network
- 【暑假】[实用数据结构]UVAlive 4329 Ping pong
- 【暑假】[实用数据结构]UVAlive 3026 Period
- 【暑假】[实用数据结构]UVAlive 4670 Dominating Patterns
- UVALive - 3644X-Plosives(并查集)
- UVALive - 3644 - X-Plosives (并查集!!)
- 【暑假】[实用数据结构]UVa11995 I Can Guess the Data Structure!
- 【暑假】[实用数据结构]UVa11991 Easy Problem from Rujia Liu?
- 【暑假】[实用数据结构]UVAlive 3135 Argus
- 【暑假】[实用数据结构]UVa11997 K Smallest Sums
- Qt下载地址
- 【暑假】[实用数据结构]UVAlive 3644 X-Plosives
- Error:Cause: peer not authenticated
- 【暑假】[实用数据结构]UVAlive 3027 Corporative Network
- 【暑假】[实用数据结构]UVAlive 4329 Ping pong
- 【暑假】[实用数据结构]UVa11235 Frequent values
- 阿里二面(3.28)
- Centos查看端口占用情况和开启端口命令
- 【暑假】[实用数据结构]前缀树 Trie
- 【暑假】[实用数据结构]KMP