zoukankan      html  css  js  c++  java
  • 并查集 示例 : poj 1703 [Find them, Catch them 帮派之争]

    #include <iostream>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <vector>
    #include <stack>
    #include <deque>
    #include <queue>
    #include <bitset>
    #include <list>
    #include <map>
    #include <set>
    #include <iterator>
    #include <algorithm>
    #include <functional>
    #include <utility>
    #include <sstream>
    #include <climits>
    #include <cassert>
    #define MID(x,y) ( ( x + y ) >> 1 )
    #define L(x) ( x << 1 )
    #define R(x) ( x << 1 | 1 )
    #define BUG puts("here!!!");
    #define STOP system("pause");
    
    using namespace std;
    const int N = 100005;
    int f[N+N];
    int n, m;
    int find(int x) {
    	if(f[x] < 0) return x;
    	return f[x] = find(f[x]);
    }
    int main() {
    	int loop;
    	cin >> loop;
    	while(loop--) {
    		scanf("%d%d", &n, &m);
    		memset(f, 255, sizeof(f));
    		while(m--) {
    			int a, b;
    			char s[3];
    			scanf("%s%d%d", s, &a, &b);
    			if(s[0] == 'A') {
    				if(find(a) != find(b) && find(a) != find(b+n)) {
    					printf("Not sure yet.\n");
    				}
    				else if(find(a) == find(b)) {
    					printf("In the same gang.\n");
    				}
    				else printf("In different gangs.\n");
    			}
    			else {
    				if(find(a) != find(b+n)) {
    					f[find(a)] = find(b+n);
    					f[find(b)] = find(a+n);
    				}
    			}
    		}
    	}
    	return 0;
    }
    Sample Input
    1
    5 5
    A 1 2
    D 1 2
    A 1 2
    D 2 4
    A 1 4
    Sample Output
    Not sure yet.
    In different gangs.
    In the same gang.
  • 相关阅读:
    memcached+狀態模式+工廠方法使用
    狀態模式
    UML类图
    Quartz.NET
    第四次作业---计算器的第二步
    做 fzu oj 1106 题目学到的
    做fzu oj 1045 做减法学到的sprintf()函数
    第三次补作业
    第三次作业随笔(new)包含了补作业
    远征系列---离港篇(学杂记)
  • 原文地址:https://www.cnblogs.com/robbychan/p/3787211.html
Copyright © 2011-2022 走看看