1016,punk1016

來源:互聯網
上載者:User

1016,punk1016

串連電腦

Time Limit:1000MS  Memory Limit:65536K
Total Submit:69 Accepted:24

Description

Description:

機房裡有若干台電腦,其中有一些電腦已經相互串連。如果A和B通過網線相連,並且B與C也通過網線相連,那麼即便A和C之間沒有直接的網線相連,也可以認為A和C是相連的。由於機房裡的布線比較亂,並不是所有電腦都相互連通,請問在不變動當前布線情況下,最少要購買幾條網線才能使得機房所有電腦都兩兩連通。

Input

Input:

多組資料。每組資料第一行為整數N,M。N是電腦數量,M是機房已布置好的網線數量。接下來M行,每行為整數A,B。表明A,B之間通過一條網線直接相連。這裡可以認為網線是不分方向的,即A->B等價於B->A。
(0 < N <= 200,0 <= M <= 10000,0 < A,B <= N 。) N=0和M=0為輸入結束,不需要處理。

Output

Output:

每組一個整數,即最少還要購買幾根網線。

Sample Input

Sample Input:4 21 22 34 01 00 0

Sample Output

Sample Output:130

Source

ahstu@ICPC01

    using System;    using System.Collections.Generic;    using System.Linq;    using System.Text;    namespace AK1016 {        class Program {            static void Main(string[] args) {                string jt;                while ((jt = Console.ReadLine()) != null) {                    string[] s = jt.Split();                    int n = int.Parse(s[0]), m = int.Parse(s[1]);                    if (m + n == 0) break;                    if (m == 0 && n != 0) { Console.WriteLine(n - 1); continue; }                    string[] b = Console.ReadLine().Split();                    int[] a = new int[205];                    int count = 1;                    a[int.Parse(b[0])] = count;                    a[int.Parse(b[1])] = count;                    for (int i = 1; i < m; i++) {                        string[] sb = Console.ReadLine().Split();                        int p = int.Parse(sb[0]), q = int.Parse(sb[1]);                        if (a[p] != 0) a[q] = count;                        else if (a[q] != 0) a[p] = count;                        else {                            count++;                            a[p] = count;                            a[q] = a[p];                        }                    }                    for (int i = 1; i <= n; i++)                        if (a[i] == 0)                            count++;                    Console.WriteLine(count - 1);                }            }        }    }


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。Welcome to share your nice codes !

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.