Submission #1294799


Source Code Expand

#include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <tuple>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <iterator>
#include <array>
#include <memory>
#include <stack>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
typedef pair<ll,ll>pll;
#define int ll
signed  main(){
    int n,s; cin>>n>>s;
    vvi d(120,vi(120,INF));
    vb used(120,false);
    rep(i,n){
        int temp;cin>>temp;
        if(temp)used[i+1]=true;
    }
    rep(i,n-1){
        pii hoge; cin>>hoge.first>>hoge.second;
        d[hoge.first][hoge.second]=1;
        d[hoge.second][hoge.first]=1;
    }
    rep(i,n+1){
        rep(j,n+1){
            rep(k,n+1){
                if(i==0||j==0||k==0||j==k)continue;
                cmin(d[j][k],d[j][i]+d[i][k]);
            }
        }
    }
    rep(i,n+1)d[i][i]=0;
    int ans=0;
    int now=s;
    while(true){
        bool update=false;
        int dmini=MAX;
        int __index=0;
        rep(i,n+1){
            if(used[i]&&now!=i){
                if(d[now][i]<dmini){
                    __index=i;
                    dmini=d[now][i];
                    update=true;
                }
            }
        }
        if(update){
            used[now]=false;
            now=__index;
            ans+=dmini;
        }else break;
    }
    cout<<ans+d[now][s]<<endl;
}

Submission Info

Submission Time
Task B - ツリーグラフ
User yebityon
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2222 Byte
Status WA
Exec Time 3 ms
Memory 384 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 100
Status
AC × 2
AC × 14
WA × 6
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 384 KB
subtask0_sample_02.txt AC 1 ms 384 KB
subtask1_line01.txt AC 1 ms 384 KB
subtask1_line02.txt AC 3 ms 384 KB
subtask1_line03.txt AC 2 ms 384 KB
subtask1_line04.txt AC 3 ms 384 KB
subtask1_line05.txt AC 3 ms 384 KB
subtask1_line06.txt AC 3 ms 384 KB
subtask1_random01.txt WA 3 ms 384 KB
subtask1_random02.txt AC 2 ms 384 KB
subtask1_random03.txt WA 2 ms 384 KB
subtask1_random04.txt AC 3 ms 384 KB
subtask1_random05.txt WA 3 ms 384 KB
subtask1_random06.txt WA 3 ms 384 KB
subtask1_random07.txt WA 3 ms 384 KB
subtask1_random08.txt WA 3 ms 384 KB
subtask1_special01.txt AC 1 ms 384 KB
subtask1_special02.txt AC 3 ms 384 KB
subtask1_special03.txt AC 3 ms 384 KB
subtask1_special04.txt AC 2 ms 384 KB