POJ 1236 Network of Schools(tarjan缩点)
来源:互联网 发布:excel 导入 数据库 编辑:程序博客网 时间:2024/06/12 21:12
Network of Schools
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 19388 Accepted: 7626
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
题解
#include <iostream>#include <stdio.h>#include <stack>#include <vector>using namespace std;#define LL long longconst int maxn = 110;vector<int>G[maxn];stack<int> q;int cor[maxn],numcor,DFN[maxn],low[maxn],tm,instack[maxn];void init(){ memset(cor,0,sizeof cor); memset(DFN,0,sizeof DFN); memset(low,0,sizeof low); memset(instack,0,sizeof instack); tm=0; numcor=0; for(int i=1;i<maxn;i++) { G[i].clear(); }}void tarjan(int u){ DFN[u]=low[u]=++tm; q.push(u); instack[u]=1; for(int i=0;i<G[u].size();i++) { int v=G[u][i]; if(DFN[v]==0) { tarjan(v); if(low[u]>low[v]) low[u]=low[v]; } else if(instack[v]&&low[u]>DFN[v]) { low[u]=DFN[v]; } } if(DFN[u]==low[u]) { int v; numcor++; do { v=q.top(); cor[v]=numcor; instack[v]=0; q.pop(); } while(v!=u); }}int main(){ int n; while(~scanf("%d",&n)) { init(); for(int i=1;i<=n;i++) { int v; while(scanf("%d",&v)) { if(v==0) break; G[i].push_back(v); } } for(int i=1;i<=n;i++){ if(DFN[i]==0) tarjan(i); } int ru[maxn],chu[maxn]; memset(ru,0,sizeof ru); memset(chu,0,sizeof chu); for(int i=1;i<=n;i++) { for(int j=0;j<G[i].size();j++) { int v=G[i][j]; if(cor[v]==cor[i]) { continue; } else { ru[cor[v]]++; chu[cor[i]]++; } } } int numru=0; int k=0; int numchu=0; for(int i=1;i<=numcor;i++) { if(ru[i]==0&&chu[i]==0) k++; if(ru[i]==0) numru++; if(chu[i]==0) numchu++; } if(k==1&&numcor==1) printf("1\n0\n"); else printf("%d\n%d\n",numru,max(numru,numchu)); } }
阅读全文
0 0
- poj 1236 Network of Schools(tarjan 缩点)
- poj 1236 Network of Schools 1 (tarjan 缩点)
- POJ 1236 Network of Schools(tarjan缩点)
- Network of Schools - POJ 1236 Tarjan 缩点
- poj 1236 Network of Schools (tarjan)
- Network of Schools(POJ 1236)(强连通+缩点)(Tarjan算法)
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
- POJ 1236 Network of Schools(Tarjan + 缩点 + 计算出度and入度)
- POJ 1236 Network of Schools(强连通分量缩点+tarjan算法)
- 强连通分量,Tarjan,缩点(Network of Schools,POJ 1236)
- POJ 1236 Network of Schools (tarjan求强连通,缩点)
- POJ 1236 Network of Schools (Tarjan算法求强连通分量+缩点) 代码详解
- POJ 1236 Network of Schools tarjan强连通缩点
- POJ 1236 Network of Schools【强连通分量分解&&缩点||tarjan&&缩点】
- [缩点] poj 1236 Network of Schools
- POJ1236-Network of Schools(Tarjan + 缩点)
- poj1236 Network of Schools(tarjan缩点)
- poj 1236 Network of Schools(Tarjan)
- Kotlin来源
- js 获取url请求参数
- bzoj 3943: [Usaco2015 Feb]SuperBull 最大生成树
- Python-learning Day04
- 查找文件的类 struct _finddata_t结构体用法
- POJ 1236 Network of Schools(tarjan缩点)
- oracle sequences 时间加序列的主键怎样取?
- Hibernate之多表查询
- c#命名
- assets与raw
- 腾讯云首推私有云存储,意欲抢占更多用户市场
- 解决聊天页面输入法和表情面板切换界面闪动问题
- oracle buffer cache
- PAT_1088. Rational Arithmetic