1. 程式人生 > >POJ 1236 Network of Schools - 縮點

POJ 1236 Network of Schools - 縮點

reat include can fine mod esp linker empty n)

POJ 1236 :http://poj.org/problem?id=1236

參考:https://www.cnblogs.com/TnT2333333/p/6875680.html

題意:

  有好多學校,每個學校可以給其他特定的學校發送文件。第一個問題是最少要給幾個學校發文件,可以使得全部的學校收到文件。第二個問題是最少要加幾條線路,使得隨意挑一個學校發文件,也能使得全部的學校收到文件。

思路:

  第一個問題,可以用tarjan給圖中先縮點,因為強連通的環相互可達。所以只要數出縮完點後圖中入度為0的點的個數。第二個問題,可以這麽考慮,縮完點後的圖中有c1個入度為0的點,有c2個出度為0的點。把入度為0的點和出度為0的點盡量匹配,剩下的就向連通圖中連一條邊即可,所以第二個問題的答案就是max(c1,c2)。

技術分享圖片
/*
* @Author: chenkexing
* @Date:   2018-09-05 11:05:14
* @Last Modified by:   chenkexing
* @Last Modified time: 2018-09-07 20:25:39
*/
#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    
<bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include
<cassert> using namespace std; //#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//這是一個大根堆q //priority_queue<int,vector<int>,greater<int> >q;//這是一個小根堆q #define fi first #define se second //#define endl ‘\n‘ #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用來壓行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; const double PI=acos(-1.0); template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<0||ch>9) f|=(ch==-),ch=getchar(); while (ch>=0&&ch<=9) x=x*10+ch-0,ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ const int maxn = 400; vector<int>mp[maxn]; int dfn[maxn],low[maxn],vis[maxn],col[maxn]; int in[maxn],out[maxn]; int tot,cnt; stack<int>S; void tarjan(int x){ low[x] = dfn[x] = ++tot; S.push(x);vis[x] = 1; for(int i=0; i<mp[x].size(); i++){ int v = mp[x][i]; if(!dfn[v]){ tarjan(v); low[x] = min(low[x],low[v]); } else if(vis[v]){ low[x] = min(low[x], dfn[v]); } } if(low[x] == dfn[x]){ cnt++; while(true){ int now = S.top(); S.pop(); col[now] = cnt; vis[now] = 0; if(now == x)break; } } } int main(){ int n; while(~scanf("%d", &n)){ for(int i=1; i<=n; i++){ mp[i].clear(); dfn[i] = low[i] = vis[i] = col[i] = 0; in[i] = out[i] = 0; tot = cnt = 0; } while(!S.empty())S.pop(); for(int i=1; i<=n; i++){ int x; while(scanf("%d", &x) && x){ mp[i].pb(x); } } for(int i=1; i<=n; i++){ if(dfn[i] == 0){ tarjan(i); } } for(int i=1; i<=n; i++){ for(int j=0; j<mp[i].size(); j++){ int u = i,v = mp[i][j]; if(col[u] != col[v]){ out[col[u]]++; in[col[v]]++; } } } // debug(cnt); int ans1 = 0,ans2 = 0; for(int i=1; i<=cnt; i++){ if(in[i] == 0)ans1++; if(out[i] == 0)ans2++; } if(cnt==1) printf("1\n0\n"); else printf("%d\n%d\n", ans1,max(ans1,ans2)); } return 0; }
POJ 1236

POJ 1236 Network of Schools - 縮點