Submission #1294688


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;
int main(){
    int n,s; cin>>n>>s;
    vvi d(101,vi(101,INF));
    vb used(101,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==j)d[i][j]=0;
                else cmin(d[i][j],d[i][k]+d[k][j]);
            }
        }
    }
    ll ans=0;
    int now=s;
    while(true){
        bool foo=true;
        int cnt=0;
        rep(i,n+1)if(used[i])cnt++;
        if(cnt<=1)break;
        rep(i,n+1){
            if(used[i+1]&&now!=i+1){
                ans+=d[now][i+1];
                used[now]=false;
                now=i+1;
            }
        }
    }
    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 2008 Byte
Status WA
Exec Time 3 ms
Memory 256 KB

Judge Result

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