Submission #286964


Source Code Expand

Copy
#include <iostream>
#include <vector>
#include <queue>
#include <utility>
const int N_MAX = 100;
using namespace std;
typedef pair<int,int> P;

int N , x;
int exist[N_MAX + 1];
vector<int> v[N_MAX+1];
queue<P> q;
int flag[N_MAX+1];

int main(){
  cin >> N >> x;
  for(int i=0;i<N;i++){
    cin >> exist[i];
  }
  for(int i=0;i<N-1;i++){
    int a,b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  int ans = 0;
  q.push(make_pair(x,0));
  flag[x] = 1;
  while(!q.empty()){
    int qsize = q.size();
    for(int j=0;j<qsize;j++){
      P tmp = q.front();
      q.pop();
      int pos = tmp.first;
      int depth = tmp.second;
      if(exist[pos - 1] == 1){
        ans += depth*2;
        for(int i=0;i<v[pos].size();i++){
          if(flag[v[pos][i]] == 0){
            q.push(make_pair(v[pos][i],1));
            flag[v[pos][i]] = 1;
          }
        }
      }
      else{
        for(int i=0;i<v[pos].size();i++){
          if(flag[v[pos][i]] == 0){
            q.push(make_pair(v[pos][i],depth+1));
            flag[v[pos][i]] = 1;
          }
        }
      }
    }
  }

  cout << ans << endl;
}

Submission Info

Submission Time
Task B - ツリーグラフ
User gamelove765
Language C++ (G++ 4.6.4)
Score 0
Code Size 1183 Byte
Status WA
Exec Time 24 ms
Memory 928 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 100
Status
AC × 2
AC × 12
WA × 8
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 800 KB
subtask0_sample_02.txt AC 22 ms 804 KB
subtask1_line01.txt AC 22 ms 928 KB
subtask1_line02.txt AC 22 ms 796 KB
subtask1_line03.txt AC 21 ms 804 KB
subtask1_line04.txt AC 22 ms 924 KB
subtask1_line05.txt AC 21 ms 800 KB
subtask1_line06.txt AC 22 ms 920 KB
subtask1_random01.txt WA 23 ms 796 KB
subtask1_random02.txt WA 22 ms 676 KB
subtask1_random03.txt WA 22 ms 800 KB
subtask1_random04.txt WA 23 ms 800 KB
subtask1_random05.txt WA 24 ms 736 KB
subtask1_random06.txt WA 22 ms 804 KB
subtask1_random07.txt WA 23 ms 928 KB
subtask1_random08.txt WA 23 ms 928 KB
subtask1_special01.txt AC 23 ms 792 KB
subtask1_special02.txt AC 22 ms 676 KB
subtask1_special03.txt AC 23 ms 800 KB
subtask1_special04.txt AC 22 ms 800 KB