BZOJ 2208: [Jsoi2010]连通数

  BZOJ的题目,就是高大上。

  然而是一道入门(稍微难一点)的水题。

  先读题,发现可以tarjan,对,这很适合tarjan。

  然后看数据范围,这不就是。。。暴力枚举+BFS找点。

  水题不做多解释。

  CODE

#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int N=2005;
vector <int> a[N];
int ans,q[N*2+10],i,j,n;
bool f[N];
inline void read(int &x)
{
    x=0; char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
}
inline int bfs(int x)
{
    memset(f,true,sizeof(f));
    int head=0,tail=1,res=1;
    f[x]=0; q[1]=x;
    while (head<tail)
    {
        int now=q[++head];
        for (int i=0;i<a[now].size();++i)
        {
            int k=a[now][i];
            if (f[k])
            {
                f[k]=0;
                q[++tail]=k;
                res++;
            }
        }
    }
    return res;
}
int main()
{
    read(n);
    for (i=1;i<=n;++i)
    {
        for (j=1;j<=n;++j)
        if (getchar()=='1') a[i].push_back(j);
        getchar();
    }
    for (i=1;i<=n;++i)
    ans+=bfs(i);
    printf("%d",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/cjjsb/p/8042613.html