日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ - 1236 Network of Schools

發布時間:2025/5/22 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ - 1236 Network of Schools 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the distribution list of school A, then A does not necessarily appear in the list of school B
You are to write a program that computes the minimal number of schools that must receive a copy of the new software in order for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure that by sending the copy of new software to an arbitrary school, this software will reach all schools in the network. To achieve this goal we may have to extend the lists of receivers by new members. Compute the minimal number of extensions that have to be made so that whatever school we send the new software to, it will reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school.

Input The first line contains an integer N: the number of schools in the network (2 <= N <= 100). The schools are identified by the first N positive integers. Each of the next N lines describes a list of receivers. The line i+1 contains the identifiers of the receivers of school i. Each list ends with a 0. An empty list contains a 0 alone in the line.
Output Your program should write two lines to the standard output. The first line should contain one positive integer: the solution of subtask A. The second line should contain the solution of subtask B.
Sample Input 5 2 4 3 0 4 5 0 0 0 1 0 Sample Output 1 2

題意:

N個學校之間有單向的網絡,每個學校可以通過單向網絡向周邊的學校傳輸信息。

問題1:初始至少需要向多少個學校發放信息,使得網絡內所有的學校最終都能收到信息。

問題2:至少需要添加幾條邊,使得任意向一個學校發送信息后,經過若干次傳送,所有的學校都能收到信息。

題解:

分析題意發現其實就是問對于一個有向無環圖:
1.至少要選幾個點,才能從這些點出發到達所有點 。

2.至少加入幾條邊,就能從圖中任何一個點出發到達所有點。

當然圖不一定是無環圖,那么就用到了Tarjan算法來找到強連通分量,然后再縮點就變成了有向無環圖。

然后不難想出對于第一個問題答案就是有向無環圖中入度為0的點的個數。對于第二個問題仔細想一下就發現了如果要達到題意肯定每個點都有出有入,所以要消滅所有入度為零和出度為零的點,所以如果入度為0的個數是n,出度為0的個數是m,至少添加邊的條數就是max(n,m)。

代碼:

#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib>using namespace std;#define MAXN 205bool is_instack[MAXN];//記錄節點是否在棧中 int stack[MAXN],top; bool map[MAXN][MAXN];//存圖,這里用的矩陣僅供理解,具體做題自己調整。 int DFN[MAXN];//記錄節點第一次被訪問時的時間 int LOW[MAXN];//記錄節點與節點的子樹節點中最早的步數 int time; int Belong[MAXN];//記錄每個節點屬于的強連通分量編號 int N,cnt;//N是點數,cnt是強連通分量編號。void Tarjan(int x){DFN[x] = LOW[x] = ++time;is_instack[x] = true;stack[++top] = x;for(int i=1 ; i<=N ; ++i){if(map[x][i] == false)continue;if(!DFN[i]){Tarjan(i);if(LOW[i]<LOW[x]){LOW[x] = LOW[i];}}else if(is_instack[i] && DFN[i]<DFN[x]){ //這里注意不能直接else,因為DFN[i]!=0還有可能是橫叉邊。LOW[x] = min(LOW[x],DFN[i]);}}if(DFN[x] == LOW[x]){++cnt;int mid;do{mid = stack[top--];is_instack[mid] = false;Belong[mid] = cnt;}while(mid != x);} }int in[MAXN];//記錄縮點后點的入度 int out[MAXN];//記錄縮點后點的出度 void init(){memset(map,false,sizeof(map));memset(DFN,0,sizeof(DFN));memset(LOW,0,sizeof(LOW));memset(in,0,sizeof(in));memset(out,0,sizeof(out));memset(is_instack,false,sizeof(is_instack));time = 0;cnt = 0;top = 0; }int main(){while(scanf("%d",&N)!=EOF){init();for(int i=1 ; i<=N ; ++i){int t;while(scanf("%d",&t) && t)map[i][t] = true;}for(int i=1 ; i<=N ; ++i){if(!DFN[i])Tarjan(i);//有可能不是連通圖所以遍歷一遍。 }for(int i=1 ; i<=N ; ++i){for(int j=1 ; j<=N ; ++j){if(map[i][j] && Belong[i] != Belong[j]){++out[Belong[i]];++in[Belong[j]];}}}if(cnt==1){printf("1\n0\n");continue;}int inzero = 0,outzero = 0;for(int i=1 ; i<=cnt ; ++i){if(!in[i])++inzero;if(!out[i])++outzero;}printf("%d\n%d\n",inzero,max(inzero,outzero));}return 0; }


轉載于:https://www.cnblogs.com/vocaloid01/p/9514080.html

總結

以上是生活随笔為你收集整理的POJ - 1236 Network of Schools的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产第六页| 精品乱码一区二区三四区视频 | 久久免费国产精品 | 看片免费黄在线观看入口 | 女上男下动态图 | 国产乱淫av片免费 | 国产精选在线 | 精品视频免费播放 | 黄瓜视频在线免费看 | 精品亚洲永久免费 | 午夜精品一区二 | 成人欧美精品一区二区 | 2021亚洲天堂 | 黄色一级录像片 | 国产一区二区三区四区五区 | 少妇无套内谢久久久久 | 久久久精品人妻av一区二区三区 | 久久亚洲一区二区 | 国产成人久久精品 | 制服丝袜在线第一页 | 曰本无码人妻丰满熟妇啪啪 | 久久久久久97 | 四虎影视免费在线观看 | 韩国av中文字幕 | 亚洲欧美国产一区二区 | 婷婷综合 | 亚洲一区免费在线 | 日韩毛片一区 | 美女免费黄视频 | 天堂а√在线中文在线 | 香蕉a视频 | 污片网站 | 日本在线观看视频网站 | 日韩免费片 | 91久久精品一区二区三区 | 日本午夜视频 | 国产亚洲精品美女久久久久 | 中文字幕xxxx | www.黄色av| 91福利影院 | 青草青青视频 | 国产欧美日韩中文字幕 | 国产69xx| 91蝌蚪在线观看 | 国产三级一区 | 久久夜色av | 无码人妻精品一区二区三 | 成人毛片在线免费观看 | 日韩最新网址 | 精品视频在线免费观看 | 黑人巨大精品欧美黑寡妇 | 国产精品第七页 | 九一在线视频 | 色悠久 | 亚洲性夜 | 午夜日韩av | 二区在线观看 | 九九视频免费看 | 亚洲欧洲一区二区三区 | 诱夫1v1高h| 欧美三级小视频 | 一本视频在线 | 就爱啪啪网| 欧美日韩高清一区二区 国产亚洲免费看 | 调教少妇视频 | 久久影院中文字幕 | 国语精品久久 | 五月天伊人网 | 91av在线看 | 国产综合亚洲精品一区二 | 免费av资源 | 99精品人妻国产毛片 | 中文字幕最新在线 | 欧美人与牲动xxxx | 日韩性生交大片免费看 | 日韩黄色视屏 | 国产剧情一区 | 人妻换人妻仑乱 | 人妻精油按摩bd高清中文字幕 | 一极黄色大片 | 最新av女优 | 在线免费看av网站 | 午夜一级片| 日韩中文字幕视频在线观看 | 天天躁日日躁狠狠很躁 | sm调教羞耻姿势图片 | 黄色三及 | 熟女精品一区二区三区 | 99re视频在线观看 | 美女露隐私网站 | 日韩在线第二页 | 日韩a级在线观看 | av手机天堂网 | 91网页在线观看 | 午夜精品福利影院 | 天天操狠狠操 | 网站久久 | 日韩久久精品视频 | 国产精品3 |