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 0Sample 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");
else
printf("%d\n%d\n",a,max(a,b));
}
}
相關文章
- POJ 1236 Network of Schools 強連通分量
- P2746 [USACO5.3] 校園網Network of Schools
- 【Tarjan SCC 加邊使得所有圖聯通 至少選取多少個點能圖聯通 】Network of Schools加強版.md
- w3schools網站的HTML教程之HTML基礎網站HTML
- Simple Neural Network
- Set介面_network
- docker network之 noneDockerNone
- The Network Diagram on the PVE system
- Network sniffing and identity authenticationIDE
- Siamese network總結
- Network(POJ-1144)
- POJ 1861 Network (Kruskal)
- Network Embedding_LINE
- network xxx was found but has incorrect label com.docker.compose.network set to "xxx"Docker
- The Network Program Log Two (Scapy)
- ch21_network_programming
- 5g-network-slicing
- kubernetes CNI(Container Network Inferface)AI
- kuangbin——QS Network - Preliminary Round
- 什麼是Phala Network?
- Phala Network經濟模型模型
- VNC(Virtual Network Computing)服務VNC
- k8s & docker networkK8SDocker
- Network config of Openwrt based ESXI Env
- Network Motif 文獻調研
- Deep Hashing Network for Efficient Similarity RetrievalMILA
- PostgreSQL DBA(173) - pgAdmin(Network latency)SQL
- Paper -- DenseNet:Densely Connected Convolutional NetworkSENet
- Chrome DevTools的Network皮膚Chromedev
- [network][easy case]troubleshoting the connection to a remote serverREMServer
- DualGNN: Dual Graph Neural Network for Multimedia RecommendationGNN
- DB2 Port (Network Security) RequirementsDB2UIREM
- SKS Keyserver Network 遭到“中毒”攻擊Server
- Feature Mask Network for Person Re-identificationIDE
- GAT: Graph Attention Network | 論文分享
- network.service - LSB: Bring up/down networking Loaded: loaded (/etc/rc.d/init.d/network; bad; v
- 什麼是 Intelligence Enterprise 的 Business network?Intel
- Graph Neural Network——圖神經網路神經網路