The Suspects——Asia Kaohsiung 2003

来源:https://www.cnblogs.com/chiweiming/archive/2018/07/18/9320855.html
-Advertisement-
Play Games

原創 The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 48698 Accepted: 23286 Description Severe acute respiratory syndrome (SARS), ...


原創 


The Suspects

Time Limit: 1000MS  Memory Limit: 20000K

Total Submissions: 48698  Accepted: 23286

Description

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

Source

Asia Kaohsiung 2003   此題出自POJ——1611:http://poj.org/problem?id=1611
  題目大意:   學校要找出患了SARS一共有多少人,n個學生人數(編號0~n-1),m個協會,每個協會有k個人,並給出這k個人的編號。 每個學生可以參加多個協會,如果一個協會裡面存在1個或多個患病學生,則協會全部人都會患病,所以患病協會裡面可能 存在參加多個協會的患病學生,這樣會帶來協會傳染,學校要求求出患病人數,當然,定義編號為0的學生是首個患病學生。   輸入:第一行n(學生人數)和m(協會數),接下來m行,每行第一個數代表k,接下來k個數代表參加此協會的學生的編號,   n==m==0時表示結束。   輸出:患病學生人數 註意:可以輸入多組數據,等結束輸入後再一起分行輸出結果。   解題思路:(並查集演算法具體思路不再贅述,詳情查看:https://www.cnblogs.com/chiweiming/p/9310599.html   並查集,我們把每個協會的學生的編號整合起來放在同一棵樹上,若一個學生參加了多個協會,則他所參協會的多棵樹會被合併起來成為一棵,這樣最後只需要找出編號為0的學生所在的樹的結點數就是患病學生的人數了。   值得註意的是,代碼中對尋找根節點的演算法進行了優化!   原來只是單純的尋找某個結點所在樹的根節點,並沒有改變數的結構:
  return node==student[node]?node:find_Father(student[node]); 

  優化後,改變了樹的結構,將結點至根節點的鏈上的所有結點直接指向了根節點,大大方便了下次再次搜尋的效率!

  static int find_Father(int node) {    //尋找根節點
        /*
        return node==student[node]?node:find_Father(student[node]); 
        */
    
        if(node!=student[node]) {
            student[node]=find_Father(student[node]);    
            //狀態壓縮,將結點node到根節點這條鏈上的結點直接指向根節點,優化下次尋找根節點的時間
        }
        return student[node];
        
    }

Java代碼:

import java.util.*;

public class TheSuspects {
    
    static int n;    //學生數量
    static int m;    //組的數量
    static int student[];
    static int book[];
    
    static int find_Father(int node) {    //尋找根節點
        /*
        return node==student[node]?node:find_Father(student[node]); 
        */
    
        if(node!=student[node]) {
            student[node]=find_Father(student[node]);    
            //狀態壓縮,將結點node到根節點這條鏈上的結點直接指向根節點,優化下次尋找根節點的時間
        }
        return student[node];
        
    }

    public static void main(String[] args) {
        
        Scanner reader=new Scanner(System.in);
        ArrayList list=new ArrayList();
        int count=0;
        
        while(reader.hasNext()) {
            
            n=reader.nextInt();
            m=reader.nextInt();
            if(n==0 && m==0) {
                break;
            }
            student=new int[n];
            book=new int[n];
            for(int i=0;i<n;i++) {
                student[i]=i;    //學生指向自己
                book[i]=1;    //每個學生為1個結點
            }
            for(int i=1;i<=m;i++) {
                int k=reader.nextInt();
                int node_One=reader.nextInt();    //輸入k個結點中的第一個
                for(int j=1;j<=k-1;j++) {
                    int node_Two=reader.nextInt();
                    int father_One=find_Father(node_One);    //找出父節點
                    int father_Two=find_Father(node_Two);
                    if(father_One!=father_Two) {
                        student[father_Two]=father_One;    //合併
                        book[father_One]+=book[father_Two];    //book[根節點]存儲其所在樹的結點數,每有一個學生加入此數,總結點數+1
                    }
                }
            }
            list.add(book[find_Father(0)]);    //求0所在樹的結點個數
            count++;
            
        }
        for(int i=0;i<count;i++) {
            System.out.println(list.get(i));
        }
    }

}

POJ截圖:

23:31:36

2018-07-18


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 前言 秒殺架構到後期,我們採用了消息隊列的形式實現搶購邏輯,那麼之前拋出過這樣一個問題:消息隊列非同步處理完每個用戶請求後,如何通知給相應用戶秒殺成功? 場景映射 首先,我們舉一個生活中比較常見的例子:我們去銀行辦理業務,一般會選擇相關業務列印一個排號紙,然後就可以坐在小板凳上玩著手機,等待被小喇叭報 ...
  • fork/join作為一個併發框架在jdk7的時候就加入到了我們的java併發包java.util.concurrent中,並且在java 8 的lambda並行流中充當著底層框架的角色。這樣一個優秀的框架設計,我自己想瞭解一下它的底層代碼是如何實現的,所以我嘗試的去閱讀了JDK相關的源碼。下麵我打 ...
  • 所有的異常都有一個超類throwable; throwable有兩個子類:Exception和error(一般在重大錯誤,不能夠自行恢復); Exception有兩個子類:checked和runtime exception異常; checked:檢查時異常,就是程式代碼有的錯誤會有紅色波浪線的異常, ...
  • Description Mato同學最近正在研究一種矩陣,這種矩陣有n行n列第i行第j列的數為gcd(i,j)。 例如n=5時,矩陣如下: 1 1 1 1 1 1 2 1 2 1 1 1 3 1 1 1 2 1 4 1 1 1 1 1 5 Mato想知道這個矩陣的行列式的值,你能求出來嗎? Mato ...
  • 上次知識回顧:https://www.cnblogs.com/dotnetcrazy/p/9278573.html 代碼褲子:https://github.com/lotapp/BaseCode 線上編程:https://mybinder.org/v2/gh/lotapp/BaseCode/mast ...
  • 引言 上一篇文章聊到了Java記憶體模型,在其中我們說JMM是建立在happens before(先行發生)原則之上的。 為什麼這麼說呢?因為在Java程式的執行過程中,編譯器和處理器對我們所寫的代碼進行了一系列的優化來提高程式的執行效率。這其中就包括對指令的“重排序”。 重排序導致了我們代碼並不會按 ...
  • goroutine只是由官方實現的超級"線程池"而已,每個實例4 5kb的棧記憶體占用和用於實現機制而大幅減少的創建和銷毀開銷。 併發不是並行(多CPU): 併發主要由切換時間片來實現"同時"運行,並行則是直接利用多核實現多線程的運行,但Go可以設置使用核數,以發揮多核電腦的能力。 通過go關鍵字實 ...
  • MIPS架構下的MCU,指令集包含R-Type、I-Type、J-Type三種,在數電課程設計時為了給MCU編寫指令集,需要將彙編語言轉化成機器代碼,這裡分享一下自己寫的Matlab 的 GUI。 主函數 C2M 函數rig_f 用來尋找名稱對應的寄存器地址 函數rig_n 用來將5位十進位數轉換成 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...