Submission #3606076


Source Code Expand

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#define MOD 1000000007
typedef long long ll;
using namespace std;

int n,x;
int h[110];
vector<int> v[110];
bool used[110];

int dfs(int now){

  //cout<<now<<" "<<d<<endl;
  used[now]=1;
  int tmp=0;
  //if(h[now]==1) tmp=d;
  for(int i=0;i<v[now].size();i++){
    int u=v[now][i];
    if(used[u]==0) tmp+=dfs(u);
  }

  if(tmp>0) return tmp+1;
  else if(h[now]==1) return 1;
  else return 0;
}

int main(){
  cin>>n>>x;
  x--;
  for(int i=0;i<n;i++) cin>>h[i];
  for(int i=1;i<n;i++){
    int a,b;
    cin>>a>>b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }

  cout<<max(0,2*(dfs(x)-1))<<endl;


  return 0;
}

Submission Info

Submission Time
Task B - ツリーグラフ
User snow39
Language C++14 (GCC 5.4.1)
Score 100
Code Size 831 Byte
Status AC
Exec Time 4 ms
Memory 384 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 2
AC × 20
Set Name Test Cases
Sample subtask0_sample_01.txt, subtask0_sample_02.txt
All subtask0_sample_01.txt, subtask0_sample_02.txt, subtask1_line01.txt, subtask1_line02.txt, subtask1_line03.txt, subtask1_line04.txt, subtask1_line05.txt, subtask1_line06.txt, subtask1_random01.txt, subtask1_random02.txt, subtask1_random03.txt, subtask1_random04.txt, subtask1_random05.txt, subtask1_random06.txt, subtask1_random07.txt, subtask1_random08.txt, subtask1_special01.txt, subtask1_special02.txt, subtask1_special03.txt, subtask1_special04.txt
Case Name Status Exec Time Memory
subtask0_sample_01.txt AC 1 ms 256 KB
subtask0_sample_02.txt AC 1 ms 256 KB
subtask1_line01.txt AC 1 ms 256 KB
subtask1_line02.txt AC 1 ms 256 KB
subtask1_line03.txt AC 1 ms 256 KB
subtask1_line04.txt AC 1 ms 256 KB
subtask1_line05.txt AC 1 ms 256 KB
subtask1_line06.txt AC 1 ms 256 KB
subtask1_random01.txt AC 1 ms 256 KB
subtask1_random02.txt AC 1 ms 256 KB
subtask1_random03.txt AC 1 ms 256 KB
subtask1_random04.txt AC 1 ms 256 KB
subtask1_random05.txt AC 1 ms 256 KB
subtask1_random06.txt AC 1 ms 256 KB
subtask1_random07.txt AC 4 ms 384 KB
subtask1_random08.txt AC 1 ms 256 KB
subtask1_special01.txt AC 1 ms 256 KB
subtask1_special02.txt AC 1 ms 256 KB
subtask1_special03.txt AC 1 ms 256 KB
subtask1_special04.txt AC 1 ms 256 KB