QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#120119#6608. Descent of DragonsKHINWA 3ms24740kbC++146.8kb2023-07-06 14:00:542023-07-06 14:00:56

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-06 14:00:56]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:24740kb
  • [2023-07-06 14:00:54]
  • 提交

answer

# include <bits/stdc++.h>

using namespace std;

namespace arcanaeden {
  auto& cin(std::cin);
  auto& cout(std::cout);
  // ifstream cin("arcanaeden.in");
  // ofstream cout("arcanaeden.out");
  constexpr uint N(200'000);
  constexpr uint W(65'536);
  static_assert(W == 1 << 16);
  struct edge { uint u, v; };
  uint n;
  uint w[N + 1];
  edge e[N + 0];
  namespace sub1 {
    constexpr uint N(11);
    vector<uint> inc[N + 1];
    bool vis[N + 1];
    uint ans(UINT_MAX);
    uint search(uint const x) {
      if (vis[x]) return 0;
      else vis[x] = true;
      uint res(w[x]);
      for (uint const y : inc[x])
        res ^= search(y);
      return res;
    }
    bool main() {
      if (n > N) return false;
      for (uint i(0); i != 1u << n; ++i) {
        for (uint j(1); j != n; ++j) if (i & 1 << j) {
          inc[e[j].u].push_back(e[j].v);
          inc[e[j].v].push_back(e[j].u);
        }
        bool ok(true);
        for (uint j(1); j <= n; ++j)
          ok = ok && inc[j].size() <= 2;
        uint res(0);
        for (uint j(1); j <= n; ++j)
          if (!vis[j] && inc[j].size() <= 1)
            res += search(j);
        for (uint j(1); j <= n; ++j)
          ok = ok && vis[j];
        if (ok) ans = min(ans, res);
        fill(inc + 1, inc + n + 1, vector<uint>());
        fill(vis + 1, vis + n + 1, false);
      }
      cout << ans << endl;
      return true;
    }
  }
  namespace sub2 {
    constexpr uint N(2'222);
    struct node {
      vector<uint> n;
      uint l, r;
      ulong ww, sum, f;
    };
    node v[N + 1];
    uint index, at[N];
    void search0(uint const x, uint const mo) {
      v[x].ww = v[mo].ww ^ w[x];
      at[v[x].l = index++] = x;
      for (uint const y : v[x].n) if (y != mo) search0(y, x);
      v[x].r = index;
    }
    void search1(uint const x, uint const mo) {
      for (uint const y : v[x].n) if (y != mo) search1(y, x);
      ulong sum(0);
      for (uint const y : v[x].n) sum += v[y].f;
      v[x].f = sum + w[x];
      for (uint const y : v[x].n) if (y != mo) {
        sum -= v[y].f;
        for (uint i(v[y].l); i != v[y].r; ++i) {
          v[x].f = min(v[x].f, v[at[i]].sum + (v[at[i]].ww ^ v[mo].ww) + sum);
        }
        sum += v[y].f;
      }
      for (uint const y : v[x].n) if (y != mo)
        for (uint const z : v[x].n) if (z != mo)
          if (y != z) {
            sum -= v[y].f + v[z].f;
            for (uint i(v[y].l); i != v[y].r; ++i)
              for (uint j(v[z].l); j != v[z].r; ++j) {
                ulong const si(v[at[i]].sum);
                ulong const sj(v[at[j]].sum);
                ulong const wd(v[at[i]].ww ^ v[at[j]].ww);
                ulong const wx(v[x].ww ^ v[mo].ww);
                v[x].f = min(v[x].f, si + sj + (wd ^ wx) + sum);
              }
            sum += v[y].f + v[z].f;
          }
      v[x].sum = sum;
      for (uint const y : v[x].n) if (y != mo) {
        sum -= v[y].f;
        for (uint i(v[y].l); i != v[y].r; ++i) v[at[i]].sum += sum;
        sum += v[y].f;
      }
    }
    bool main() {
      if (n > N) return false;
      for (uint i(1); i != n; ++i) {
        v[e[i].u].n.push_back(e[i].v);
        v[e[i].v].n.push_back(e[i].u);
      }
      search0(1, 0);
      search1(1, 0);
      cout << v[1].f << endl;
      return true;
    }
  }
  namespace sub3 {
    constexpr uint W(256);
    static_assert(W == 1 << 8);
    constexpr int inf(N * W);
    struct node {
      vector<uint> n;
      vector<int> f;
    };
    node v[N + 1];
    void search(uint const x, uint const mo) {
      v[x].f.assign(W, inf);
      int sum(0);
      for (uint const y : v[x].n) if (y != mo)
        search(y, x), sum += v[y].f[0];
      v[x].f[0] = w[x] + sum, v[x].f[w[x]] = sum;
      static int f[W];
      memset(f, 0x3f, sizeof f);
      for (uint const y : v[x].n) if (y != mo) {
        sum -= v[y].f[0];
        vector<uint> set;
        for (uint i(0); i != W; ++i) if (v[y].f[i] < inf) {
          uint const j(i ^ w[x]);
          v[x].f[j] = min(v[x].f[j], v[y].f[i] + sum);
          v[x].f[0] = min(v[x].f[0], v[y].f[i] + sum + int(j));
          set.push_back(i);
        }
        for (uint i(0); i != W; ++i) if (f[i] < inf) for (uint j : set) {
          int const s(sum + f[i] + v[y].f[j]);
          v[x].f[0] = min(v[x].f[0], s + int(i ^ j ^ w[x]));
        }
        for (uint i(0); i != W; ++i)
          f[i] = min(f[i], v[y].f[i] - v[y].f[0]);
        sum += v[y].f[0];
      }
    }
    bool main() {
      if (*max_element(w + 1, w + n + 1) > W) return false;
      for (uint i(1); i != n; ++i) {
        v[e[i].u].n.push_back(e[i].v);
        v[e[i].v].n.push_back(e[i].u);
      }
      search(1, 0);
      cout << v[1].f[0] << endl;
      return true;
    }
  }
  namespace sub6 {
    struct node {
      vector<uint> n;
      vector<pair<ulong, ulong>> f;
      ulong g;
    };
    node v[N + 1];
    void search(uint const x, uint const mo) {
      ulong sum(0);
      for (uint const y : v[x].n) if (y != mo)
        search(y, x), sum += v[y].g;
      v[x].f.emplace_back(w[x] + sum, w[x]);
      v[x].g = w[x] + sum;
      for (uint const y : v[x].n) if (y != mo)
        for (auto& f : v[y].f) {
          ulong val(f.first - f.second + (w[x] ^ f.second));
          for (uint const z : v[x].n)
            if (z != mo && z != y) val += v[z].g;
          v[x].f.emplace_back(val, w[x] ^ f.second);
          if (v[x].f.size() > 30)
            v[x].f.erase(max_element(v[x].f.begin(), v[x].f.end()));
        }
      for (uint const y : v[x].n) if (y != mo)
        for (uint const z : v[x].n) if (z != mo)
          if (y != z) for (auto& fy : v[y].f) for (auto& fz : v[z].f) {
            ulong val(w[x] ^ fy.second ^ fz.second);
            val += fy.first - fy.second;
            val += fz.first - fz.second;
            for (ulong const a : v[x].n)
              if (a != mo && a != y && a != z)
                val += v[a].g;
            v[x].g = min(v[x].g, val);
          }
      // fprintf(stderr, "%u : %lu %lu %lu\n", x, v[x].f, v[x].g, v[x].h);
    }
    bool main() {
      for (uint i(1); i != n; ++i) {
        v[e[i].u].n.push_back(e[i].v);
        v[e[i].v].n.push_back(e[i].u);
      }
      search(1, 0);
      cout << v[1].g << endl;
      return true;
    }
  }
  void main() {
    // cerr << sizeof sub2::v << endl;
    // cerr << sizeof sub3::v << endl;
    // cerr << sizeof sub6::v << endl;
    cin >> n;
    for (uint i(1); i <= n; ++i) cin >> w[i];
    for (uint i(1); i != n; ++i) cin >> e[i].u >> e[i].v;
    if (sub1::main()) return;
    if (sub2::main()) return;
    if (sub3::main()) return;
    if (sub6::main()) return;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  arcanaeden::cin.tie(nullptr);
  arcanaeden::main();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 24740kb

input:

5 5
1 3 5 0
1 1 4 1
1 1 5 2
2 2 2
2 4 5

output:

4

result:

wrong answer 1st numbers differ - expected: '0', found: '4'