1. 程式人生 > >P2746 [USACO5.3]校園網Network of Schools tarjan 縮點

P2746 [USACO5.3]校園網Network of Schools tarjan 縮點

urn cond 一個 tac net std cli prior sta

題意

給出一個有向圖,
A任務:求最少需要從幾個點送入信息,使得信息可以通過有向圖走遍每一個點
B任務:求最少需要加入幾條邊,使得有向圖是一個強聯通分量

思路

任務A,比較好想,可以通過tarjan縮點,求出入度為0的點的個數
任務B
一開始以為任務A,B沒有關系
其實是入度為0的點的個數、出度為0的點的個數的最大值。
因為任務B要求在任意學校投放軟件使得所有學校都能收到,所以很明顯是需要整張圖形成一個環,而環中所有節點入度和出度都不為0,所以需要把所有入度和出度的點度數增加。

(註意判斷本身就全聯通的情況

技術分享圖片
#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> /* ⊂_ヽ   \\ Λ_Λ 來了老弟    \(‘?‘)     > ⌒ヽ    /   へ\    /  / \\    ? ノ   ヽ_つ   / /   / /|  ( (ヽ  | |、\  | 丿 \ ⌒)  | |  ) / ‘ノ )  L? */ using
namespace std; #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 __int128 bll; 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 boost ios::sync_with_stdio(false);cin.tie(0) #define rep(a, b, c) for(int a = (b); a <= (c); ++ a) #define max3(a,b,c) max(max(a,b), c); #define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<17; 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); const double PHI=0.61803399; //黃金分割點 const double tPHI=0.38196601; 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; } inline void cmax(int &x,int y){if(x<y)x=y;} inline void cmax(ll &x,ll y){if(x<y)x=y;} inline void cmin(int &x,int y){if(x>y)x=y;} inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = 109; vector<int>mp1[maxn],mp2[maxn]; int dfn[maxn],low[maxn],vis[maxn],col[maxn]; int dp[maxn][maxn], in[maxn]; stack<int>st; int tot = 0,nn = 0; void tarjan(int u){ dfn[u] = low[u] = ++tot; st.push(u); vis[u] = 1; for(int i=0; i<mp1[u].size(); i++){ int v = mp1[u][i]; if(dfn[v] == 0){ tarjan(v); low[u] = min(low[u], low[v]); } else if(vis[v]){ low[u] = min(low[u], dfn[v]); } } if(low[u] == dfn[u]){ nn++; while(!st.empty()){ int x = st.top(); st.pop(); col[x] = nn; vis[x] = 0; if(x == u) break; } } } int main(){ int n; scanf("%d", &n); rep(i, 1, n){ int x; while(~scanf("%d", &x) && x) mp1[i].pb(x); } rep(i, 1, n) if(!dfn[i]) tarjan(i); rep(i, 1, n) { int u = col[i]; for(int j=0; j<mp1[i].size(); j++){ int v = col[mp1[i][j]]; dp[u][v] = 1; } } rep(i, 1, nn) { rep(j, 1, nn) { if(i == j) continue; if(dp[i][j]) mp2[i].pb(j), in[j]++; } } int ansa = 0,c1=0,c2=0; rep(i, 1, nn) { if(in[i] == 0) ansa++,c1++; if(mp2[i].size() == 0) c2 ++; } if(nn == 1) c1 = c2 = 0; printf("%d\n%d\n", ansa,max(c1, c2)); return 0; }
View Code

P2746 [USACO5.3]校園網Network of Schools tarjan 縮點