Submission #1113790


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
const int size = 100;

int N, X;
int H[size];
vector<int> edges[size];
bool hasJewel[size];
int cnt[size];
vector<int> idByDepth;

void init() {
  cin >> N >> X;
  X -= 1;
  for(int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if(a == 1)
      hasJewel[i] = true;
    else
      hasJewel[i] = false;
  }
  
  for(int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a -= 1;
    b -= 1;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }
}

int parent[size];

void getparent() {
  bool visited[N];
  for(int i = 0; i < N; i++)
    visited[i] = false;
  queue<int> q;
  q.push(X);
  parent[X] = -1;
  visited[X] = true;
  while(q.empty() == false) {
    int from = q.front();
    q.pop();
    for(int to: edges[from]) {
      if(visited[to] == false) {
	idByDepth.push_back(to);
	visited[to] = true;
	parent[to] = from;
	q.push(to);
      }
    }
  }
  reverse(idByDepth.begin(), idByDepth.end());

}

void f() {


  for(int i: idByDepth) {
    if(hasJewel[i]) {
      cnt[ parent[i] ] += (cnt[i] + 1);
      hasJewel[ parent[i] ] = true;
      hasJewel[i] = false;
      f();
    }
  }
}

void solve() {
  getparent();
  f();
  cout << cnt[X] * 2 << endl;
}

int main() {
  init();
  solve();
}

Submission Info

Submission Time
Task B - ツリーグラフ
User kkty
Language C++14 (GCC 5.4.1)
Score 100
Code Size 1347 Byte
Status AC
Exec Time 1 ms
Memory 256 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 1 ms 256 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