QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#65684#2475. Bank RobberyUBB_Zalau00RE 0ms0kbC++143.3kb2022-12-02 21:02:042022-12-02 21:02:05

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-12-02 21:02:05]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2022-12-02 21:02:04]
  • 提交

answer

#include <bits/stdc++.h>
#ifdef LOCAL_DEFINE
#include <dbg.h>
#else
#define dbg(...)
#endif

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m; cin >> n >> m;
  int copy_m = m;
  vector<vector<int>> g(n);
  for (int i = 0; i < n - 1; ++i) {
    int a, b; cin >> a >> b;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }

  int leaves = 0;
  int root = -1;
  for (int i = 0; i < n; ++i) {
    if ((int)g[i].size() == 1) ++leaves;
    else root = i;
  }
  dbg(leaves);

  vector<int> depth(n), par(n, -1);
  function<void(int)> DFS = [&](int node) {
    for (int x : g[node]) {
      if (x == par[node]) continue;
      depth[x] = depth[node] + 1;
      par[x] = node;
      DFS(x);
    }
  };

  DFS(root);

  vector<bool> prot(n);

  auto GetInt = [&]() {
    int node; cin >> node;
    if (node == -1) exit(0);
    return node;
  };

  auto Defender = [&]() {
    cout << "DEFEND" << endl;
    for (int i = 0; i < n; ++i) {
      if ((int)g[i].size() > 1) {
        cout << i << ' ';
        prot[i] = true;
        --m;
      }
    }

    int leaf = -1;
    for (int i = 0; i < n; ++i) {
      if ((int)g[i].size() == 1) leaf = i;
      if ((int)g[i].size() == 1 && m > 0) {
        cout << i << ' ';
        prot[i] = true;
        leaf = i;
        --m;
      }
    }
    cout << endl;
    assert(leaf != -1);

    while (true) {
      int node = GetInt();
      if (prot[node]) {
        cout << 0 << endl; 
      } else {
        assert((int)g[node].size() == 1); 
        prot[leaf] = false;
        prot[node] = true;

        vector<int> path;
        while (depth[node] != depth[leaf]) {
          path.emplace_back(node);
          node = par[node];
        }
        vector<int> path2;
        while (node != leaf) {
          path.emplace_back(node);
          path2.emplace_back(leaf);  
          leaf = par[leaf];
          node = par[node];
        }
        path.emplace_back(node);
        reverse(path2.begin(), path2.end());
        for (int x : path2) path.emplace_back(x);
        cout << (int)path.size() - 1 << ' ';
        for (int i = 0; i + 1 < (int)path.size(); ++i) {
          cout << path[i] << ' ' << path[i + 1] << ' ';
        }
        cout << endl;
      }
    }
  };

  auto Attacker = [&]() {
    cout << "ATTACK" << endl;
    for (int i = 0; i < copy_m; ++i) {
      int node = GetInt();
      prot[node] = true;
    }

    int it = 0;
    auto Attack = [&](int node) {
      ++it;
      if (it > 365) exit(0);
      cout << node << endl;

      int moves = GetInt();
      for (int i = 0; i < moves; ++i) {
        int from = GetInt(), to = GetInt();
        assert(prot[from]);
        prot[from] = false;
        prot[to] = true;
      }

      for (int i = 0; i < n; ++i) {
        if (prot[i]) continue;
        bool ok = true;
        for (int x : g[i]) {
          if (prot[x]) ok = false;
        }
        if (ok) {
          cout << i << endl;
          exit(0);
        }
      }
    };

    while (true) {
      for (int i = 0; i < n; ++i) {
        if ((int)g[i].size() == 1) {
          Attack(i);
        }
      }
    }
  };

  int inner = n - leaves;
  if (m >= inner + 1) {
    Defender();
  } else {
    Attacker();
  }
}

詳細信息

Test #1:

score: 0
Dangerous Syscalls

input:

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

output:

DEFEND
0 3 1 
2 4 3 3 5 

result: