Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 18759 | Accepted: 7387 |
Description
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
Output
Sample Input
5 2 4 3 0 4 5 0 0 0 1 0
Sample Output
1 2
Source
原题链接:http://poj.org/problem?id=1236
题意:
一些学校连成了网络, 在学校之间存在某个协议:每个学校都维护一张传送表,表明他们要负责将收到的软件传送到表中的所有学校。如果A在B的表中,那么B不一定在A的表中。
现在的任务就是,给出所有学校及他们维护的表,问1、如果所有学校都要被传送到,那么需要几份软件备份;2、如果只用一份软件备份,那么需要添加几条边?
PS:第二道Tarjan,并且还用了缩点,搞了一下午,注意,此题中Tarjan用到的栈要定义成全局变量.....
参考博客:
http://blog.youkuaiyun.com/jiangzh7/article/details/8639709
http://www.cnblogs.com/ACMERY/p/4681803.html
http://blog.youkuaiyun.com/wangjian8006/article/details/7888558
http://blog.youkuaiyun.com/huzhengnan/article/details/7787595[不错]
http://www.cnblogs.com/kuangbin/archive/2011/08/07/2130277.html
AC代码:
/**
* 行有余力,则来刷题!
* 博客链接:http://blog.youkuaiyun.com/hurmishine
* 个人博客网站:http://wuyunfeng.cn/
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
using namespace std;
const int maxn=1000+5;
bool a[maxn][maxn];
int low[maxn],dfn[maxn];
int belong[maxn];
int in[maxn],out[maxn];
bool vis[maxn];
int index,cnt;
int n;
stack<int>s;
void Init()
{
for(int i=0;i<=n;i++)
{
dfn[i]=low[i]=vis[i]=0;
}
memset(a,false,sizeof(a));
index=cnt=0;
}
void Tarjan(int u)
{
low[u]=dfn[u]=++index;
s.push(u);
vis[u]=true;
for(int v=1;v<=n;v++)
{
if(!a[u][v])
continue;
if(!dfn[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v])
{
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u])
{
int x;
cnt++;
do
{
x=s.top();
s.pop();
belong[x]=cnt;
vis[x]=false;
}while(x!=u);
}
}
int main()
{
//freopen("C:\\Documents and Settings\\Administrator\\桌面\\data.txt","r",stdin);
while(cin>>n)
{
Init();
int x;
for(int i=1;i<=n;i++)
{
while(scanf("%d",&x)&& x)
{
a[i][x]=true;
}
}
for(int i=1;i<=n;i++)
{
if(!dfn[i])
Tarjan(i);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][j]&&belong[i]!=belong[j])
{
out[belong[i]]++;
in[belong[j]]++;
}
}
}
int t1=0,t2=0;
for(int i=1;i<=cnt;i++)
{
if(!in[i])
t1++;
if(!out[i])
t2++;
}
if(cnt==1)
cout<<"1"<<endl<<"0"<<endl;
else
cout<<t1<<endl<<max(t1,t2)<<endl;
}
return 0;
}
AC代码2:
/**
* 行有余力,则来刷题!
* 博客链接:http://blog.youkuaiyun.com/hurmishine
* 个人博客网站:http://wuyunfeng.cn/
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
using namespace std;
const int maxn=1000+5;
int low[maxn],dfn[maxn];
bool vis[maxn];
int belong[maxn];
int in[maxn],out[maxn];
vector<int>G[maxn];
stack<int>s;
int n;
int index,cnt;
void Init()
{
cnt=index=0;
for(int i=0;i<=n;i++)
{
G[i].clear();
low[i]=dfn[i]=vis[i]=0;
//belong[i]=in[i]=out[i]=
}
}
void Tarjan(int u)
{
low[u]=dfn[u]=++index;
vis[u]=true;
s.push(u);
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(!dfn[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v])
{
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u])
{
cnt++;
int x;
do
{
x=s.top();
s.pop();
vis[x]=false;
belong[x]=cnt;
}while(x!=u);
}
}
int main()
{
//freopen("C:\\Documents and Settings\\Administrator\\桌面\\data.txt","r",stdin);
while(cin>>n)
{
Init();
int x;
for(int i=1;i<=n;i++)
{
while(scanf("%d",&x),x)
{
G[i].push_back(x);
}
}
for(int i=1;i<=n;i++)
{
if(!dfn[i])
{
Tarjan(i);
}
}
for(int i=1;i<=n;i++)
{
for(int j=0;j<G[i].size();j++)
{
int v=G[i][j];
if(belong[i]!=belong[v])
{
//cout<<belong[i]<<","<<belong[v]<<endl;
out[belong[i]]++;
in[belong[v]]++;
}
}
}
int t1=0,t2=0;
for(int i=1;i<=cnt;i++)
{
if(!in[i])
t1++;
if(!out[i])
t2++;
}
//cout<<cnt<<endl;
if(cnt==1)
cout<<"1"<<endl<<"0"<<endl;
else
cout<<t1<<endl<<max(t1,t2)<<endl;
}
return 0;
}