analysis
模版。。。。。
code
#include<bits/stdc++.h>
using namespace std;
#define loop(i,start,end) for(register int i=start;i<=end;++i)
#define anti_loop(i,start,end) for(register int i=start;i>=end;--i)
#define clean(arry,num) memset(arry,num,sizeof(arry))
#define max(a,b) ((a>b)?a:b)
#define min(a,b) ((a<b)?a:b)
#define ll long long
template<typename T>void read(T &x){
x=0;char r=getchar();T neg=1;
while(r>'9'||r<'0'){if(r=='-')neg=-1;r=getchar();}
while(r>='0'&&r<='9'){x=(x<<1)+(x<<3)+r-'0';r=getchar();}
x*=neg;
}
const int maxn=1100;
const int maxm=maxn*maxn;
int n,cnt=0,m=0;
struct node{int _v;int nxt;}edge[maxm];
struct e{int _s;int _e;}line[maxm];
int head[maxn];
inline void addl(int u,int v){
edge[cnt]._v=v;
edge[cnt].nxt=head[u];
head[u]=cnt++;
}
int dfn[maxn],low[maxn];
int sta[maxn],top=0,belong[maxn];
int num=0,col=0;
void tarjan(int u){
dfn[u]=low[u]=++num;sta[++top]=u;
for(register int i=head[u];i!=-1;i=edge[i].nxt){
int v=edge[i]._v;
if(!dfn[v]){
tarjan(v);
low[u]=min(low[u],low[v]);
}else if(!belong[v])low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]){
belong[u]=++col;
while(top>=0&&sta[top]!=u){
belong[sta[top]]=col;
--top;
}
--top;
}
}
int in[maxn],out[maxn];
inline bool cmp(e a,e b){return ((a._s!=b._s)?a._s<b._s:a._e<b._e);}
inline void reset(){
loop(i,1,m){
line[i]._e=belong[line[i]._e];
line[i]._s=belong[line[i]._s];
}sort(line+1,line+1+m,cmp);
}
inline void rebuild(){
clean(head,-1),cnt=0;reset();clean(in,0),clean(out,0);
loop(i,1,m){
if(line[i]._s!=line[i]._e&&(line[i]._s!=line[i-1]._s||line[i]._e!=line[i-1]._e))
addl(line[i]._s,line[i]._e),++in[line[i]._e],++out[line[i]._s];
}
}
int con[maxn][maxn];
int main(){
#ifndef ONLINE_JUDGE
freopen("datain.txt","r",stdin);
//freopen("dataout.txt","w",stdout);
#endif // ONLINE_JUDGE
clean(head,-1);read(n);clean(dfn,0),clean(low,0);
clean(belong,0);clean(sta,0);clean(con,0);
register int v;
loop(i,1,n){
loop(j,1,n){
read(con[i][j]);
if(con[i][j])line[++m]._s=i,line[m]._e=j,addl(i,j);
}
}
loop(i,1,n)
if(!dfn[i])tarjan(i);
rebuild();
register int sum=0;
loop(i,1,col)
if(!in[i])++sum;
printf("%d\n",sum);
return 0;
}