Contest Duration: - (local time) (90 minutes) Back to Home

Submission #287260

Source Code Expand

Copy
#include <iostream>
#include <sstream>
#include <cstdio>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <list>
#include <stack>
#include <queue>
#include <cstdlib>
#include <tuple>
#define INF 1e9
#define EPS 1e-9
using namespace std;

typedef list<int> L;
typedef pair <int,int> P;
typedef vector<int> V;
typedef queue<int> Q;
typedef stack<int> S;
typedef map<string,int> M;

int main()
{
int n, x;
cin >> n >> x;
V h(n);
int sum = 0;
for (auto &i : h) {
cin >> i;
if (i == 1) sum++;
}

vector<V> A(n+1, V(n+1,10000000));;
for (int i = 0; i < n-1; i++) {
int a, b;
cin >> a >> b;
A[a][b] = A[b][a] = 1;
}
for (int i = 0; i < n; i++) {
A[i][i] = 0;
}

Q p;
for (int i = 1; i < A[x].size(); i++) {
if (A[x][i] == 1) {
p.push(i);
A[i][0] = x;
}
}
A[x][0] = 0;
int res = 0;
V d(n+1,0);
while (p.size()) {
int t = p.front(); p.pop();
for (int i = 1; i < A[t].size(); i++) {
if (A[t][0] != i && A[t][i] == 1) {
p.push(i);
A[i][0] = t;
}
}
if (h[t-1] == 1) {
res += d[A[t][0]] + 2;
} else {
d[t] = d[A[t][0]] + 2;
}
}

cout << res << endl;

return 0;
}

#### Submission Info

Submission Time 2014-11-29 22:07:08+0900 B - ツリーグラフ wanimaru47 C++11 (GCC 4.8.1) 0 1476 Byte WA 28 ms 928 KB

#### Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 100
Status
 AC × 2
 AC × 12 WA × 8
Set Name Test Cases
Case Name Status Exec Time Memory
subtask0_sample_01.txt AC 22 ms 796 KB
subtask0_sample_02.txt AC 25 ms 800 KB
subtask1_line01.txt AC 28 ms 800 KB
subtask1_line02.txt AC 23 ms 796 KB
subtask1_line03.txt AC 23 ms 924 KB
subtask1_line04.txt AC 23 ms 792 KB
subtask1_line05.txt AC 22 ms 836 KB
subtask1_line06.txt AC 22 ms 928 KB
subtask1_random01.txt WA 24 ms 728 KB
subtask1_random02.txt WA 23 ms 804 KB
subtask1_random03.txt WA 22 ms 920 KB
subtask1_random04.txt WA 23 ms 928 KB
subtask1_random05.txt WA 24 ms 840 KB
subtask1_random06.txt WA 23 ms 928 KB
subtask1_random07.txt WA 23 ms 800 KB
subtask1_random08.txt WA 22 ms 800 KB
subtask1_special01.txt AC 22 ms 792 KB
subtask1_special02.txt AC 22 ms 736 KB
subtask1_special03.txt AC 24 ms 804 KB
subtask1_special04.txt AC 25 ms 920 KB