Submission #1294144
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;
vvi d(1000,vi(1000,INF));
int main(){
vb flag(1000,false);
int n,s; cin>>n>>s;
rep(i,n){
int temp; cin>>temp;
if(temp)flag[i+1]=true;
}
rep(i,n-1){
pii p; cin>>p.first>>p.second;
d[p.first][p.second]=1;
d[p.second][p.first]=1;
}
rep(i,n+1)
rep(j,n+1)
rep(k,n+1)
cmin(d[i][j],d[i][k]+d[k][j]);
ll ans=0;
int now=s;
vector<vector<bool>>visit(1000,vb(1000,true));
while(true){
bool hoge=true;
rep(i,n+1)if(flag[i])hoge=false;
if(hoge)break;
rep(i,n+1){
if(d[now][i]!=INF&&flag[i]&&now!=i){
if(visit[i][now]==false || visit[now][i]==false)continue;
ans+=d[now][i];
//cout<<d[now][i]<<" "<<now<<i<<endl;
visit[now][i]=false;
visit[i][now]=false;
flag[i]=false;
flag[now]=false;
now=i;
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 |
2193 Byte |
Status |
WA |
Exec Time |
2104 ms |
Memory |
4352 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
0 / 100 |
Status |
|
|
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 |
3 ms |
4352 KB |
subtask0_sample_02.txt |
TLE |
2104 ms |
4352 KB |
subtask1_line01.txt |
TLE |
2104 ms |
4352 KB |
subtask1_line02.txt |
AC |
5 ms |
4352 KB |
subtask1_line03.txt |
AC |
4 ms |
4352 KB |
subtask1_line04.txt |
AC |
5 ms |
4352 KB |
subtask1_line05.txt |
AC |
5 ms |
4352 KB |
subtask1_line06.txt |
WA |
5 ms |
4352 KB |
subtask1_random01.txt |
TLE |
2104 ms |
4352 KB |
subtask1_random02.txt |
WA |
4 ms |
4352 KB |
subtask1_random03.txt |
WA |
4 ms |
4352 KB |
subtask1_random04.txt |
TLE |
2104 ms |
4352 KB |
subtask1_random05.txt |
TLE |
2104 ms |
4352 KB |
subtask1_random06.txt |
TLE |
2104 ms |
4352 KB |
subtask1_random07.txt |
TLE |
2104 ms |
4352 KB |
subtask1_random08.txt |
TLE |
2104 ms |
4352 KB |
subtask1_special01.txt |
TLE |
2104 ms |
4352 KB |
subtask1_special02.txt |
AC |
5 ms |
4352 KB |
subtask1_special03.txt |
AC |
5 ms |
4352 KB |
subtask1_special04.txt |
WA |
4 ms |
4352 KB |