POJ 题目1236 Network of Schools(强联通)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 12015 | Accepted: 4783 |
Description
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
Output
Sample Input
5 2 4 3 0 4 5 0 0 0 1 0
Sample Output
1 2
Source
问题一:至少要向几个学校传递 原始 信息,才能保证所有学校都能收到信息。
问题二:至少要添加多少组关系(每组关系类型如右:a 可以 向 b 传递信息),才能保证 给任意一个学校原始信息后,其他所有学校都能收到信息。
#include<stdio.h> #include<string.h> #include<vector> #include<iostream> using namespace std; #define min(a,b) (a>b?b:a) #define max(a,b) (a>b?a:b) /*struct s { int u,v,next; }edge[110*110*2*100];*/ int low[110],dfn[110],ins[110],belong[110],head[110],stack[110],in[110],out[110]; int cnt,taj,top,time; vector<int>vt[110]; void init() { memset(dfn,-1,sizeof(dfn)); memset(ins,0,sizeof(ins)); memset(belong,-1,sizeof(dfn)); memset(low,-1,sizeof(low)); memset(stack,0,sizeof(stack)); memset(head,-1,sizeof(head)); memset(out,0,sizeof(out)); memset(in,0,sizeof(in)); cnt=top=time=taj=0; } /*void add(int u,int v) { edge[cnt].u=u; edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++; }*/ void tarjan(int u) { dfn[u]=low[u]=time++; ins[u]=1; stack[top++]=u; for(int i=0;i<vt[u].size();i++) { int v=vt[u][i]; if(dfn[v]==-1) { tarjan(v); low[u]=min(low[v],low[u]); } else if(ins[v]) low[u]=min(dfn[v],low[u]); } if(low[u]==dfn[u]) { int now; taj++; do { now=stack[--top]; ins[now]=0; belong[now]=taj; }while(now!=u); } } int main() { int n; while(scanf("%d",&n)!=EOF) { int i; init(); for(i=0;i<=n;i++) vt[i].clear(); for(i=1;i<=n;i++) { int v; while(scanf("%d",&v)!=EOF,v) { //add(i,v); vt[i].push_back(v); } } for(i=1;i<=n;i++) { if(dfn[i]==-1) tarjan(i); } if(taj==1) { printf("1\n0\n"); continue; } for(i=1;i<=n;i++) { //int u=edge[i].u; //int v=edge[i].v; // printf("%d %d\n",u,belong[u]); // printf("%d %d\n",v,belong[v]); int u=i; for(int j=0;j<vt[u].size();j++) { int v=vt[u][j]; if(belong[u]!=belong[v]) { in[belong[v]]++; out[belong[u]]++; } } } int ans1=0,ans2=0; for(i=1;i<=taj;i++) { if(in[i]==0) ans1++; if(out[i]==0) ans2++; } printf("%d\n",ans1); printf("%d\n",max(ans1,ans2)); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。