POJ 1236 Network of Schools
来源:互联网 发布:淘宝怎么开店好赚钱呢 编辑:程序博客网 时间:2024/05/29 14:34
http://poj.org/problem?id=1236
Network of Schools
Time Limit: 1000MSMemory Limit: 10000KTotal Submissions: 11919Accepted: 4750
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
tarjan 求scc图。第一问是求scc图中入度为0的顶点数目。第二问是求scc图中入读为0 的点和出度为0 的点的数目的最大值。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 100+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
bool cmp(int a,int b)
{
return a>b;
}
vector<int>G[maxn];
int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;
stack<int>S;
int n;
int outdegree[maxn],indegree[maxn];
void dfs(int u)
{
pre[u]=lowlink[u]=++dfs_clock;
S.push(u);
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(!pre[v])
{
dfs(v);
lowlink[u]=min(lowlink[u],lowlink[v]);
}
else if(!sccno[v]) lowlink[u]=min(lowlink[u],pre[v]);
}
if(lowlink[u]==pre[u])
{
scc_cnt++;
for(;;)
{
int x=S.top();S.pop();
sccno[x]=scc_cnt;
if(x==u)break;
}
}
}
void find_scc(int n)
{
dfs_clock=scc_cnt=0;
cle(sccno);
cle(pre);
for(int i=1;i<=n;i++)
if(!pre[i])dfs(i);
}
void solve()
{
find_scc(n);
int in=0,out=0;
if(scc_cnt==1)
{
printf("%d\n%d\n",1,0);
}
else
{
cle(outdegree),cle(indegree);
for(int i=1;i<=n;i++)
for(int j=0;j<G[i].size();j++)
{
int k=G[i][j];
if(sccno[i]!=sccno[k])
{
outdegree[sccno[i]]++;
indegree[sccno[k]]++;
}
}
for(int i=1;i<=scc_cnt;i++)
{
if(indegree[i]==0)in++;
if(outdegree[i]==0)out++;
}
printf("%d\n%d\n",in,max(in,out));
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
while(cin>>n)
{
int a,b;
for(int i=1;i<=n;i++)
{
while(1)
{
scanf("%d",&a);
if(a==0)break;
G[i].push_back(a);
}
}
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
- javascript、JavaWeb、URL的区别联系
- HDU 1556 Color the ball
- 1B. Spreadsheets
- HDU 5174 Ferries Wheel
- Tarjan算法
- POJ 1236 Network of Schools
- POJ 2553 The Bottom of a Graph
- POJ 2186 Popular Cows
- HDU 5050 Divided Land
- HDU 4027 Can you answer these queries?
- Kosaraju算法
- HDU 2063 过山车
- HDU 3861 The King’s Problem
- CF 293 A. Vitaly and Strings
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
切断机厂家
切断机刀片
调直切断机生产厂家
钢筋切断机功率
加气块切断机
二手钢筋切断机
钢丝绳切断机
工字钢切断机
角钢切断机价格
切断机价格
锚杆切断机
切断机型号
切断机图片
钢筋切断机的价格
小型切断机
钢管切断机
钢筋 切断机
胶带切断机
钢筋切断机图片
钢筋切断机报价
钢筋切断机配件
gq40型钢筋切断机
线缆切断机
直螺纹切断机
切割机坡口机
混凝土切割设备
海绵切割设备
水切割机报价
金属切割设备
数控水切割机
水切割机价格
仿型切割机
不锈钢金属切割机
半自动火焰切割机
铝合金精密切割机
芯样切割机
卫生纸切割设备
圆管切割机
大型数控切割机
小型金属切割机价格
微型数控切割机