题意:题意杀...网络中的一学校可以将软件发送给其他一些学校,能够发送给谁取决于他们各自维护的一个清单。将学校看成一个节点,给出每个学校的维护清单,问至少需要复制几次软件,使毎个学校都能够得到该软件。然后问在清单中至少添加几项,可使软件只要复制一次,所有学校都可以得到
思路:和HDU2767一样,最少加多少条边使得图强连通
1. 求出该图的所有强连通分量.
2. 将每个分量缩点构新DAG图.
3. 第一问就是新图中入度为0点的个数,第二问是max(入度0点数,出度0点数)
注意:当原本就是强连通的时候要特判
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
#include <stack>
using namespace std;
#define maxn 100+10
#define LL long long
int cas=1,T;
vector<int>G[maxn];
int pre[maxn];
int lowlink[maxn];
int sccno[maxn];
int dfs_clock,scc_cnt;
int n,m;
stack<int>S;
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;
memset(sccno,0,sizeof(sccno));
memset(pre,0,sizeof(pre));
for (int i = 1;i<=n;i++)
if (!pre[i])
dfs(i);
}
int in[maxn];
int out[maxn];
int main()
{
//freopen("in","r",stdin);
while (scanf("%d",&n)!=EOF && n)
{
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
for (int i = 0;i<=n;i++)
G[i].clear();
for (int u=1;u<=n;u++)
{
int v;
while (scanf("%d",&v) && v)
{
G[u].push_back(v);
}
}
find_scc(n);
for (int i = 1;i<=scc_cnt;i++)
{
in[i]=out[i]=1;
}
for (int u = 1;u<=n;u++)
for (int i =0;i<G[u].size();i++)
{
int v = G[u][i];
if (sccno[u] != sccno[v])
in[sccno[v]]=out[sccno[u]]=0;
}
int a=0,b=0;
for (int i = 1;i<=scc_cnt;i++)
{
if (in[i])
a++;
if (out[i])
b++;
}
if (scc_cnt !=1)
printf("%d\n%d\n",a,max(a,b));
else
printf("1\n0\n");
}
return 0;
}