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.
  • 相关阅读:
    MySQL--字符集参数
    MySQL--字符集基础
    Cassandra基础2
    Cassandra基础
    Cassandra -- Cassandra 3.0版本安装
    Cassandra Demo--Python操作cassandra
    MySQL--批量插入导致自增跳号问题
    MySQL Disk--SSD和HDD的性能
    MySQL Lock--并发插入导致的死锁
    TCL-视图
  • 原文地址:https://www.cnblogs.com/robbychan/p/3787211.html
Copyright © 2011-2022 走看看