POJ 1703 - Find them, Catch them

POJ 1703 - Find them, Catch them
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.

題意:一個(gè)城市有兩個(gè)幫派酸些,現(xiàn)在公安局掌握了一些資料肛炮,但是只知道A,B兩個(gè)屬于不同的幫派。要你根據(jù)所給的信息判別任意兩個(gè)人的關(guān)系,是屬于同一個(gè)幫派還是屬于不同的幫派咽笼,還是他們的關(guān)系不能確定了?

解題思路:
食物鏈的簡化版
A x y 不能確定xy是否是同一幫派
D x y 確定xy屬于不同幫派
x表示幫派A x+n表示幫派B

#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
const int N = 100000;
const int MAX = 2*N+16;

int fa[MAX], deep[MAX];

void init()
{
    memset(fa, -1, sizeof(fa));
    memset(deep, 0, sizeof(deep));
}

int find(int x)
{
    if (fa[x] == -1) return x;
    return fa[x] = find(fa[x]);
}

void unite(int x, int y)
{
    x = find(x), y = find(y);
    if (x == y) return;
    if (deep[x]<deep[y])
        fa[x] = y;
    else
    {
        fa[y] = x;
        if (deep[x] == deep[y])
            deep[x]++;
    }
}

bool same(int x, int y)
{
    return find(x) == find(y);
}


int main()
{
    int T;
    scanf("%d", &T);
    while (T--) {
    int n, m;
    int x, y;
    char c[2];//這里如果定義char c 會出現(xiàn)Runtime Error
        if (T >= 20) break;
        scanf("%d%d", &n, &m);
        init();
        //cin>>n>>m;
        if (n>N || m>N) break;
        while (m--) {
            scanf("%s%d%d", c, &x, &y);
            //cin >> c >> x >> y;
            if (c[0] == 'D')
            {
                unite(x, y + n);
                unite(x + n, y);
            }
            else {
                if (same(x, y) || same(x + n, y + n))
                {
                    printf("In the same gang.\n");
                    continue;
                }
                if (same(x, y + n) || same(x + n, y))
                {
                    printf("In different gangs.\n");
                    continue;
                }
                printf("Not sure yet.\n");
            }
        }

    }
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末躲查,一起剝皮案震驚了整個(gè)濱河市酌心,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌蚤霞,老刑警劉巖酗失,帶你破解...
    沈念sama閱讀 221,406評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異昧绣,居然都是意外死亡规肴,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,395評論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來拖刃,“玉大人删壮,你說我怎么就攤上這事《夷担” “怎么了央碟?”我有些...
    開封第一講書人閱讀 167,815評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長发绢。 經(jīng)常有香客問我硬耍,道長,這世上最難降的妖魔是什么边酒? 我笑而不...
    開封第一講書人閱讀 59,537評論 1 296
  • 正文 為了忘掉前任经柴,我火速辦了婚禮,結(jié)果婚禮上墩朦,老公的妹妹穿的比我還像新娘坯认。我一直安慰自己,他們只是感情好氓涣,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,536評論 6 397
  • 文/花漫 我一把揭開白布牛哺。 她就那樣靜靜地躺著,像睡著了一般劳吠。 火紅的嫁衣襯著肌膚如雪引润。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,184評論 1 308
  • 那天痒玩,我揣著相機(jī)與錄音淳附,去河邊找鬼。 笑死蠢古,一個(gè)胖子當(dāng)著我的面吹牛奴曙,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播草讶,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼洽糟,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了堕战?” 一聲冷哼從身側(cè)響起坤溃,我...
    開封第一講書人閱讀 39,668評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎嘱丢,沒想到半個(gè)月后浇雹,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,212評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡屿讽,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,299評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片伐谈。...
    茶點(diǎn)故事閱讀 40,438評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡烂完,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出诵棵,到底是詐尸還是另有隱情抠蚣,我是刑警寧澤,帶...
    沈念sama閱讀 36,128評論 5 349
  • 正文 年R本政府宣布履澳,位于F島的核電站嘶窄,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏距贷。R本人自食惡果不足惜柄冲,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,807評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望忠蝗。 院中可真熱鬧现横,春花似錦、人聲如沸阁最。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,279評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽速种。三九已至姜盈,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間配阵,已是汗流浹背馏颂。 一陣腳步聲響...
    開封第一講書人閱讀 33,395評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留闸餐,地道東北人饱亮。 一個(gè)月前我還...
    沈念sama閱讀 48,827評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像舍沙,于是被迫代替她去往敵國和親近上。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,446評論 2 359

推薦閱讀更多精彩內(nèi)容