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 !