日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

【解析】案例4-1.7 文件传输 (25 分)

發(fā)布時(shí)間:2024/2/28 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【解析】案例4-1.7 文件传输 (25 分) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

立志用最少的代碼做最高效的表達(dá)


當(dāng)兩臺(tái)計(jì)算機(jī)雙向連通的時(shí)候,文件是可以在兩臺(tái)機(jī)器間傳輸?shù)摹=o定一套計(jì)算機(jī)網(wǎng)絡(luò),請(qǐng)你判斷任意兩臺(tái)指定的計(jì)算機(jī)之間能否傳輸文件?

輸入格式:
首先在第一行給出網(wǎng)絡(luò)中計(jì)算機(jī)的總數(shù) N (2≤N≤10^4),于是我們假設(shè)這些計(jì)算機(jī)從 1 到 N 編號(hào)。隨后每行輸入按以下格式給出:

I c1 c2
其中I表示在計(jì)算機(jī)c1和c2之間加入連線,使它們連通;或者是

C c1 c2
其中C表示查詢計(jì)算機(jī)c1和c2之間能否傳輸文件;又或者是

S
這里S表示輸入終止。

輸出格式:
對(duì)每個(gè)C開(kāi)頭的查詢,如果c1和c2之間可以傳輸文件,就在一行中輸出"yes",否則輸出"no"。當(dāng)讀到終止符時(shí),在一行中輸出"The network is connected.“如果網(wǎng)絡(luò)中所有計(jì)算機(jī)之間都能傳輸文件;或者輸出"There are k components.”,其中k是網(wǎng)絡(luò)中連通集的個(gè)數(shù)。

輸入樣例 1:
5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
S
輸出樣例 1:
no
no
yes
There are 2 components.

輸入樣例 2:
5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
I 1 3
C 1 5
S
輸出樣例 2:
no
no
yes
yes
The network is connected.


暴力使用DFS必定超時(shí)。 并查集模板題


#include<iostream> #include<cstdio> #include<string> using namespace std; const int maxn = 1e4 + 5; int pre[maxn]; //定義并查集數(shù)組 int find(int x) { return x == pre[x] ? x : pre[x] = find(pre[x]); }void merge(int x,int y){int fx=find(x);int fy=find(y);if(fx > fy) pre[fx] = fy;else pre[fy] = fx; }int main() {int n, x, y;char c; cin >> n;getchar();for(int i = 1; i <= n; i++) pre[i] = i; //1、初始化 while(cin >> c) {if(c == 'S') { //判斷連通塊 int ans = 0; for(int i = 1; i <= n; i++) {if(i == pre[i]) ans++; //如果是這樣,說(shuō)明是單獨(dú)節(jié)點(diǎn) }if(ans == 1) printf("The network is connected.\n");else printf("There are %d components.\n", ans);break;} else if(c == 'I') {scanf("%d%d", &x, &y);getchar();merge(x, y);} else if(c == 'C') {scanf("%d%d", &x, &y);getchar();if(find(x) == find(y)) printf("yes\n");else printf("no\n");}}return 0; }

耗時(shí)


?????????——泰山崩于前而色不變,麋鹿興于左而目不瞬。

總結(jié)

以上是生活随笔為你收集整理的【解析】案例4-1.7 文件传输 (25 分)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。