[poj] 1236 networks of schools
2017-11-15 20:13
246 查看
原题
这是一道强连通分量板子题。
显然subtask1 是要输出入度为0的点的个数
而subtask2,我们考虑一下最优一定是把一个出度为零的点连到入度为零的点上,这样我们要输出的就是max(出度为零的个数,入度为零的个数)
另外,如果只有一个强连通分量,那么subtask2答案是0
#include<cstdio> #include<algorithm> #include<stack> #define N 10010 #define M 50010 using namespace std; int n,m,head ,dfn ,low ,cnt=1,t,sum,bel ,out ,num ,in ,ans,as; bool instk ; stack <int> stk; struct hhh { int to,next; }edge[M]; int read() { int ans=0,fu=1; char j=getchar(); for (;(j<'0' || j>'9') && j!='-';j=getchar()) ; if (j=='-') fu=-1,j=getchar(); for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0'; return ans*fu; } void add(int u,int v) { edge[cnt].to=v; edge[cnt].next=head[u]; head[u]=cnt++; } void Tarjan(int x) { dfn[x]=low[x]=++t; stk.push(x); instk[x]=1; int v; for (int i=head[x];i;i=edge[i].next) { v=edge[i].to; if (!dfn[v]) { Tarjan(v); low[x]=min(low[x],low[v]); } else if (instk[v]) low[x]=min(low[x],dfn[v]); } if (dfn[x]==low[x]) { sum++; do { v=stk.top(); stk.pop(); instk[v]=0; bel[v]=sum; num[sum]++; }while(v!=x); } } int main() { n=read(); for (int i=1,a;i<=n;i++) { a=read(); while(a!=0) { add(i,a); a=read(); } } for (int i=1;i<=n;i++) if (!dfn[i]) Tarjan(i); for (int i=1;i<=n;i++) for (int j=head[i],v;j;j=edge[j].next) { v=edge[j].to; if (bel[i]!=bel[v]) in[bel[v]]++,out[bel[i]]++; } for (int i=1;i<=sum;i++) { if (!in[i]) ans++; if (!out[i]) as++; } printf("%d\n",ans); if (sum==1) printf("0\n"); else printf("%d",max(ans,as)); return 0; }
相关文章推荐
- poj 1236 Network of Schools(Tarjan)
- [缩点] poj 1236 Network of Schools
- poj 1236 Network of Schools(tarjan 缩点)
- poj-1236 Network Of Schools【强连通缩点】
- POJ 1236 Network of Schools(强连通分量)
- poj 2186【Popular Cows】1236【Network of Schools】2553【The Bottom of a Graph】
- poj 1236 Network of Schools(强连通、缩点、出入度)
- Network of Schools - POJ 1236 Tarjan 缩点
- Network of Schools POJ - 1236(tarjan+缩点)
- POJ 1236--Network of Schools【scc缩点构图 && 求scc入度为0的个数 && 求最少加几条边使图变成强联通】
- poj 1236 Network of Schools
- POJ - 1236 Network of Schools(强连通分量)
- Network of Schools - POJ 1236 - 强连通分量
- POJ 1236 Network of Schools【强连通分量分解&&缩点||tarjan&&缩点】
- POJ 1236 Network of Schools(强连通分量)#by zh
- poj 1236 Network of Schools(连通图入度,出度为0)
- POJ-1236-Network of Schools(强连通图,找缩点,判断入度出度)
- POJ 1236 Network of Schools 有向图强连通分量
- POJ 1236 Network of Schools
- POJ1236——Network of Schools