poj 1236 Network of Schools

题目链接:http://poj.org/problem?id=1236

题目大意:在有向图中求两个问题:
(1)有一个新软件时,至少需要向几个学校发送才能保证全部学校都能收到.
(2)至少需要添加几个名单才能使如果有一个新软件出现在某一学校中,所有学校都能收到.

题目思路:强连通的模板题.

PS:要注意(1),这个新软件一开始并不再某学校中.

代码:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define ull unsigned __int64
#define ll __int64
//#define ull unsigned long long
//#define ll long long
#define son1 New(p.xl,xm,p.yl,ym),(rt<<2)-2
#define son2 New(p.xl,xm,min(ym+1,p.yr),p.yr),(rt<<2)-1
#define son3 New(min(xm+1,p.xr),p.xr,p.yl,ym),rt<<2
#define son4 New(min(xm+1,p.xr),p.xr,min(ym+1,p.yr),p.yr),rt<<2|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define middle (l+r)>>1
#define MOD 1000000007
#define esp (1e-8)
const int INF=0x3F3F3F3F;
const double DINF=10000.00;
//const double pi=acos(-1.0);
const int M=20010;
int min(int x,int y){return x<y? x:y;}
int max(int x,int y){return x>y? x:y;}
void swap(int x,int y){int t=x;x=y;y=t;}

int m;
struct pay{
	int num,id;
	bool operator < (const pay &p) const{
		return num < p.num;
	}
}p[M];

struct node{
	int v,next;
}edge[M];
int init[M];
int low[M],pre[M],Index;
int idx[M],ss[M],top;
int _out[M],_in[M];
int n,nn,len;

void dfs(int u){
	low[u]=pre[u]=++Index;
	ss[++top]=u;
	int i,v;
	for(i=init[u];i!=-1;i=edge[i].next){
		v=edge[i].v;
		if(pre[v]==-1) dfs(v),low[u]=min(low[u],low[v]);
		else if(idx[v]==-1) low[u]=min(low[u],pre[v]);
	}
	if(pre[u]==low[u]){
		v=-1,nn++;
		while(u!=v) v=ss[top--],idx[v]=nn;
	}
}

void Tarjan(){
	memset(idx,-1,sizeof(int)*(n+1));
	memset(pre,-1,sizeof(int)*(n+1));
	Index=top=nn=0,ss[0]=-1;
	for(int u=1;u<=n;u++) if(pre[u]==-1) dfs(u);
}

void get_deg(){
	memset(_in,0,sizeof(int)*(nn+1));
	memset(_out,0,sizeof(int)*(nn+1));
	for(int u=1;u<=n;u++){
		for(int i=init[u];i!=-1;i=edge[i].next){
			int v=edge[i].v;
			if(idx[u]!=idx[v]) _in[idx[v]]++,_out[idx[u]]++;
		}
	}
}

void _init(){
	memset(init,-1,sizeof(int)*(n+1));
	int i,v;
	for(i=1,m=1;i<=n;i++){
		while(scanf("%d",&v) && v)
			edge[m].next=init[i],init[i]=m,edge[m].v=v,m++;
	}
}

void _sof(){
	Tarjan();get_deg();
	int in_c=0,out_c=0;
	for(int u=1;u<=nn;u++){
		if(!_in[u]) in_c++;
		if(!_out[u]) out_c++;
	}
	printf("%d\n",in_c);
	printf("%d\n",nn==1? 0:max(in_c,out_c));
}

int main(){
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	//_init(),_sof();
	//int T,cas;scanf("%d",&T);for(cas=1;cas<=T;cas++) _init(),_sof();
	while(~scanf("%d",&n)) _init(),_sof();
	return 0;
}


你可能感兴趣的:(poj 1236 Network of Schools)