poj1236 Network of Schools
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.
分析:先縮點,第一個問題是in[]為0的點的個數,第二個問題是max(Σin[],Σout[]);
證明:要形成強連通分量,那麼只要解決那些出度為0的或者入度為0的點,最好的方式是出度為0的點向入度為0的點連邊,但他們可能不會一一對應,所以取個max。
code:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<stack> using namespace std; const int maxn=1e5+10; const int INF=2e9; vector<int>E[maxn]; struct point{ int x,y,r,co; }boom[maxn]; int dfn[maxn],low[maxn],id,vis[maxn],ans,deg[maxn]; int num[maxn],cnt,cost[maxn],in[maxn],out[maxn]; stack<int>s; bool ju(point a,point b){ return (1ll*(a.x-b.x)*(a.x-b.x)+1ll*(a.y-b.y)*(a.y-b.y)<=1ll*a.r*a.r); } void init(){ id=cnt=0; memset(vis,0,sizeof(vis)); memset(dfn,0,sizeof(dfn)); memset(deg,0,sizeof(deg)); memset(num,0,sizeof(num)); } void tarjan(int u){ low[u]=dfn[u]=++id; s.push(u); vis[u]=1; for(int i=0;i<E[u].size();i++){ int v=E[u][i]; if(!dfn[v]){ 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]){ int minco=INF; cnt++; while(1){ int Top=s.top(); s.pop(); vis[Top]=0; num[Top]=cnt; if(Top==u)break; } } } int main(){ init(); int n; cin>>n; getchar(); for(int i=1;i<=n;i++){ while(1){ char ch=getchar(); while(!(ch>='0'&&ch<='9'))ch=getchar(); int numm=0; while (ch>='0'&&ch<='9'){ numm=numm*10+ch-'0'; ch=getchar(); } if(!numm)break; E[i].push_back(numm); if(ch=='\n')break; } } for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i); for(int i=1;i<=n;i++){ for(int j=0;j<E[i].size();j++){ int v=E[i][j]; if(num[i]!=num[v]){ ++out[num[i]]; ++in[num[v]]; } } } int aa=0,bb=0; for(int i=1;i<=cnt;i++){ aa+=in[i]==0; bb+=out[i]==0; } if(cnt==1)cout<<1<<endl<<0; else cout<<aa<<endl<<max(aa,bb); return 0; }