MacKenlly's blog

By MacKenlly, history, 8 years ago, In Russian

Не давно я наткнулся на эту задачу И уже довольно долгое время ломаю голову, почему и где программка зацикливается. Я надеюсь, вы — как опытные гуру, сможете мне помочь...


#include <fstream> #include <string.h> #include<string> #include <iostream> #include <cstring> #include <algorithm> #include <map> #include<cstdio> #include<vector> #include <string> using namespace std; int cat[1000]={0}; bool color[5000]={0}; int a[5000][5000]={0},n,k,c=0; int dfs(int i) { bool f=true; color[i]=1; if (cat[i]<=k) {for (int j=1;j<=n;j++) { if (color[j]==0&&a[i][j]==1) { f=false;if (cat[j]!=0) cat[j]+=cat[i];dfs(j);}} if (f) c++;} return 0; } int main() { cin>>n>>k; for (int i=1; i<=n; i++) cin>>cat[i]; for (int j=1; j<n;j++) {int x,y; cin>>x>>y; a[x][y]=1;a[y][x]=1;} dfs(1); cout<<c; return 0; }

Заранее спасибо)

  • Vote: I like it
  • -13
  • Vote: I do not like it