AtCoder Grand Contest 010

Submission #1097950

Source codeソースコード

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define FOR(i,k,n) for (int (i)=(k); (i)<(n); ++(i))
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr<< #x <<": "<<x<<endl
#define debug2(x,y) cerr<< #x <<": "<< x <<", "<< #y <<": "<< y <<endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ll> vll;
typedef vector<vector<ll> > vvll;
template<class T> using vv=vector<vector< T > >;

vi a;
vvi g;
vi par;

ll evaluation(int u) {
  if ((int)g[u].size() == 1) {
    return a[u];
  }
  ll sum_childs = 0;
  vll childs;
  for (int v : g[u]) {
    if (v == par[u]) {
      continue;
    }
    ll tmp = evaluation(v);
    childs.push_back(tmp);
    sum_childs += tmp;
  }
  sort(all(childs));
  ll min_a, max_a;
  max_a = sum_childs;

  if (childs.back() * 2 >= sum_childs) {
    min_a = childs.back();
  } else {
    min_a = (sum_childs + 1) / 2;
  }

  if (a[u] < min_a || max_a < a[u]) {
    printf("NO\n");
    exit(0);
  }
  return sum_childs - (sum_childs - a[u]) * 2;
}

void search_main(int root) {
  ll sum_child = 0;
  vll childs;
  for (int u : g[root]) {
    ll tmp = evaluation(u);
    childs.push_back(tmp);
    sum_child += tmp;
  }
  if (a[root] * 2 != sum_child) {
    printf("NO\n");
    exit(0);
  }
  sort(all(childs));
  if (childs.back() * 2 > sum_child) {
    printf("NO\n");
    exit(0);
  }
}

int main() {
  int n;
  scanf("%d", &n);
  a.resize(n);
  rep (i, n) {
    scanf("%d", &a[i]);
  }
  g.resize(n);
  rep (i, n-1) {
    int u, v;
    scanf("%d %d", &u, &v);
    u -= 1;
    v -= 1;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  if (n == 2) {
    if (a[0] == a[1]) {
      printf("YES\n");
    } else {
      printf("NO\n");
    }
    return 0;
  }
  int root = 0;
  rep (i, n) {
    if (g[root].size() < g[i].size()) {
      root = i;
    }
  }
  par.resize(n);
  par[root] = root;
  deque<int> st;
  for (int u : g[root]) {
    par[u] = root;
    st.push_back(u);
  }
  while (!st.empty()) {
    int u = st.front();
    st.pop_front();
    for (int v : g[u]) {
      if (v == par[u]) {
        continue;
      }
      par[v] = u;
      st.push_back(v);
    }
  }
  search_main(root);
  printf("YES\n");

  return 0;
}

Submission

Task問題 C - Cleaning
User nameユーザ名 056 tspcx
Created time投稿日時
Language言語 C++14 (Clang 3.8.0)
Status状態 AC
Score得点 700
Source lengthソースコード長 2773 Byte
File nameファイル名
Exec time実行時間 70 ms
Memory usageメモリ使用量 15872 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - sample1.txt,sample2.txt,sample3.txt
All 700 / 700 sample1.txt,sample2.txt,sample3.txt,in1.txt,in10.txt,in11.txt,in12.txt,in13.txt,in14.txt,in15.txt,in16.txt,in17.txt,in18.txt,in19.txt,in2.txt,in20.txt,in21.txt,in22.txt,in23.txt,in24.txt,in25.txt,in26.txt,in27.txt,in28.txt,in29.txt,in3.txt,in30.txt,in31.txt,in32.txt,in33.txt,in34.txt,in35.txt,in36.txt,in37.txt,in38.txt,in39.txt,in4.txt,in40.txt,in41.txt,in42.txt,in43.txt,in44.txt,in45.txt,in5.txt,in6.txt,in7.txt,in8.txt,in9.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
in1.txt AC 62 ms 6912 KB
in10.txt AC 61 ms 6656 KB
in11.txt AC 70 ms 14080 KB
in12.txt AC 70 ms 14080 KB
in13.txt AC 68 ms 15872 KB
in14.txt AC 67 ms 14592 KB
in15.txt AC 3 ms 256 KB
in16.txt AC 3 ms 256 KB
in17.txt AC 3 ms 256 KB
in18.txt AC 3 ms 256 KB
in19.txt AC 57 ms 6656 KB
in2.txt AC 61 ms 6656 KB
in20.txt AC 54 ms 6656 KB
in21.txt AC 54 ms 6656 KB
in22.txt AC 56 ms 6656 KB
in23.txt AC 58 ms 6656 KB
in24.txt AC 56 ms 6656 KB
in25.txt AC 3 ms 256 KB
in26.txt AC 55 ms 6656 KB
in27.txt AC 55 ms 6656 KB
in28.txt AC 3 ms 256 KB
in29.txt AC 51 ms 6656 KB
in3.txt AC 62 ms 6656 KB
in30.txt AC 3 ms 256 KB
in31.txt AC 62 ms 6656 KB
in32.txt AC 61 ms 6656 KB
in33.txt AC 61 ms 6656 KB
in34.txt AC 61 ms 6656 KB
in35.txt AC 58 ms 6656 KB
in36.txt AC 55 ms 6656 KB
in37.txt AC 3 ms 384 KB
in38.txt AC 62 ms 6656 KB
in39.txt AC 61 ms 6656 KB
in4.txt AC 62 ms 6656 KB
in40.txt AC 60 ms 6656 KB
in41.txt AC 61 ms 6656 KB
in42.txt AC 61 ms 6656 KB
in43.txt AC 57 ms 6656 KB
in44.txt AC 57 ms 6656 KB
in45.txt AC 61 ms 6656 KB
in5.txt AC 61 ms 6656 KB
in6.txt AC 61 ms 6656 KB
in7.txt AC 55 ms 6016 KB
in8.txt AC 16 ms 1792 KB
in9.txt AC 61 ms 6656 KB
sample1.txt AC 3 ms 256 KB
sample2.txt AC 3 ms 256 KB
sample3.txt AC 3 ms 256 KB