#include <cstdio> #include <cstring> #include <cstdlib> using namespace std; const int N=2001; struct G { int v,nxt; }map[N*N]; int tt,hd[N]; int n,v[N],res; void dfs(int now) { v[now]=1; res++; for (int k=hd[now];k;k=map[k].nxt) if (!v[map[k].v]) dfs(map[k].v); } inline void ins(int u,int v) { map[++tt].v=v; map[tt].nxt=hd[u]; hd[u]=tt; } int main(void) { char c; scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("\n"); for (int j=1;j<=n;j++) { scanf("%c",&c); if (c=='1') ins(i,j); } } for (int i=1;i<=n;i++) { memset(v,0,sizeof v); dfs(i); } printf("%d\n",res); return 0; }
/************************************************************** Problem: 2208 User: y20070316 Language: C++ Result: Accepted Time:2636 ms Memory:73012 kb ****************************************************************/ #include <cstdio> #include <cstring> #include <cstdlib> #include <bitset> using namespace std; const int N=2140; struct G { int v,nxt; }map[N*N]; int hd[N],tt; //Graph int n; //Basic int dfn[N],pre[N],v[N],ct; int stk[N],color[N],cnt[N],cc; //Tarjan G mapc[N*N]; int hdc[N],tc; //SCC Graph bitset <N> b[N]; int res,f[N]; int q[N],h,t,d[N]; //Topo+dp inline void ins(int u,int v) { map[++tt].v=v; map[tt].nxt=hd[u]; hd[u]=tt; } void init(void) { char c; scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("\n"); for (int j=1;j<=n;j++) { scanf("%c",&c); if (c=='1') ins(i,j); } } } inline int min(int i,int j) { return i<j?i:j; } void tarjan(int now) { pre[now]=dfn[now]=++ct; stk[++stk[0]]=now; v[now]=1; for (int k=hd[now];k;k=map[k].nxt) { if (v[map[k].v]==2) continue; if (v[map[k].v]==1) { dfn[now]=min(dfn[now],pre[map[k].v]); continue; } tarjan(map[k].v); dfn[now]=min(dfn[now],dfn[map[k].v]); } if (pre[now]==dfn[now]) { cc++; for (;stk[stk[0]]^now;stk[0]--) { color[stk[stk[0]]]=cc; v[stk[stk[0]]]=2; cnt[cc]++; b[cc][stk[stk[0]]]=1; stk[stk[0]]=0; } color[stk[stk[0]]]=cc; v[stk[stk[0]]]=2; b[cc][stk[stk[0]]]=1; cnt[cc]++; stk[stk[0]--]=0; } } inline void insc(int u,int v) { mapc[++tc].v=v; mapc[tc].nxt=hdc[u]; hdc[u]=tc; } void build(void) { for (int i=1;i<=n;i++) if (!v[i]) tarjan(i); for (int i=1;i<=n;i++) for (int k=hd[i];k;k=map[k].nxt) if (color[i]^color[map[k].v]) { d[color[map[k].v]]++; insc(color[i],color[map[k].v]); } } void topo(void) { int now; for (int i=1;i<=cc;i++) if (!d[i]) q[++t]=i; for (;h^t;) { now=q[++h]; for (int k=hdc[now];k;k=mapc[k].nxt) { d[mapc[k].v]--; if (!d[mapc[k].v]) q[++t]=mapc[k].v; } } bitset<N> tmp; for (int i=cc;i;i--) { tmp.reset(); res+=cnt[q[i]]*cnt[q[i]]; for (int k=hdc[q[i]];k;k=mapc[k].nxt) tmp|=b[mapc[k].v]; res+=cnt[q[i]]*tmp.count(); b[q[i]]|=tmp; } printf("%d\n",res); /* for (int i=1;i<=cc;i++) b[i][i]=true; for (int i=1;i<=cc;i++) { res+=cnt[i]*cnt[i]; bitset<N> tmp; for (int j=1;j<=n;j++) if (color[j]==i) for (int k=hd[j];k;k=map[k].nxt) tmp|=b[map[k].v]; res+=cnt[i]*tmp.count(); b[i]|=tmp; } printf("%d\n",res); */ } int main(void) { init(); build(); topo(); return 0; }
#include <cstdio> #include <cstring> #include <cstdlib> #include <bitset> using namespace std; const int N=2140; struct G { int v,nxt; }map[N*N]; int hd[N],tt; //Graph int n; //Basic int dfn[N],pre[N],v[N],ct; int stk[N],color[N],cnt[N],cc; //Tarjan G mapc[N*N]; int hdc[N],tc; //SCC Graph bitset <N> b[N]; int res,f[N]; int q[N],h,t,d[N]; //Topo+dp inline void ins(int u,int v) { map[++tt].v=v; map[tt].nxt=hd[u]; hd[u]=tt; } void init(void) { char c; scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("\n"); for (int j=1;j<=n;j++) { scanf("%c",&c); if (c=='1') ins(i,j); } } } inline int min(int i,int j) { return i<j?i:j; } void tarjan(int now) { pre[now]=dfn[now]=++ct; stk[++stk[0]]=now; v[now]=1; for (int k=hd[now];k;k=map[k].nxt) { if (v[map[k].v]==2) continue; if (v[map[k].v]==1) { dfn[now]=min(dfn[now],pre[map[k].v]); continue; } tarjan(map[k].v); dfn[now]=min(dfn[now],dfn[map[k].v]); } if (pre[now]==dfn[now]) { cc++; for (;stk[stk[0]]^now;stk[0]--) { color[stk[stk[0]]]=cc; v[stk[stk[0]]]=2; cnt[cc]++; b[cc][stk[stk[0]]]=1; stk[stk[0]]=0; } color[stk[stk[0]]]=cc; v[stk[stk[0]]]=2; b[cc][stk[stk[0]]]=1; cnt[cc]++; stk[stk[0]--]=0; } } inline void insc(int u,int v) { mapc[++tc].v=v; mapc[tc].nxt=hdc[u]; hdc[u]=tc; } void build(void) { for (int i=1;i<=n;i++) if (!v[i]) tarjan(i); for (int i=1;i<=n;i++) for (int k=hd[i];k;k=map[k].nxt) if (color[i]^color[map[k].v]) { d[color[map[k].v]]++; insc(color[i],color[map[k].v]); } } void topo(void) { bitset<N> tmp; for (int i=1;i<=cc;i++) { res+=cnt[i]*cnt[i]; tmp.reset(); for (int k=hdc[i];k;k=mapc[k].nxt) tmp|=b[mapc[k].v]; res+=cnt[i]*tmp.count(); b[i]|=tmp; } printf("%d\n",res); } int main(void) { init(); build(); topo(); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u013598409/article/details/47037499