Submission #286718


Source Code Expand

#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <climits>
#include <set>
using namespace std;
#define repn(i,s,n) for (int i=int(s); i < int(n); i++)
#define rep(i,n) repn(i,0,n)
#define repd(i,n) for (int i=int(n)-1; i >= 0; i--)
typedef vector<int> VI;
typedef vector<VI> VVI;

int n, x;

VVI edges;
VI h;

// ここ以下の歩数をかえす
int dfs(int v, int p) {
  int ret = 0;
  bool hasTakara = !!h[v];
  rep(ei, edges[v].size()) {
    int u = edges[v][ei];
    if (u != p) {
      int retu = dfs(u, v);
      if (retu != -1) {
        hasTakara = true;
        ret += retu + 2;  // 行って帰ってくる
      }
    }
  }
  return hasTakara ? ret : -1;
}

int main() {
  ios::sync_with_stdio(false);
  cin >> n >> x;
  x--;
  edges = VVI(n);
  h = VI(n);
  rep(i,n) cin >> h[i];
  rep(i, n-1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }

  int ans = dfs(x, -1);
  cout << (ans >= 0 ? ans : 0) << endl;

  return 0;
}

Submission Info

Submission Time
Task B - ツリーグラフ
User dai1741
Language C++ (G++ 4.6.4)
Score 100
Code Size 1142 Byte
Status AC
Exec Time 25 ms
Memory 932 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 22 ms 928 KB
subtask0_sample_02.txt AC 22 ms 924 KB
subtask1_line01.txt AC 22 ms 924 KB
subtask1_line02.txt AC 22 ms 780 KB
subtask1_line03.txt AC 21 ms 792 KB
subtask1_line04.txt AC 21 ms 924 KB
subtask1_line05.txt AC 21 ms 800 KB
subtask1_line06.txt AC 22 ms 816 KB
subtask1_random01.txt AC 23 ms 796 KB
subtask1_random02.txt AC 22 ms 928 KB
subtask1_random03.txt AC 22 ms 924 KB
subtask1_random04.txt AC 22 ms 932 KB
subtask1_random05.txt AC 22 ms 888 KB
subtask1_random06.txt AC 23 ms 804 KB
subtask1_random07.txt AC 21 ms 808 KB
subtask1_random08.txt AC 21 ms 796 KB
subtask1_special01.txt AC 22 ms 796 KB
subtask1_special02.txt AC 25 ms 928 KB
subtask1_special03.txt AC 21 ms 920 KB
subtask1_special04.txt AC 21 ms 928 KB