poj 1236 Network of Schools
来源:互联网 发布:美国人口普查历年数据 编辑:程序博客网 时间:2024/06/06 03:42
Network of Schools
Time Limit: 1000MS
Memory Limit: 10000KTotal Submissions: 17946
Accepted: 7065
Memory Limit: 10000KTotal Submissions: 17946
Accepted: 7065
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.
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
这道题和nyoj 120 这道题很像,但是这题多了个输出。
1.首先输出点为入度为0 的个数;
2.在输出我们需要加多少条关系可以让学校之间两两互通。(也就是入度和出度的最大值)
注意:这道题在只有一个点的时候 是要输出
1
0
这一点坑到了。。。。
AC代码:
这道题和nyoj 120 这道题很像,但是这题多了个输出。
1.首先输出点为入度为0 的个数;
2.在输出我们需要加多少条关系可以让学校之间两两互通。(也就是入度和出度的最大值)
注意:这道题在只有一个点的时候 是要输出
1
0
这一点坑到了。。。。
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <stack>using namespace std;#define MAX 105int map[MAX][MAX], DFN[MAX], low[MAX], in[MAX], out[MAX];int flag[MAX], step[MAX];stack<int>S;int res, tot, M, ans, ans1;void Init(){ memset(map, 0, sizeof(map)); memset(DFN, 0, sizeof(DFN)); memset(low, 0, sizeof(low)); memset(in, 0, sizeof(in)); memset(out, 0, sizeof(out)); memset(flag, 0, sizeof(flag)); if(!S.empty()) S.pop(); tot = 0, res = 0;}void tarjan(int v){ DFN[v] = low[v] = ++tot;///初始化两个值,自己为能找到的最先访问的祖先 int u; flag[v] = 1; S.push(v); for(int i = 1; i <= M; i++) { if(map[v][i]) { if(!DFN[i]) ///如果该点没有访问过 { tarjan(i); low[v] = min(low[v], low[i]); } else if(flag[i]) { low[v] = min(low[v], low[i]); } } } if(DFN[v]==low[v]) { ++res; do { u = S.top(); S.pop(); flag[u] = 0; step[u] = res; } while(v!=u); }}void solve(){ for(int i = 1; i <= M; i++) { for(int j = 1; j <= M; j++) { if(step[i]!=step[j]&&map[i][j]) in[step[j]]++, out[step[i]]++; } } int xx = 0, yy = 0; for(int i = 1; i <= res; i++) { if(in[i]==0) { xx++; } else if(out[i]==0) yy++; } ans = xx; ans1 = max(xx, yy); printf("%d\n",ans); printf("%d\n",ans1);}int main(){ while(~scanf("%d",&M)) { Init(); for(int i = 1; i <= M; i++) { int x; while(scanf("%d",&x),x) { map[i][x] = 1; } } for(int i = 1; i <= M; i++) { if(!DFN[i]) { tarjan(i); } } if(res == 1) { printf("1\n0\n"); continue; } solve(); } return 0;}
0 0
- POJ 1236 Network of Schools
- POJ 1236 Network of Schools
- poj 1236 Network of Schools
- POJ 1236 Network of Schools
- POJ 1236 Network of Schools
- poj 1236 Network of Schools
- poj 1236 Network of Schools
- POJ 1236 Network of Schools
- POJ-1236-Network of Schools
- POJ 1236 Network of Schools
- POJ 1236 Network of Schools
- poj 1236 Network of Schools
- poj 1236 Network of Schools
- POJ-1236-Network of Schools
- POJ 1236 Network of Schools
- poj 1236 Network of Schools
- Network of Schools (POJ 1236)
- POJ 1236:Network of Schools
- 自动化用到的一些小技巧
- POJ 1679:The Unique MST(次小生成树)
- 剑指offer-面试题 20:顺时针打印矩阵
- C++作业5
- Java将远程服务器上的文件经过本地服务器中转后输出至前端
- poj 1236 Network of Schools
- Emmet(ZenCoding)语法(一)
- git的基础使用
- es6中"类"--class的方式创建对象和es5中正常用法比较
- [硬件]树莓派2代无显示器安装系统(固定IP+VNC开机自启动)
- 纯原生js 贪吃蛇(锻炼你逻辑思维)
- 问号
- 微信公众号开发---群发消息
- 如何编写linux下nand flash驱动