Codeforces Round #134 (Div. 2) 并查集
2012-08-18 21:40
357 查看
1.阅读题,读明白题就能过。
2.贪心题,简单想一想就能过。
3.并查集,抽象出来图论模型就能过,抽象不出来,累死你。
按照《图论算法理论、实现及应用》书上的模板敲,RE在第48组,现在也不知道为什么。
于是敲常规的并查集:
发现飞哥的神并查集+优化模板:
2.贪心题,简单想一想就能过。
3.并查集,抽象出来图论模型就能过,抽象不出来,累死你。
按照《图论算法理论、实现及应用》书上的模板敲,RE在第48组,现在也不知道为什么。
于是敲常规的并查集:
#include <iostream> #include <stdio.h> using namespace std; struct P{ int x,y; }p[100000]; int parent[100000]; int n; int find(int x){ return x==parent[x] ? x : parent[x]=find(parent[x]); } int main(){ while(scanf("%d",&n)!=EOF){ for(int i=0;i<n;i++){ cin >> p[i].x >> p[i].y; } for(int i=0;i<n;i++){ parent[i]=i; } for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if(p[i].x==p[j].x || p[i].y==p[j].y){ if(find(i)==find(j)) continue; else parent[find(i)]=find(j); } } } int cou=0; for(int i=0;i<n;i++){ if(parent[i]==i) cou++; } printf("%d\n",cou-1); } return 0; }
发现飞哥的神并查集+优化模板:
int find(int x){ return x==parent[x] ? x : parent[x]=find(parent[x]); }
for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if(p[i].x==p[j].x || p[i].y==p[j].y){ if(find(i)==find(j)) continue; else parent[parent[i]]=j; } } }
相关文章推荐
- 并查集Codeforces Round #134 (Div. 1), problem: (A) Ice Skating
- Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 开关--并查集
- Codeforces Round #250 (Div. 1) B 并查集
- 【2-SAT】【并查集】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集
- Codeforces Round #268 (Div. 2) D Two Sets[并查集]
- Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 开关--并查集
- Codeforces Round #134 (Div. 2)A. Mountain Scenery
- Codeforces Round #345 (Div. 2) E. Table Compression 并查集
- Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 开关--并查集
- Codeforces Round #134 (Div. 2)B. Airport
- Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集
- Codeforces Round #181 (Div. 2) B. Coach 带权并查集
- Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 开关--并查集
- Codeforces Round #134 (Div. 2)C. Ice Skating
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C 并查集
- Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 开关--并查集
- Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 开关--并查集
- Educational Codeforces Round 33 (Rated for Div. 2)【C】【并查集】
- Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary【带权并查集】