Submission #286600


Source Code Expand

#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;
typedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll;
typedef vector<string> vs; typedef long double ld;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

vector<int> h;
vector<vi> g;

int dfs(int i, int p = -1) {
	int res = 0;
	bool b = h[i] == 1;
	each(j, g[i]) if(*j != p) {
		int t = dfs(*j, i);
		if(t != -1) {
			b = true;
			res += t + 2;
		}
	}
	return b ? res : -1;
}

int main() {
	int n, x;
	scanf("%d%d", &n, &x), -- x;
	h.resize(n);
	rep(i, n) scanf("%d", &h[i]);
	g.assign(n, vi());
	rep(i, n-1) {
		int a, b;
		scanf("%d%d", &a, &b), -- a, -- b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	int ans = dfs(x);
	printf("%d\n", ans == -1 ? 0 : ans);
	return 0;
}

Submission Info

Submission Time
Task B - ツリーグラフ
User anta
Language C++ (G++ 4.6.4)
Score 100
Code Size 1963 Byte
Status AC
Exec Time 25 ms
Memory 928 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:59:29: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
./Main.cpp:61:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
./Main.cpp:65:36: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]

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 23 ms 800 KB
subtask0_sample_02.txt AC 23 ms 924 KB
subtask1_line01.txt AC 22 ms 788 KB
subtask1_line02.txt AC 23 ms 800 KB
subtask1_line03.txt AC 22 ms 920 KB
subtask1_line04.txt AC 22 ms 796 KB
subtask1_line05.txt AC 24 ms 800 KB
subtask1_line06.txt AC 23 ms 928 KB
subtask1_random01.txt AC 22 ms 928 KB
subtask1_random02.txt AC 22 ms 800 KB
subtask1_random03.txt AC 21 ms 800 KB
subtask1_random04.txt AC 22 ms 796 KB
subtask1_random05.txt AC 21 ms 800 KB
subtask1_random06.txt AC 22 ms 796 KB
subtask1_random07.txt AC 23 ms 804 KB
subtask1_random08.txt AC 23 ms 804 KB
subtask1_special01.txt AC 25 ms 796 KB
subtask1_special02.txt AC 23 ms 788 KB
subtask1_special03.txt AC 23 ms 796 KB
subtask1_special04.txt AC 22 ms 800 KB