bzoj2208: [Jsoi2010]连通数(dfs)

2208: [Jsoi2010]连通数

题目:传送门 

题解:

   强烈吐槽(表示自己想复杂了)

   看到这个破题就直接想强联通,错了很久看一波路牌,发现没错,但是要bitset(蒟蒻不会啊)

   水了个dfs准备等下对拍....tmdA了!!!

   不想多说...

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 struct node
 8 {
 9     int x,y,next;
10 }a[4100000];
11 int len,last[410000];
12 void ins(int x,int y)
13 {
14     len++;
15     a[len].x=x;a[len].y=y;
16     a[len].next=last[x];last[x]=len;
17 }
18 bool v[2100];
19 char st[2100];
20 void dfs(int x)
21 {
22     v[x]=1;
23     for(int k=last[x];k;k=a[k].next)
24         if(v[a[k].y]==false)dfs(a[k].y);
25 }
26 int main()
27 {
28     int n;
29     scanf("%d",&n);
30     len=0;memset(last,0,sizeof(last));
31     for(int i=1;i<=n;i++)
32     {
33         scanf("%s",st+1);
34         for(int j=1;j<=n;j++)
35             if(st[j]=='1')
36                 ins(i,j);
37     }
38     int ans=0;
39     for(int i=1;i<=n;i++)
40     {
41         memset(v,false,sizeof(v));dfs(i);
42         for(int j=1;j<=n;j++)ans+=v[j];
43     }
44     printf("%d\n",ans);
45     return 0;
46 }
posted @ 2018-03-03 16:34  CHerish_OI  阅读(118)  评论(0编辑  收藏  举报