poj1703
来源:互联网 发布:网络会员制营销 编辑:程序博客网 时间:2024/04/30 14:06
#include <vector>#include <cstdio>#include <cstring>using namespace std;class UnionFindTree {public: UnionFindTree(int n) { rank = vector<int>(n, 0); par = vector<int>(n); for (int i = 0; i < n; i++) par[i] = i; } int find(int x) { if (x == par[x]) return x; else return par[x] = find(par[x]); } void unite(int x, int y) { int rx = find(x), ry = find(y); if (rank[rx] < rank[ry]) par[rx] = ry; else { par[ry] = rx; if (rank[rx] == rank[ry]) rank[rx]++; } } bool same(int x, int y) { return find(x) == find(y); }private: vector<int> par; vector<int> rank;};int main() { int T; scanf("%d", &T); for (int t = 0; t < T; t++) { int N, M; scanf("%d %d", &N, &M); UnionFindTree tree(N * 2); for (int i = 0; i < M; i++) { char type[2]; int a, b; scanf("%s %d %d", type, &a, &b); if (strcmp(type, "D") == 0) { tree.unite(a-1, N+(b-1)); tree.unite(N+(a-1), b-1); } else { if (tree.same(a-1, b-1)) { printf("In the same gang.\n"); } else if (tree.same(a-1, N+(b-1))) { printf("In different gangs.\n"); } else { printf("Not sure yet.\n"); } } } }}
0 0
- poj1703
- POJ1703
- POJ1703
- poj1703
- poj1703
- poj1703
- poj1703
- poj1703
- poj1703
- poj1703
- poj1703
- poj1703
- POj1703
- poj1703 题解
- Poj1703并查集
- poj1703 并查集
- poj1703解题报告
- poj1703 并查集
- 6个变态的C语言Hello World程序 之 雷人的程序语言
- Unable to compile class for JSP 问题解决方法
- [编程之美] PSet2.13 子数组的最大乘积
- Hybrid--WebView中使用Ajax
- OC:打僵尸问题(类的问题)
- poj1703
- Highcharts选项配置详细说明文档
- hdu1251 字典树
- Android BaiduMap 定位到指定坐标
- 单例的灾难
- 【黑马程序员】Java学习技术博客——银行业务调度系统
- 从“O2O演唱会”中得到的启示
- 封了博客
- 关于java中的StringBuilder的线程安全问题