POJ 1236 Network of Schools【强连通分量分解&&缩点||tarjan&&缩点】
来源:互联网 发布:江南龙族大画集淘宝店 编辑:程序博客网 时间:2024/05/16 18:48
Network of Schools
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 14237 Accepted: 5675
Description
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.
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
The first line 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.
Output
Your program should write two lines to the standard output. The first line should contain one positive integer: the solution of subtask A. The second line should contain the solution of subtask B.
Sample Input
52 4 3 04 5 0001 0
Sample Output
12
恩,题目大意是,许多学校建立的计算机网络,要将文件传达到各个学校,首先问需要将文件发送给多少个学校才可达到目的。然后就是问要想随意向其中一个学校发送文件即可传达到所有学校还需要建立的计算机网络线路多少条。
恩,看了人家的分析,第一问是强连通分量分解缩点后入度为0的点的个数,第二问是缩点后入度为0的点的个数和出度为0的点的个数的最大值,如果当前图即为强连通的,那么直接输出1 0
#include <iostream>#include<cstdio>#include<cstring>#include<vector>#define maxn 110using namespace std;int n;bool vis[maxn];int rec[maxn];int in[maxn];int out[maxn];vector<int>g[maxn];vector<int>rg[maxn];vector<int>vs;void add(int f,int t){ g[f].push_back(t); rg[t].push_back(f);}void dfs(int v){ vis[v]=true; for(int i=0;i<g[v].size();++i) if(!vis[g[v][i]]) dfs(g[v][i]); vs.push_back(v);}void rdfs(int v,int k){ vis[v]=true; rec[v]=k; for(int i=0;i<rg[v].size();++i) if(!vis[rg[v][i]]) rdfs(rg[v][i],k);}int scc(){ vs.clear(); memset(rec,0,sizeof(rec)); memset(vis,false,sizeof(vis)); for(int i=1;i<=n;++i) if(!vis[i]) dfs(i); int k=0; memset(vis,false,sizeof(vis)); for(int i=vs.size()-1;i>=0;--i) if(!vis[vs[i]]) rdfs(vs[i],k++); return k;}int main(){ int a; while(~scanf("%d",&n)) { for(int i=0;i<=n;++i) { g[i].clear(); rg[i].clear(); } for(int i=1;i<=n;++i) { while(scanf("%d",&a)&&a) add(i,a); } int cnt=scc(); if(cnt==1) { printf("1\n0\n"); continue; } memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); for(int i=1;i<=n;++i)//缩点 { for(int j=0;j<g[i].size();++j) { if(rec[i]!=rec[g[i][j]])//不属于同一个强连通分量 out[rec[i]]++,in[rec[g[i][j]]]++; } } int cnt1=0,cnt2=0; for(int i=0;i<cnt;++i) { if(in[i]==0) cnt1++; if(out[i]==0) cnt2++; } int ans2=max(cnt1,cnt2); printf("%d\n%d\n",cnt1,ans2); } return 0;}
刚稍稍看的有点懂的tarjan算法,这个相当于是一次dfs,记录当前点的时间戳和它的孩子所能到达的最先遍历的点,若这两个值相等则证明它是该强连通分量的根。
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<stack>#define maxn 110using namespace std;int n;vector<int>g[maxn];stack<int>sta;bool ins[maxn];int id[maxn];int low[maxn],dfn[maxn];int in[maxn],out[maxn];int scnt,idx;void tarjan(int v){ dfn[v]=low[v]=++idx; sta.push(v); ins[v]=true; for(int i=0;i<g[v].size();++i) { if(!dfn[g[v][i]]) { tarjan(g[v][i]); low[v]=min(low[v],low[g[v][i]]); } else if(ins[g[v][i]]&&dfn[g[v][i]]<low[v]) low[v]=dfn[g[v][i]]; } if(low[v]==dfn[v]) { ++scnt; while(sta.top()!=v) { id[sta.top()]=scnt; ins[sta.top()]=false; sta.pop(); } id[v]=scnt; ins[v]=false; sta.pop(); }}int main(){ int a; while(~scanf("%d",&n)) { for(int i=0;i<=n;++i) g[i].clear(); while(!sta.empty()) sta.pop(); memset(ins,false,sizeof(ins)); memset(low,0,sizeof(low)); memset(dfn,0,sizeof(dfn)); memset(id,0,sizeof(id)); for(int i=1;i<=n;++i) { while(scanf("%d",&a)&&a) { g[i].push_back(a); } } scnt=0; idx=0; for(int i=1;i<=n;++i) if(!dfn[i]) tarjan(i); if(scnt==1) { printf("1\n0\n"); continue; } memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); for(int i=1;i<=n;++i) { for(int j=0;j<g[i].size();++j) { if(id[i]!=id[g[i][j]]) out[id[i]]++,in[id[g[i][j]]]++; } } int cnt1=0,cnt2=0; for(int i=1;i<=scnt;++i) { if(in[i]==0) cnt1++; if(out[i]==0) cnt2++; } int ans2=max(cnt1,cnt2); printf("%d\n%d\n",cnt1,ans2); } return 0;}
0 0
- POJ 1236 Network of Schools【强连通分量分解&&缩点||tarjan&&缩点】
- POJ 1236 Network of Schools强连通分量缩点
- POJ 1236 Network of Schools (强连通分量+缩点)
- POJ 1236 Network of Schools 强连通分量+缩点
- poj 1236 Network of Schools (强连通分量+缩点)
- poj 1236 Network of Schools(强连通分量+缩点)
- poj 1236 Network of Schools(强连通分量缩点)
- POJ-1236-Network of Schools【强连通分量】【缩点】
- POJ 1236 Network of Schools(强连通分量,缩点)
- POJ 1236 Network of Schools(强连通分量缩点+tarjan算法)
- 强连通分量,Tarjan,缩点(Network of Schools,POJ 1236)
- POJ 1236 Network of Schools (Tarjan算法求强连通分量+缩点) 代码详解
- POJ 1236 Network of Schools HDU 3836 Equivalent Sets 强连通分量+缩点 tarjan or kosaraju
- 【连通图|强连通分量+缩点】POJ-1236 Network of Schools
- POJ 1236 Network of Schools (强连通分量,块,缩点)
- poj 1236 Network of Schools (强连通分量+缩点)
- POJ 1236 Network of Schools(强连通分量缩点)
- POJ 1236 Network of Schools (强连通分量、缩点)
- 深入理解HTTP Session
- Android粒子效果之雨
- 别以为真懂Openstack: 虚拟机创建的50个步骤和100个知识点(1)
- VBA教程初级(一):简单宏
- 串口编程 tcflush()函数
- POJ 1236 Network of Schools【强连通分量分解&&缩点||tarjan&&缩点】
- JavaScript 闭包
- 用MATLAB處理grib2數據
- LeetCode第70题 Climbing Stairs
- freetype2.4.6在iOS上各版本的编译脚本
- Java进阶07 嵌套类
- 浏览器实现图片100%,而且保证不变形
- 转换说明符(%s、%u、%c.....)
- 5.各类学习资源通道汇总