zoukankan      html  css  js  c++  java
  • POJ 1703

    Find them, Catch them

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 62172   Accepted: 18837

    Description

    The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)

    Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:

    1. D [a] [b]
    where [a] and [b] are the numbers of two criminals, and they belong to different gangs.

    2. A [a] [b]
    where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.

    Input

    The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one message as described above.

    Output

    For each message "A [a] [b]" in each case, your program should give the judgment based on the information got before. The answers might be one of "In the same gang.", "In different gangs." and "Not sure yet."

    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.

    思路:一道简单的并查集题目,突破口是用一个 n*2 大小的数组来表示两人是否为同一集合;

    ac代码:
    // #include<bits/stdc++.h>
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <queue> //priority_queue
    #include <map>
    #include <set> //multiset set<int,greater<int>>大到小
    #include <vector> // vector<int>().swap(v);清空释放内存
    #include <stack>
    #include <cmath> // auto &Name : STLName  Name.
    #include <utility>
    #include <sstream>
    #include <string> //__builtin_popcount(ans);//获取某个数二进制位1的个数
    using namespace std;
    #define rep(i,a,n) 	for(int i=a;i<=n;i++)
    #define per(i,a,n) 	for(int i=n;i>=a;i--)
    #define read_a_int(x) 	scanf("%d",&x)
    #define Read(x,y) 		scanf("%d%d",&x,&y)
    typedef long long ll;
    
    const int INF = 0x3f3f3f3f;
    const int mod1e9 = 1000000007;
    const int mod998 = 998244353;
    const int mod = mod1e9;
    const int MAX_N = 100000 + 10;
    
    int par[MAX_N*2],ranks[MAX_N*2];
    
    void init(int n)
    {
    	for(int i=1;i<=n;i++){
    		par[i]=i;
    		ranks[i]=0;
    	}
    }
    
    int getRoot(int x)
    {
    	return par[x] == x ? x : par[x] = getRoot(par[x]);
    }
    
    void unite(int x,int y)
    {
    	x=getRoot(x);
    	y=getRoot(y);
    	if(x==y)	return;
    	
    	if(ranks[x]<ranks[y])
    		par[x]=y;
    	else{
    		par[y]=x;
    		if(ranks[x]==ranks[y])
    			ranks[x]++;
    	}
    }
    
    bool check_(int x,int y)
    {
    	return getRoot(x)==getRoot(y);
    }
    
    int main(void)
    {	
    	// ios::sync_with_stdio(false);
    	int t;
    	scanf("%d",&t);
    	while(t--){
    		int n,m;
    		scanf("%d%d",&n,&m);
    		
    		init(n*2);
    
    		char cmd;
    		int a,b;
    		for(int i=1;i<=m;i++){
    			scanf("
    %c%d%d",&cmd,&a,&b);
    			if(cmd=='D')
    			{
    				unite(a+n,b);
    				unite(a,b+n);
    			}else{
    				if(check_(a,b+n)||check_(a+n,b))
    					printf("In different gangs.
    ");
    				else if(check_(a,b)||check_(a+n,b+n))
    					printf("In the same gang.
    ");
    				else	
    					printf("Not sure yet.
    ");
    			}
    		}
    	}
    	
    
    	return 0;
    }
    

      


  • 相关阅读:
    20145202马超《网络对抗》Exp9*_* Web安全基础实践
    20145202马超《java》【课堂实验】P98测试
    20145202马超 2016-2017-2 《Java程序设计》第11周学习总结
    20145202马超《网络对抗》Exp8 Web基础
    20145202马超 2016-2017-2 《Java程序设计》第三次实验
    20145202马超 2016-2017-2 《Java程序设计》第10周学习总结
    20145202马超《网络对抗》Exp7 网络欺诈技术防范
    20145202马超 2016-2017-2 《Java程序设计》第二次实验
    20145202马超 2016-2017-2 《Java程序设计》第9周学习总结
    20145202马超《网络对抗》Exp6 信息搜集与漏洞扫描
  • 原文地址:https://www.cnblogs.com/jaszzz/p/12831855.html
Copyright © 2011-2022 走看看