食物链(POJ-1182)
来源:互联网 发布:java实现九九乘法表 编辑:程序博客网 时间:2024/05/16 16:22
经典并查集的使用。
并查集可以高效的维护集合之间的关系。还可以判断图的连通性。
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>using namespace std;const int max_n = 50005*3;const int max_k = 100005;int N,K,par[max_n],rankk[max_n];int T[max_k],X[max_k],Y[max_k];void init(int n) { for(int i=0;i<n;i++) { par[i] = i; rankk[i] = 0; }}int findd(int x) { return par[x] == x ? x : par[x] = findd(par[x]);}bool same(int a,int b) { return findd(a) == findd(b);}void unite(int x,int y) { x = findd(x); y = findd(y); if(x==y) return ; if(rankk[x]<rankk[y]) { par[x] = y; } else { par[y] = x; if(rankk[x] == rankk[y]) rankk[x]++; }}void solve() { init(N*3); int ans = 0; for(int i=0;i<K;i++) { int t = T[i]; int x = X[i] - 1,y = Y[i] - 1; if(x<0||N<=x||y<0||N<=y) { ans++; continue; } if(t == 1) { if(same(x,y+N)||same(x,y+2*N)) { ans++; } else { unite(x,y); unite(x+N,y+N); unite(x+N*2,y+N*2); } } else { if(same(x,y) || same(x,y+2*N)) { ans++; } else { unite(x,y+N); unite(x+N,y+2*N); unite(x+2*N,y); } } } printf("%d\n",ans);}int main() { scanf("%d%d",&N,&K); for(int i=0;i<K;i++) { scanf("%d",&T[i]); scanf("%d%d",&X[i],&Y[i]); } solve(); return 0;}
0 0
- poj 1182 食物链
- poj 1182 食物链
- poj 1182 食物链(转)
- poj 1182 食物链
- poj 1182 食物链
- poj 1182 食物链
- POJ-1182 食物链
- POJ 1182 食物链
- poj 1182食物链
- POJ 1182 食物链
- POJ 1182 食物链
- poj 1182 食物链
- POJ 1182 食物链
- POJ 1182 食物链
- poj 1182 食物链
- POJ 1182 - 食物链
- poj 1182 食物链
- poj 1182 食物链
- android之官方下拉刷新组件SwipeRefreshLayout
- VS2013常用快捷键
- XUtil学习之DBUtil(八)
- POJ 1088 滑雪 动态规划
- Linux网络接口配置文件ifcfg-eth0解析
- 食物链(POJ-1182)
- eclipseLink中TENANT实现
- 破解SQLSERVER存储过程的加密
- linux 根文件系统
- 指定edittext的行数
- unity ios渠道批量打包以及需要注意的两个小点
- android开源库----ViewPagerTransforms
- 如何做好回归测试
- Android WebView (1)清除缓存