CUIT-ACM 2017-01-16 新生訓(xùn)練賽 F - The Suspects

Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.

Input
The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n?1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.

Output
For each case, output the number of suspects in one line.

Sample Input
100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0

Sample Output
4
1
1

運(yùn)用并查集的方法
把數(shù)據(jù)往零上并蛾狗,初始為負(fù)一

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

int f[30005];

int find(int x)
{
    if (f[x]<0)return x;
    return find(f[x]);
} 

void Union(int x,int y)
{
    int a=find(x);
    int b=find(y);
    if(a == b)  
        return;//相等退出又活,否則超時(shí)
    if(f[a] > f[b])
    {
        f[b] += f[a];
        f[a]=b;
    }
    else
    {
        f[a] += f[b];  
                f[b] = a;
    }
}

int main()
{
    int m,n,c,p,q;
    while(~scanf("%d %d",&m,&n) && (n || m))
    {
        memset(f,-1,sizeof(f));
        while(n--)
        {
            scanf("%d%d",&c,&p);
            for(int i=1;i<c;i++)
            {
                scanf("%d",&q);
                Union(p,q);
            }
        }
        printf("%d\n",-f[find(0)]);
    } 
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市萎攒,隨后出現(xiàn)的幾起案子勒极,更是在濱河造成了極大的恐慌是掰,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,080評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件辱匿,死亡現(xiàn)場(chǎng)離奇詭異键痛,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)匾七,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,422評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門絮短,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人昨忆,你說我怎么就攤上這事丁频。” “怎么了邑贴?”我有些...
    開封第一講書人閱讀 157,630評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵席里,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我拢驾,道長(zhǎng)奖磁,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,554評(píng)論 1 284
  • 正文 為了忘掉前任独旷,我火速辦了婚禮署穗,結(jié)果婚禮上寥裂,老公的妹妹穿的比我還像新娘嵌洼。我一直安慰自己案疲,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,662評(píng)論 6 386
  • 文/花漫 我一把揭開白布麻养。 她就那樣靜靜地躺著褐啡,像睡著了一般鳖昌。 火紅的嫁衣襯著肌膚如雪备畦。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,856評(píng)論 1 290
  • 那天懂盐,我揣著相機(jī)與錄音莉恼,去河邊找鬼俐银。 笑死端仰,一個(gè)胖子當(dāng)著我的面吹牛捶惜,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播荔烧,決...
    沈念sama閱讀 39,014評(píng)論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼吱七,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了鹤竭?” 一聲冷哼從身側(cè)響起陪捷,我...
    開封第一講書人閱讀 37,752評(píng)論 0 268
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎诺擅,沒想到半個(gè)月后市袖,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,212評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡烁涌,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,541評(píng)論 2 327
  • 正文 我和宋清朗相戀三年苍碟,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片撮执。...
    茶點(diǎn)故事閱讀 38,687評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡微峰,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出抒钱,到底是詐尸還是另有隱情蜓肆,我是刑警寧澤颜凯,帶...
    沈念sama閱讀 34,347評(píng)論 4 331
  • 正文 年R本政府宣布,位于F島的核電站仗扬,受9級(jí)特大地震影響症概,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜早芭,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,973評(píng)論 3 315
  • 文/蒙蒙 一彼城、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧退个,春花似錦募壕、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,777評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至刀荒,卻和暖如春代嗤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背照棋。 一陣腳步聲響...
    開封第一講書人閱讀 32,006評(píng)論 1 266
  • 我被黑心中介騙來泰國(guó)打工资溃, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人烈炭。 一個(gè)月前我還...
    沈念sama閱讀 46,406評(píng)論 2 360
  • 正文 我出身青樓溶锭,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親符隙。 傳聞我的和親對(duì)象是個(gè)殘疾皇子趴捅,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,576評(píng)論 2 349

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

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,437評(píng)論 0 23
  • 秋夜等候 /深山老林(千年桃妖) 野地 秋風(fēng)低嚎 簡(jiǎn)易的木頭 塑料布帳篷 在顫栗動(dòng)蕩 雨點(diǎn)兒零碎滴嗒 燭火欲滅又...
    深山老林千年桃妖閱讀 231評(píng)論 1 2
  • 損失規(guī)避 「概念理解」 心理上感受到等量失去的痛苦大于等量收獲的喜悅 「商業(yè)技巧」 1、換購(gòu)替代打折霹疫;2拱绑、"獲得框...
    安嶺雪豬閱讀 527評(píng)論 0 2
  • 一:民性自古素愛抄襲? 我這個(gè)“抄襲”自然是要打引號(hào)的丽蝎,相比與不愛抄襲的西方人猎拨,中國(guó)文明在歷史的發(fā)展歷程上明顯優(yōu)勢(shì)...
    Ludi_Fly閱讀 348評(píng)論 0 2
  • 2013年-8月-8日 金天,高二屠阻,156cm 82斤 巴掌小臉小嘴巴 單純簡(jiǎn)單污穢 “我恨我自己” (不定時(shí)更新)
    繞音閱讀 349評(píng)論 0 0