【UER #5】万圣节的南瓜灯
2015-11-03 15:29
330 查看
题目链接:http://uoj.ac/contest/21/problem/142
题目大意:给你一个n*m的网格,最右边的一排方格和最左边的一排方格相连,有些方格被弄坏了,请问剩下的方格是不是一棵树
我想对出题人说:不看数据范围我们还是朋友……
uoj的题解真的写的非常好,虽然这次题目质量被狂喷,但是我感觉还不错的说……一定是因为我太弱了QAQ
对于50%的数据,可以相邻的格子建一条边,然后并查集合并,暴力判断……
这里要注意:不管合法的边和n*m-k-1相比不管是多还是少,只要不相等都不是一棵树
对于更大的数据怎么办呢?可以看到n,m巨大但是k巨小,所以直接判断如果n*m大于一个值,就视作不是一棵树,于是就……A……了………………
题目大意:给你一个n*m的网格,最右边的一排方格和最左边的一排方格相连,有些方格被弄坏了,请问剩下的方格是不是一棵树
我想对出题人说:不看数据范围我们还是朋友……
uoj的题解真的写的非常好,虽然这次题目质量被狂喷,但是我感觉还不错的说……一定是因为我太弱了QAQ
对于50%的数据,可以相邻的格子建一条边,然后并查集合并,暴力判断……
这里要注意:不管合法的边和n*m-k-1相比不管是多还是少,只要不相等都不是一棵树
对于更大的数据怎么办呢?可以看到n,m巨大但是k巨小,所以直接判断如果n*m大于一个值,就视作不是一棵树,于是就……A……了………………
[code]#include <iostream> #include <cstdio> #include <cstring> #define ll long long using namespace std; bool h[2333333]; int fa[2333333]; int d[2][2] = {{0,1},{1,0}}; int father(int x) { if(fa[x] != x) fa[x] = father(fa[x]); return fa[x]; } int main() { int N,n,m,k,x,y; for(scanf("%d",&N);N ;N --) { scanf("%d%d%d",&n,&m,&k); memset(h,0,sizeof(h)); ll nn = n; ll mm = m; bool fuck = 0; if (nn * mm > 400000ll) fuck = 1; for(int i = 1;i <= k;i ++) { scanf("%d%d",&x,&y); if(!fuck) { h[(x-1)*m + y] = 1; } } if(fuck) { cout << "No" << endl; continue ; } for(int i = 1;i <= n*m;i ++) fa[i] = i; int cnt = n*m-k-1; bool flag = 0; for(int i = 1;i <= n;i ++) { for(int j = 1;j <= m;j ++) { if(!h[(i-1)*m+j]) { for(int k = 0;k <= 1;k ++) { x = i + d[k][0]; y = j + d[k][1]; if(x > n) continue ; if(y == 0) y = m; if(y > m) y = 1; if(!h[(x-1)*m+y]) { int xx = father((x-1)*m+y),yy = father((i-1)*m+j); if(xx != yy) { fa[xx] = yy; cnt --; } else flag = 1; } } } } } if(flag || cnt) cout << "No" << endl; else cout << "Yes" << endl; } return 0; }
相关文章推荐
- UINavigationController层次关系
- Web Service单元测试工具实例介绍之SoapUI
- The project cannot be built until build path errors are resolved解决方案
- Longest Increasing Subsequence
- hdoj 3450 Counting Sequences 【离散化 + 树状数组优化dp】
- UISwipeGestureRecognizer手势识别
- UINavigationItem UINavigationBar 关系分析
- Ligerui表格基本操作(二)
- request.getRequestURL() getRequestURI区别
- UIButton (常用点击按钮)
- 关于安装WindowBuilder问题
- request.getSession(true)和request.getSession(false)的区别
- iOS9 系统分享调用之UIActivityViewController
- iOS开发日记42-UIScrollView与UITableview
- HBuilder Android平台离线打包支付插件配置
- EasyUI ComboBox应用示例
- 浅谈Looper handler Message MessageQueue bundle parcel parcelable
- 代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧
- Permutation Sequence
- 优化编译环境Optimizing a build environment (optional)