標籤:school output tarjan out scanf order develop war eve
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
| Total Submissions: 16873 |
|
Accepted: 6672 |
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
52 4 3 04 5 0001 0
Sample Output
12
Source
IOI 1996
題目大意:
100個學校,有單向網路連接,從而分享軟體。給出n個點,接下來n行,第i行的數表示i與這些點相連,以一個0來結束這一行。
輸出要求:第一行輸出最少有幾個學校得到軟體,其餘所有學校才可以都得到軟體;第二行輸出再增加幾條單向邊,可以使任意一個學校得到軟體,其餘學校便都可以得到軟體。
思路:Tarjan跑一遍,進行縮點,最後有幾個入度為零的點輸出就是第一行的答案,第二行需要特判,若是本來就可以縮成一個點,則輸出0,否則出度為0的點數和入度為0 的點數取大。。。
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<stack> 5 #define maxn 1005 6 #define maxm 10005 7 using namespace std; 8 int n,m; 9 struct edge{10 int u,v,next;11 }e[maxm],ee[maxm];12 int head[maxn],js,headd[maxn],jss,bianshu;13 bool exist[maxn];14 int visx,cur;// cur--縮出的點的數量 15 int dfn[maxn],low[maxn],belong[maxn],chudu[maxn],rudu[maxn];16 stack<int>st;17 void add_edge(int u,int v){18 e[++js].u=u;e[js].v=v;19 e[js].next=head[u];head[u]=js;20 }21 void add_edgee(int u,int v){22 ee[++jss].u=u;ee[jss].v=v;23 ee[jss].next=head[u];head[u]=jss;24 }25 void tarjan(int u){26 dfn[u]=low[u]=++visx;27 st.push(u);exist[u]=true;28 for(int i=head[u];i;i=e[i].next){29 int v=e[i].v;30 if(dfn[v]==0) {31 tarjan(v);32 low[u]=min(low[v],low[u]);33 }34 else if(exist[v]&&low[u]>dfn[v]) low[u]=dfn[v];35 }36 int j;37 if(low[u]==dfn[u]){38 ++cur;39 do{40 j=st.top();st.pop();41 exist[j]=false;belong[j]=cur;42 }while(j!=u);43 }44 }45 int main()46 {47 scanf("%d",&n);48 for(int i=1;i<=n;i++){49 while(scanf("%d",&m)==1&&m!=0){50 add_edge(i,m);bianshu++;51 }52 }53 for(int i=1;i<=n;i++)54 if(dfn[i]==0)55 tarjan(i); 56 for(int i=1;i<=js;i++){57 int u=e[i].u,v=e[i].v;58 if(belong[u]!=belong[v]){59 add_edgee(belong[u],belong[v]);60 rudu[belong[v]]++;chudu[belong[u]]++;61 }62 }63 int chu=0,ru=0;64 for(int i=1;i<=cur;i++){65 if(chudu[i]==0) chu++;66 if(rudu[i]==0) ru++;67 }68 printf("%d\n",ru);69 if(cur==1) printf("%d\n",0);70 else printf("%d\n",max(ru,chu));71 72 return 0;73 }
POJ 1236 Network of Schools