使用标记数组,dfs前清空

vector<int>a[1005];
bitset<1005>f,emp;
void dfs(int x){f[x]=1;for(auto i:a[x]){if(!f[i])dfs(i);}
}f=emp,dfs(x);