【图论】Network of Schools
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 18969 | Accepted: 7467 |
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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=1001;
const int INF=999999;
int N,M;
vector<int> vec[MAXN];
int dfn[MAXN],low[MAXN],tar[MAXN];
int que[MAXN];
bool inq[MAXN];
int tmp,Col,tot;
int ind[MAXN],oud[MAXN]; void Tarjan(int x){
que[++tmp]=x;
++tot; dfn[x]=low[x]=tot;
inq[x]=true;
for(int i=0;i<vec[x].size();i++){
int to=vec[x][i];
if(!dfn[to]){
Tarjan(to);
low[x]=min(low[x],low[to]);
}
else if(inq[to]) low[x]=min(low[x],dfn[to]);
}
if(low[x]==dfn[x]){
++Col;
tar[x]=Col;
inq[x]=false;
while(que[tmp]!=x){
int k=que[tmp];
tar[k]=Col;
inq[k]=false;
tmp--;
}
tmp--;
}
return ;
}
int ans1,ans2;
int main(){
N=read();
for(int i=1;i<=N;i++){
int v=read();
while(v!=0){
vec[i].push_back(v);
v=read();
}
}
for(int i=1;i<=N;i++) if(!dfn[i]) Tarjan(i);
for(int i=1;i<=N;i++){
for(int j=0;j<vec[i].size();j++){
if(tar[i]!=tar[vec[i][j]]){
ind[tar[vec[i][j]]]++;
oud[tar[i]]++;
}
}
}
if(Col==1){
puts("1\n0");
return 0;
}
for(int i=1;i<=Col;i++)
if(!ind[i]) ans1++;
for(int i=1;i<=Col;i++)
if(!oud[i]) ans2++;
printf("%d\n%d\n",ans1,max(ans1,ans2));
}
【图论】Network of Schools的更多相关文章
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- Network of Schools --POJ1236 Tarjan
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Description A number of schools are conne ...
- [强连通分量] POJ 1236 Network of Schools
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16803 Accepted: 66 ...
- POJ1236 - Network of Schools tarjan
Network of Schools Time Limit: 1000MS Memory Limi ...
- POJ 1236 Network of Schools (Tarjan + 缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12240 Accepted: 48 ...
- POJ1236 Network of Schools (强连通)(缩点)
Network of Schools Time Limit: 1000MS ...
- POJ 1236 Network of Schools (有向图的强连通分量)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9073 Accepted: 359 ...
- poj 1236 Network of Schools(连通图入度,出度为0)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- poj 1236 Network of Schools(又是强连通分量+缩点)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
随机推荐
- 【转载】Lua中实现类的原理
原文地址 http://wuzhiwei.net/lua_make_class/ 不错,将metatable讲的很透彻,我终于懂了. --------------------------------- ...
- C++获取系统时间的方法
//首先是了解这个结构体,_SYSTEMTIME ,然后通过系统函数GetLocalTime往这个结构体的变量中写入当前系统时间typedef struct _SYSTEMTIME { WORD wY ...
- python urllib2练习发送简单post
import urllib2 import urllib url = 'http://localhost/1.php' while True: data = raw_input('(ctrl+c ex ...
- map,set的底层实现:红黑树[多图,手机慎入]
最近天下有一种颇不太平的感觉,各地的乱刀砍人,到处是贪官服法.京东准备上市了,阿里最近也提交申请了,猎豹也逆袭了,据说猎豹移动在国际市场上表现甚是抢眼.只有屌丝还在写着代码.花开花又谢,花谢花又开,为 ...
- Linux 内核通知链随笔【中】【转】
转自:http://blog.chinaunix.net/uid-23069658-id-4364171.html 关于内核通知链不像Netlink那样,既可以用于内核与用户空间的通信,还能用于内核不 ...
- java.lang.ClassCastException: org.springframework.web.filter.CharacterEncodingFilter cannot be cast
严重: Exception starting filter encodingFilterjava.lang.ClassCastException: org.springframework.web.fi ...
- maven添加jar包到本地仓库
mvn install:install-file -Dfile=desutill.jar -DgroupId=com.bfd -DartifactId=des -Dversion=1.0 -Dpack ...
- Django_admin源码流程
admin.py from django.contrib import admin from . import models """ 通过原生的django admin来 ...
- [前端随笔][JavaScript][自制数据可视化] “中国地图”
说在前面 想自己实现一个可视化的中国地图(可以实现如用户来源省份数据统计功能),网上搜了一下,翻了几页几乎都是第三方库(如echarts.js)实现的,简直不能忍. 不是第三方库不好,只是要花时间去适 ...
- Go语言练习之方法,接口,并发
多练练,有感觉了就写实际的东东. package main import ( "fmt" "math" "os" "time&qu ...