Submission #1725528


Source Code Expand

#include <bits/stdc++.h>

using namespace std;

int n,x;
bool duel[102];
vector<int>g[103];
int d_c;
bool use[103];
int dx;
int ans;

void dfs(int v, int d, bool on) {

  //cout <<on<<"   "<<v<<endl;

  if(on && v == x) {ans += d; return;}
  
  if(duel[v]) {
    dx = v;
    duel[v] = false;
    ans += d;
    //  cout << d <<"   "<<v<< endl;
    return ;
  }

  use[v] = true;

  for(int i = 0; i < g[v].size(); i++) {
    int u = g[v][i];
    if(!use[u]) {
      return dfs(u,d+1,on);
    }
  }
}
  

int main() {
  cin >> n >> x;

  for(int i = 1; i <= n; i++) {
    int h;
    cin >> h;
    if(h) {
      duel[i] = true;
      d_c++;
    }
    else duel[i] = false;
  }

 

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

  dx = x;

  for(int i = 0; i < d_c; i++) {
    fill(use,use+101,false);
    dfs(dx,0,false);
  }
  fill(use,use+101,false);
  dfs(dx,0,true);

  cout << ans << endl;
}

  
  

Submission Info

Submission Time
Task B - ツリーグラフ
User shossie1016
Language C++14 (GCC 5.4.1)
Score 0
Code Size 1043 Byte
Status WA
Exec Time 1 ms
Memory 256 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 100
Status
AC × 2
AC × 7
WA × 13
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 WA 1 ms 256 KB
subtask1_line03.txt WA 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 WA 1 ms 256 KB
subtask1_random02.txt WA 1 ms 256 KB
subtask1_random03.txt WA 1 ms 256 KB
subtask1_random04.txt WA 1 ms 256 KB
subtask1_random05.txt WA 1 ms 256 KB
subtask1_random06.txt WA 1 ms 256 KB
subtask1_random07.txt WA 1 ms 256 KB
subtask1_random08.txt WA 1 ms 256 KB
subtask1_special01.txt AC 1 ms 256 KB
subtask1_special02.txt WA 1 ms 256 KB
subtask1_special03.txt WA 1 ms 256 KB
subtask1_special04.txt WA 1 ms 256 KB