标签:题目 lines from com pac eth tput ted ever
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 62172 | Accepted: 18837 |
Description
Input
Output
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("\n%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.\n");
else if(check_(a,b)||check_(a+n,b+n))
printf("In the same gang.\n");
else
printf("Not sure yet.\n");
}
}
}
return 0;
}
标签:题目 lines from com pac eth tput ted ever
原文地址:https://www.cnblogs.com/jaszzz/p/12831855.html