Submission #286934


Source Code Expand

Copy
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
//#include <utility>
#include <set>
#include <iostream>
//#include <memory>
#include <string>
#include <vector>
#include <algorithm>
//#include <functional>
#include <sstream>
//#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
//#include <cctype>
#include <cstring>
//#include <ctime>
#include <iterator>
#include <bitset>
//#include <numeric>
//#include <list>
//#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;

int a[110][110]={0};
vint b;
int n,x;
int vis[110];
int dfs(int x,int y,int z){
	// cout<<x<<" "<<y<<" "<<z<<endl;
	int ret=0;
	if(b[x]==1){
		ret=y-z;
		z=y;
	}
	vis[x]=1;
	rep(i,n){
		if(a[x][i]==1&&vis[i]==0){
			int t=dfs(i,y+1,z);
			if(t>0) z=y;
			ret+=t;
		}
	}
	return ret;
}

void mainmain(){
	// int n,x;
	cin>>n>>x;
	vint h(n);
	x--;
	rep(i,n) cin>>h[i];
	b=h;
	rep(i,n-1){
		int c,d;
		cin>>c>>d;
		c--;
		d--;
		a[c][d]=1;
		a[d][c]=1;
	}
	int ans=dfs(x,0,0);
	cout<<2*ans<<endl;
	// vint hi(n,-1);
	// queue<int> q;
	// q.push(x);
	// int cnt=1;
	// hi[x]=0;
	// while(q.size()){
		// int qs=q.size();
		// rep(i,qs){
			// int t=q.front();
			// q.pop();
			// hi[t]=cnt;
			// rep(j,n){
				// if(a[t][j]==1&&hi[j]==-1){
					// q.push(j);
					// hi[t]=cnt;
				// }
			// }
		// }
	// }
}



}
main() try{
    mainmain();
}
catch(...){
}

Submission Info

Submission Time
Task B - ツリーグラフ
User j_gui0121
Language C++11 (GCC 4.8.1)
Score 100
Code Size 2552 Byte
Status AC
Exec Time 23 ms
Memory 932 KB

Judge Result

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