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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Network of Schools(POJ-1236)

發布時間:2025/3/17 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Network of Schools(POJ-1236) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Problem Description

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 個結點,以及每個結點可到達的點,求有幾個強連通分量,然后問至少在可到達的點中添加幾個點,使得圖變為強連通圖

思路:先求出圖的所有強連通分量,然后進行縮點構建新圖,第一問是新圖中入度為 0 點的個數,第二問是新圖中入度0點數與出度0點數最大的一個

Source Program

#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<cstdlib> #include<queue> #include<set> #include<map> #include<stack> #include<ctime> #include<vector> #define INF 0x3f3f3f3f #define PI acos(-1.0) #define N 20001 #define MOD 16007 #define E 1e-6 #define LL long long using namespace std; int n,m; vector<int> G[N]; stack<int> S; int dfn[N],low[N]; bool in[N],out[N]; bool vis[N];//標記數組 int sccno[N];//記錄結點i屬于哪個強連通分量 int block_cnt;//時間戳 int sig;//記錄強連通分量個數 void Tarjan(int x){vis[x]=true;dfn[x]=low[x]=++block_cnt;//每找到一個新點,紀錄當前節點的時間戳S.push(x);//當前結點入棧for(int i=0;i<G[x].size();i++){//遍歷整個棧int y=G[x][i];//當前結點的下一結點if(vis[y]==false){//若未被訪問過Tarjan(y);low[x]=min(low[x],low[y]);}else if(!sccno[y])//若已被訪問過,且不屬于任何一個連通分量low[x]=min(low[x],dfn[y]);}if(dfn[x]==low[x]){//滿足強連通分量要求sig++;//記錄強連通分量個數while(true){//記錄元素屬于第幾個強連通分量int temp=S.top();S.pop();sccno[temp]=sig;if(temp==x)break;}} } int main() {while(scanf("%d",&n)!=EOF){for(int i=0;i<n;i++)G[i].clear();for(int i=0;i<n;i++){int y;while(scanf("%d",&y)!=EOF&&y){y--;G[i].push_back(y);}}sig=0;block_cnt=0;memset(vis,0,sizeof(vis));memset(dfn,0,sizeof(dfn));memset(low,0,sizeof(low));memset(sccno,0,sizeof(sccno));for(int i=0;i<n;i++)if(vis[i]==false)Tarjan(i);memset(in,false,sizeof(in));memset(out,false,sizeof(out));for(int i=1;i<=sig;i++)in[i]=out[i]=true;for(int x=0;x<n;x++)for(int i=0;i<G[x].size();i++){int y=G[x][i];if(sccno[x]!=sccno[y])in[sccno[y]]=out[sccno[x]]=false;}int a=0,b=0;for(int i=1;i<=sig;i++){if(in[i])a++;if(out[i])b++;}if(sig==1)printf("1\n0\n");elseprintf("%d\n%d\n",a,max(a,b));} }

?

總結

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

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