usaco 5.3 Network of Schools(强连通分量+构造强连通最少边)

Network of Schools
IOI '96 Day 1 Problem 3

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.

PROGRAM NAME: schlnet

INPUT FORMAT

The first line of the input file 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.

SAMPLE INPUT (file schlnet.in)

5
2 4 3 0
4 5 0
0
0
1 0

OUTPUT FORMAT

Your program should write two lines to the output file. The first line should contain one positive integer: the solution of subtask A. The second line should contain the solution of subtask B.

SAMPLE OUTPUT (file schlnet.out)

1
2

题意:给你n个学校的关系图(学校间相互传递软件),每次要分发一个新软件,最少要发给几个学校,如果要使得随便选一个学校,就能发到所有学校,至少要建立几个新的关系。。。

分析:一开始就想到强连通,但是好久没写,想偷懒,就直接深搜加标记入度出度,一顿乱搞,居然过了10组数据,最后一组实在是骗不过去了T_T

后来就复习了下强连通分量,先把原图强连通缩点,新的图是一片深林,只要统计有几个树根和几个叶子,树根的个数就是第一解,叶子和树根的最大值就是第二解,注意可能只有一个节点,那么第二解为0

PS:再也不敢偷懒了,wa了七次啊= =

代码:

/*
ID: 15114582
PROG: schlnet
LANG: C++
*/
#include<cstdio>
#include<iostream>
using namespace std;
const int mm=111;
const int mn=111111;
int in[mm],out[mm],head[mm],dfn[mm],low[mm],q[mm],id[mm];
int ver[mn],next[mn];
int i,j,n,ans1,ans2,edge,index,top,num;
void dfs(int u)
{
    dfn[u]=low[u]=++index;
    q[top++]=u;
    for(int i=head[u],v;i>=0;i=next[i])
        if(!dfn[v=ver[i]])
            dfs(v),low[u]=min(low[u],low[v]);
        else if(!id[v])low[u]=min(low[u],dfn[v]);
    if(dfn[u]==low[u])
    {
        id[u]=++num;
        while(q[--top]!=u)id[q[top]]=num;
    }
}
void tarjan()
{
    top=index=num=0;
    for(int i=0;i<=n;++i)id[i]=dfn[i]=0;
    for(int i=1;i<=n;++i)
        if(!dfn[i])dfs(i);
}
int main()
{
    freopen("schlnet.in","r",stdin);
    freopen("schlnet.out","w",stdout);
    while(~scanf("%d",&n))
    {
        ans1=ans2=0;
        for(edge=i=0;i<=n;++i)in[i]=out[i]=0,head[i]=-1;
        for(i=1;i<=n;++i)
            while(scanf("%d",&j),j)
                ver[edge]=j,next[edge]=head[i],head[i]=edge++;
        tarjan();
        for(i=1;i<=n;++i)
            for(j=head[i];j>=0;j=next[j])
                if(id[i]!=id[ver[j]])
                    ++in[id[ver[j]]],++out[id[i]];
        for(i=1;i<=num;++i)
            ans1+=(!in[i]),ans2+=(!out[i]);
        if(num<2)ans2=0;
        else ans2=max(ans1,ans2);
        printf("%d\n%d\n",ans1,ans2);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是P4087 [USACO17DEC]Milk Measurement的c++代码: ```c++ #include<bits/stdc++.h> using namespace std; int n,d,i,x,minn=1e9,maxn=-1e9,sum=7;//注意sum要初始化为7,因为一开始有三个人挤奶! map<int,int> mp; struct node{ int day,milk,id;//day表示某一天,milk表示这一天的产奶量,id表示这头牛的编号 }a[100010]; bool cmp(node x,node y){ return x.day<y.day; } int main(){ scanf("%d%d",&n,&d); for(i=1;i<=n;i++){ scanf("%d%d%d",&a[i].day,&a[i].id,&a[i].milk); minn=min(minn,a[i].id);//记录最小的牛的编号 maxn=max(maxn,a[i].id);//记录最大的牛的编号 } sort(a+1,a+n+1,cmp);//排序 for(i=1;i<=n;i++){ int p=a[i].id; mp[p]+=a[i].milk;//记录每头牛产奶总量 if(mp[p]-a[i].milk>=mp[minn]&&mp[p]>=mp[minn]){//如果这头牛的产奶总量减去这一天的产奶量后等于最小产奶量且这头牛的产奶总量大于等于最小产奶量 sum--; } if(mp[p]>=mp[maxn]&&mp[p]-a[i].milk<mp[maxn]){//如果这头牛的产奶总量大于等于最大产奶量且这头牛的产奶总量减去这一天的产奶量小于最大产奶量 sum++; } if(mp[p]-a[i].milk<mp[maxn]&&mp[p]>=mp[maxn]){//如果这头牛的产奶总量减去这一天的产奶量小于最大产奶量且这头牛的产奶总量大于等于最大产奶量 if(mp[maxn]-mp[p]+a[i].milk>0)sum++; } mp[p]-=a[i].milk;//减去这一天的产奶量 if(i==n||a[i].day!=a[i+1].day){//如果到了新的一天或者到了最后一天 if(mp[maxn]!=mp[a[i].id]&&mp[a[i].id]>=mp[maxn])sum++;//如果这头牛的产奶总量不等于最大产奶量且这头牛的产奶总量大于等于最大产奶量 if(mp[maxn]==mp[a[i].id]){//如果这头牛的产奶总量等于最大产奶量 if(a[i].id==maxn)sum+=0;//如果这头牛就是最大产奶量的牛,那么不需要增加计数器 else sum++;//否则需要增加计数器 } if(mp[minn]!=mp[a[i].id]&&mp[a[i].id]>=mp[minn])sum++;//如果这头牛的产奶总量不等于最小产奶量且这头牛的产奶总量大于等于最小产奶量 if(mp[minn]==mp[a[i].id]){ if(a[i].id==minn)sum+=0;//如果这头牛就是最小产奶量的牛,那么不需要增加计数器 else sum++;//否则需要增加计数器 } } } printf("%d\n",sum); return 0; } ``` 该题的解题思路是模拟,需要注意细节问题。我们可以首先将输入的数据按天数排序,然后模拟每一天挤奶的情况,并根据题目要求进行计数即可。具体细节请见代码注释。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值