Topic Links:
http://poj.org/problem?id=1236
Network of schools
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 11433 |
|
Accepted: 4551 |
Description A number of schools is connected to a computer network. Agreements has been developed among those Schools:each School maintains a list of schools to which it distributes Softwa Re (the "Receiving schools"). Note that if B was in the distribution list of school A, then a does not necessarily appear in the list of school B You is to write a program this computes the minimal number of schools that must receive a copy of the new software in Ord Er for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure is sending the copy of new software to an arbitrary school, this software would r Each of the schools in the network. To achieve this goal we are having to extend the lists of receivers by new members. Compute the minimal number of extensions that has to is made so this whatever school we send the new software to, it'll 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 a integer n:the number of schools in the network (2 <= N <= 100). The schools is 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 is ends with a 0. An empty list contains a 0 alone in the line.Output Your program should write, 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 field=source&key=ioi+1996 "style=" Text-decoration:none ">ioi 1996 |
[Submit] [Go back] [Status] [Discuss]
topic Meaning:
Give a list of schools that each school can send information to.
Ask subtask A: At least the number of schools to send source information, talent is all schools have received information.
Subtask B: Ask for at least a few schools to receive information, so that when information is sent to a random school, it can be transferred to other schools.
Problem Solving Ideas:
The strongly connected components of the graph are first obtained. And then indent the dots.
Statistics of the points out and into the degree.
Subtask A is the number of points for which the degree of 0 is to be calculated.
Subtask B is for max (0 numbers in degrees.) The number of degrees is 0).
Connect each node with a zero-out to the next tree with a 0-degree node.
Code:
#include <CSpreadSheet.h> #include <iostream> #include <cmath> #include <cstdio> #include <sstream> #include <cstdlib> #include <string> #include <string.h> #include <cstring># include<algorithm> #include <vector> #include <map> #include <set> #include <stack># include<list> #include <queue> #include <ctime> #include <bitset> #include <cmath># Define EPS 1e-6#define INF 0x3f3f3f3f#define PI acos ( -1.0) #define LL __int64#define ll long long#define Lson l,m, (RT<&L t;1) #define Rson m+1,r, (rt<<1) |1#define m 1000000007//#pragma comment (linker, "/stack:1024000000,1024000000") using namespace std; #define MAXN 110int low[maxn],dfn[maxn],dindex,n;int sta[maxn],belong[maxn],bcnt,ss;bool ISS[MAXN ];int de1[maxn],de2[maxn];vector<vector<int> >myv;void tarjan (int cur) {//printf (":%d\n", cur); System ("pause"); int NE; Dfn[cur]=low[cur]=++dindex; Iss[cur]=true; Sta[++ss]=cur; for (int I=0;i<myv[cur].size (); i++) {ne=myv[cur][i]; if (!dfn[ne]) {Tarjan (NE); Low[cur]=min (Low[cur],low[ne]); } else if (Iss[ne]&&dfn[ne]<low[cur]) Low[cur]=dfn[ne]; } if (Dfn[cur]==low[cur]) {bcnt++; do {ne=sta[ss--]; Iss[ne]=false; belong[ne]=bcnt; }while (ne!=cur); }}void solve () {int i; ss=bcnt=dindex=0; memset (dfn,0,sizeof (DFN)); memset (Iss,false,sizeof (ISS)); for (int i=1;i<=n;i++) if (!dfn[i]) Tarjan (i);} int main () {//freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w", stdout); while (~SCANF ("%d", &n)) {myv.clear (); Myv.resize (n+1); for (int i=1;i<=n;i++) {int A; while (scanf ("%d", &a) &&a) Myv[i].push_back (a); } solve (); if (bcnt==1)//is a strong connected component {printf ("1\n0\n"); ContInue; } int ansa=0,ansb=0; memset (de1,0,sizeof (de1)); memset (de2,0,sizeof (DE2)); for (int i=1;i<=n;i++) {for (int j=0;j<myv[i].size (); j + +) {int ne=myv[i][j]; if (Belong[i]!=belong[ne]) {de1[belong[ne]]++;//in degrees De2[be long[i]]++; Out of the degree}}} for (int i=1;i<=bcnt;i++) {if (!de1[i]) an sa++; if (!de2[i]) ansb++; } ansb=max (Ansb,ansa); printf ("%d\n%d\n", ANSA,ANSB); } return 0;}
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
[Tarjan] POJ 1236 Network of schools