POJ 1236 Network of Schools - 缩点

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; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0); template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/
const int maxn = ;
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] = ;
for(int i=; 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] = ;
if(now == x)break;
}
}
}
int main(){
int n;
while(~scanf("%d", &n)){
for(int i=; i<=n; i++){
mp[i].clear();
dfn[i] = low[i] = vis[i] = col[i] = ;
in[i] = out[i] = ;
tot = cnt = ;
}
while(!S.empty())S.pop(); for(int i=; i<=n; i++){
int x;
while(scanf("%d", &x) && x){
mp[i].pb(x);
}
} for(int i=; i<=n; i++){
if(dfn[i] == ){
tarjan(i);
}
} for(int i=; i<=n; i++){
for(int j=; 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 = ,ans2 = ;
for(int i=; i<=cnt; i++){
if(in[i] == )ans1++;
if(out[i] == )ans2++;
}
if(cnt==)
printf("1\n0\n");
else printf("%d\n%d\n", ans1,max(ans1,ans2));
}
return ;
}

POJ 1236

上一篇:使用strace工具故障排查的5种简单方法


下一篇:Poj 1236 Network of Schools (Tarjan)