Submission #286777


Source Code Expand

Copy
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>

using namespace std;
int parent[200];
int cnt[200];
int exists[200];
vector<int> edge[200];
pair<int,int> dfs( int x, int prev ){
  int c = 0;
  int dist = 0;
  for( int i = 0 ; i < (int) edge[x].size(); i++ ){
    int nxt = edge[x][i];
    if(nxt != prev){
      pair<int,int> p = dfs(nxt,x);
      if( p.first != 0 ){
        c+= p.first;
        dist+=p.second+2;
      }
    }
  }
  parent[x] = prev;
  c+=exists[x];
  cnt[x] = c;
  return make_pair(c,dist);
}

int main(){
  int n, x;
  cin >> n >>  x;
  for( int i = 0 ; i <n ; i++ ) cin >> exists[i];
  for( int i = 0 ; i < n-1 ; i++ ){
    int s,t;
    cin >> s >> t;
    s--;
    t--;
    edge[s].push_back(t);
    edge[t].push_back(s);
  }
  memset(parent,-1,sizeof(parent));
  memset(cnt,0,sizeof(cnt)); 
  x--;
  pair<int,int> p = dfs(x,x);
  cout << p.second<<endl;
  
  return 0;
}

Submission Info

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