zoukankan      html  css  js  c++  java
  • POJ 1236 Network of Schools

    题目链接:https://vjudge.net/problem/POJ-1236

    转自:https://www.cnblogs.com/qldabiaoge/p/9073147.html

    题目大意

      一些学校连接到计算机网络。这些学校之间已经达成了协议:每所学校都有一份分发软件的学校名单(“接收学校”)。请注意,如果 B 在学校 A 的分发名单中,则 A 没必要出现在学校B的名单中。
      您需要编写一个计划,计算必须接收新软件副本的最少学校数量,以便软件根据协议(子任务 A)到达网络中的所有学校。作为进一步的任务,我们希望确保通过将新软件的副本发送到任意学校,该软件将覆盖网络中的所有学校。为了实现这一目标,我们可能需要扩大新成员的接收者名单。计算必须做的扩展的最小数目,以便我们发送新软件的任何学校,它将到达所有其他学校(子任务 B)。一种扩展意味着将一名新成员引入一所学校的接收者名单。

    分析

      Tarjan 算法划分 SCC 模板题。
      对于子任务 A,就是问有多少个 SCC 的入度为 0,因为一个入度为 0 的 SCC,必须收到一个软件副本,因为没有其他学校能发给这个 SCC。
      对于子任务 B,就是问至少添加多少条边,使得整个图强连通,也就是说,每个 SCC 必须既有入度,又有出度。出度为 0 的 SCC 必须增加一个出度;入度为 0 的 SCC 必须增加一个入度。答案就是 max(需要增加的出度数, 需要增加的入度数)。
      注意特判只有一个 SCC 的情况。

    代码如下

      1 #include <cmath>
      2 #include <ctime>
      3 #include <iostream>
      4 #include <string>
      5 #include <vector>
      6 #include <cstdio>
      7 #include <cstdlib>
      8 #include <cstring>
      9 #include <queue>
     10 #include <map>
     11 #include <set>
     12 #include <algorithm>
     13 #include <cctype>
     14 #include <stack>
     15 #include <deque>
     16 #include <list>
     17 #include <sstream>
     18 #include <cassert>
     19 using namespace std;
     20  
     21 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
     22 #define Rep(i,n) for (int i = 0; i < (n); ++i)
     23 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
     24 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
     25 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
     26 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
     27 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
     28 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
     29  
     30 #define pr(x) cout << #x << " = " << x << "  "
     31 #define prln(x) cout << #x << " = " << x << endl
     32  
     33 #define LOWBIT(x) ((x)&(-x))
     34  
     35 #define ALL(x) x.begin(),x.end()
     36 #define INS(x) inserter(x,x.begin())
     37 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
     38 #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c 
     39 #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
     40 #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
     41  
     42 #define ms0(a) memset(a,0,sizeof(a))
     43 #define msI(a) memset(a,inf,sizeof(a))
     44 #define msM(a) memset(a,-1,sizeof(a))
     45 
     46 #define MP make_pair
     47 #define PB push_back
     48 #define ft first
     49 #define sd second
     50  
     51 template<typename T1, typename T2>
     52 istream &operator>>(istream &in, pair<T1, T2> &p) {
     53     in >> p.first >> p.second;
     54     return in;
     55 }
     56  
     57 template<typename T>
     58 istream &operator>>(istream &in, vector<T> &v) {
     59     for (auto &x: v)
     60         in >> x;
     61     return in;
     62 }
     63  
     64 template<typename T1, typename T2>
     65 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
     66     out << "[" << p.first << ", " << p.second << "]" << "
    ";
     67     return out;
     68 }
     69 
     70 inline int gc(){
     71     static const int BUF = 1e7;
     72     static char buf[BUF], *bg = buf + BUF, *ed = bg;
     73     
     74     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
     75     return *bg++;
     76 } 
     77 
     78 inline int ri(){
     79     int x = 0, f = 1, c = gc();
     80     for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
     81     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
     82     return x*f;
     83 }
     84 
     85 template<class T>
     86 inline string toString(T x) {
     87     ostringstream sout;
     88     sout << x;
     89     return sout.str();
     90 }
     91 
     92 inline int toInt(string s) {
     93     int v;
     94     istringstream sin(s);
     95     sin >> v;
     96     return v;
     97 }
     98 
     99 //min <= aim <= max
    100 template<typename T>
    101 inline bool BETWEEN(const T aim, const T min, const T max) {
    102     return min <= aim && aim <= max;
    103 }
    104  
    105 typedef long long LL;
    106 typedef unsigned long long uLL;
    107 typedef pair< double, double > PDD;
    108 typedef pair< int, int > PII;
    109 typedef pair< int, PII > PIPII;
    110 typedef pair< string, int > PSI;
    111 typedef pair< int, PSI > PIPSI;
    112 typedef set< int > SI;
    113 typedef set< PII > SPII;
    114 typedef vector< int > VI;
    115 typedef vector< double > VD;
    116 typedef vector< VI > VVI;
    117 typedef vector< SI > VSI;
    118 typedef vector< PII > VPII;
    119 typedef map< int, int > MII;
    120 typedef map< LL, int > MLLI;
    121 typedef map< int, string > MIS;
    122 typedef map< int, PII > MIPII;
    123 typedef map< PII, int > MPIII;
    124 typedef map< string, int > MSI;
    125 typedef map< string, string > MSS;
    126 typedef map< PII, string > MPIIS;
    127 typedef map< PII, PII > MPIIPII;
    128 typedef multimap< int, int > MMII;
    129 typedef multimap< string, int > MMSI;
    130 //typedef unordered_map< int, int > uMII;
    131 typedef pair< LL, LL > PLL;
    132 typedef vector< LL > VL;
    133 typedef vector< VL > VVL;
    134 typedef priority_queue< int > PQIMax;
    135 typedef priority_queue< int, VI, greater< int > > PQIMin;
    136 const double EPS = 1e-8;
    137 const LL inf = 0x3fffffff;
    138 const LL infLL = 0x3fffffffffffffffLL;
    139 const LL mod = 20100713;
    140 const int maxN = 1e2 + 7;
    141 const LL ONE = 1;
    142 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
    143 const LL oddBits = 0x5555555555555555;
    144 
    145 struct Edge{
    146     int from, to;
    147     
    148     Edge() {}
    149     Edge(int x, int y) : from(x), to(y) {}
    150 };
    151 
    152 int N, ansA, ansB;
    153 VI V[maxN];
    154 vector< Edge > E;
    155 
    156 void addEdge(Edge &x) {
    157     V[x.from].PB(E.size());
    158     E.PB(x);
    159 }
    160 
    161 stack< int > sk; // 递归处理 SCC (强连通分量)
    162 bool insk[maxN]; // 是否在栈中 
    163 
    164 int scc[maxN], sccid; // 存每个点对应SCC的编号 
    165 int in[maxN], out[maxN]; // SCC的入度与出度 
    166 
    167 int Time;
    168 int tp[maxN]; // timestamp,时间戳
    169 int facr[maxN]; // The farthest ancestor that can be reached,每个节点最远的返回的祖先 
    170 // S :当前节点号
    171 // 划分SCC并缩点 
    172 void Tarjan(int S) {
    173     tp[S] = facr[S] = ++Time;
    174     sk.push(S);
    175     insk[S] = 1;
    176     
    177     Rep(i, V[S].size()) {
    178         Edge &e = E[V[S][i]];
    179         
    180         if(!tp[e.to]) {
    181             Tarjan(e.to);
    182             facr[S] = min(facr[S], facr[e.to]);
    183         }
    184         else if(insk[e.to]) facr[S] = min(facr[S], tp[e.to]); // 必须要保证在栈中,不然不能保证是一块SCC 
    185     }
    186     
    187     if(facr[S] == tp[S]) {
    188         ++sccid;
    189         while(!sk.empty()) {
    190             int tmp = sk.top(); sk.pop();
    191             
    192             insk[tmp] = 0;
    193             scc[tmp] = sccid;
    194             if(tmp == S) break;
    195         } 
    196     }
    197 }
    198 
    199 int main(){
    200     //freopen("MyOutput.txt","w",stdout);
    201     //freopen("input.txt","r",stdin);
    202     INIT();
    203     cin >> N;
    204     For(i, 1, N) {
    205         int x;
    206         while(cin >> x && x) {
    207             Edge e(i, x);
    208             addEdge(e);
    209         }
    210     }
    211     
    212     For(i, 1, N) if(!tp[i]) Tarjan(i);
    213     For(i, 1, N) {
    214         Rep(j, V[i].size()) {
    215             Edge &e = E[V[i][j]];
    216             
    217             if(scc[e.from] != scc[e.to]) { // 如果一条边所连接的两个点不属于同一个SCC 
    218                 ++in[scc[e.to]];
    219                 ++out[scc[e.from]];
    220             }
    221         } 
    222     }
    223     
    224     For(i, 1, sccid) {
    225         if(!in[i]) ++ansA;
    226         if(!out[i]) ++ansB;
    227     }
    228     ansB = max(ansA, ansB);
    229     if(sccid == 1) ansB = 0;
    230     
    231     cout << ansA << endl << ansB << endl;
    232     return 0;
    233 }
    View Code
  • 相关阅读:
    springboot学习入门简易版二---springboot2.0项目创建
    springboot学习入门简易版一---springboot2.0介绍
    mybatis学习系列一(mybatis简介/使用)
    mybatis学习系列--逆向工程简单使用及mybatis原理
    mybatis学习系列五--插件及类型处理器
    enum类使用
    mybatis学习系列四--mybatis generator逆向工程
    mybatis学习系列三(部分)
    常见开源协议及含义
    【maven】常见插件
  • 原文地址:https://www.cnblogs.com/zaq19970105/p/11261306.html
Copyright © 2011-2022 走看看