QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#527023#5504. Flower Gardennhuang685RE 0ms3544kbC++235.9kb2024-08-22 07:38:002024-08-22 07:38:00

Judging History

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

  • [2024-08-22 07:38:00]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3544kb
  • [2024-08-22 07:38:00]
  • 提交

answer

/**
 * @author n685
 * @brief
 * @date 2024-08-21 17:05:01
 *
 *
 */
#include "bits/stdc++.h"

#ifdef LOCAL
#include "dd/debug.h"
#else
#define dbg(...) 42
#define dbg_proj(...) 420
#define dbg_rproj(...) 420420

void nline() {}

void bar() {}

void start_clock() {}

void end_clock() {}
#endif

void tarjan(
  int node,
  std::vector<int> &clr,
  std::vector<int> &dfn,
  int n,
  const std::vector<std::vector<int>> &adj
) {
  static int cnt = 0, num = 0;
  static std::vector<int> low(n, -1);
  static std::vector<bool> vis(n);
  static std::stack<int, std::vector<int>> st;
  dfn[node] = cnt++;
  low[node] = dfn[node];
  st.push(node);
  vis[node] = true;
  for (int i : adj[node]) {
    if (dfn[i] == -1) {
      tarjan(i, clr, dfn, n, adj);
    }
    if (vis[i]) {
      low[node] = std::min(low[node], low[i]);
    }
  }
  if (dfn[node] == low[node]) {
    while (!st.empty()) {
      int v = st.top();
      st.pop();
      clr[v] = num;
      vis[v] = false;
      if (node == v) {
        break;
      }
    }
    ++num;
  }
}

std::vector<int> scc(int n, const std::vector<std::vector<int>> &adj) {
  std::vector<int> clr(n, -1), dfn(n, -1);
  for (int i = 0; i < n; ++i) {
    if (dfn[i] == -1) {
      tarjan(i, clr, dfn, n, adj);
    }
  }
  return clr;
}

std::optional<std::vector<bool>> type1(
  int n,
  int thres,
  const std::vector<int> &sz,
  const std::vector<std::vector<int>> &adj,
  const std::vector<std::vector<int>> &radj
) {
  std::vector<int> in(n);
  std::queue<int> q;
  for (int i = 0; i < n; ++i) {
    in[i] = static_cast<int>(adj[i].size());
    if (in[i] == 0) {
      q.push(i);
    }
  }
  std::vector<bool> ans(n);
  int sum = 0;
  while (!q.empty()) {
    int node = q.front();
    q.pop();
    if (sz[node] >= thres) {
      continue;
    }
    sum += sz[node];
    ans[node] = true;
    if (sum >= thres) {
      return ans;
    }
    for (int i : radj[node]) {
      if (--in[i] == 0) {
        q.push(i);
      }
    }
  }
  return std::nullopt;
}

int dfs_type2(
  int node,
  std::vector<bool> &ans,
  const std::vector<int> &sz,
  const std::vector<std::vector<int>> &adj
) {
  int res = sz[node];
  ans[node] = true;
  for (int i : adj[node]) {
    res += dfs_type2(i, ans, sz, adj);
  }
  return res;
}

std::optional<std::vector<bool>> type2(
  int n,
  int thres,
  const std::vector<int> &sz,
  const std::vector<std::vector<int>> &adj
) {
  for (int i = 0; i < 3 * n; ++i) {
    if (sz[i] >= thres) {
      std::vector<bool> ans(n);
      if (dfs_type2(i, ans, sz, adj) <= 2 * thres) {
        return ans;
      }
    }
  }
  return std::nullopt;
}

void build(
  int node,
  int l,
  int r,
  int &sz,
  std::vector<int> &iin,
  std::vector<int> &iout,
  std::vector<std::vector<int>> &adj
) {
  if (l == r) {
    iin[node] = l;
    iout[node] = l;
    return;
  }
  iin[node] = sz++;
  iout[node] = sz++;
  adj.emplace_back();
  adj.emplace_back();
  int mid = (l + r) / 2;
  build(2 * node, l, mid, sz, iin, iout, adj);
  build(2 * node + 1, mid + 1, r, sz, iin, iout, adj);
  adj[iin[node]].push_back(iin[2 * node]);
  adj[iin[node]].push_back(iin[2 * node + 1]);
  adj[iout[2 * node]].push_back(iout[node]);
  adj[iout[2 * node + 1]].push_back(iout[node]);
}

enum class Dir : int8_t { IN, OUT };

void unite(
  int a,
  int b,
  int v,
  Dir dir,
  int node,
  int l,
  int r,
  std::vector<std::vector<int>> &adj,
  const std::vector<int> &iin,
  const std::vector<int> &iout
) {
  if (a <= l && r <= b) {
    if (dir == Dir::OUT) {
      adj[iout[node]].push_back(v);
    } else {
      adj[v].push_back(iin[node]);
    }
    return;
  }
  int mid = (l + r) / 2;
  if (a <= mid) {
    unite(a, b, v, dir, 2 * node, l, mid, adj, iin, iout);
  }
  if (b > mid) {
    unite(a, b, v, dir, 2 * node + 1, mid + 1, r, adj, iin, iout);
  }
}

void solve() {
  int n, q;
  std::cin >> n >> q;

  int bound = static_cast<int>(std::bit_ceil<uint32_t>(3 * n));

  std::vector<int> iin(2 * bound), iout(2 * bound);
  int cnt = bound;
  std::vector<std::vector<int>> adj(bound);
  build(1, 0, bound - 1, cnt, iin, iout, adj);
  for (int i = 0; i < q; ++i) {
    int a, b, c, d;
    std::cin >> a >> b >> c >> d;
    --a;
    --b;
    --c;
    --d;
    adj.emplace_back();
    unite(a, b, cnt + i, Dir::OUT, 1, 0, bound - 1, adj, iin, iout);
    unite(c, d, cnt + i, Dir::IN, 1, 0, bound - 1, adj, iin, iout);
  }
  std::vector<int> clr = scc(cnt + q, adj);
  int m = *std::ranges::max_element(clr) + 1;
  std::vector<int> sz(m);
  std::vector<std::vector<int>> sadj(m);
  std::vector<std::vector<int>> gr(m);
  for (int i = 0; i < cnt + q; ++i) {
    if (i < 3 * n) {
      ++sz[clr[i]];
      gr[clr[i]].push_back(i);
    }
    for (int j : adj[i]) {
      if (clr[i] != clr[j]) {
        sadj[clr[i]].push_back(clr[j]);
      }
    }
  }
  for (std::vector<int> &v : sadj) {
    std::ranges::sort(v);
    v.erase(std::unique(v.begin(), v.end()), v.end());
  }
  std::vector<std::vector<int>> sradj(m);
  for (int i = 0; i < m; ++i) {
    for (int j : sadj[i]) {
      sradj[j].push_back(i);
    }
  }

  std::optional<std::vector<bool>> opt = type1(m, n, sz, sadj, sradj);
  std::vector<bool> ans;
  if (opt.has_value()) {
    ans = *opt;
  } else {
    opt = type2(m, n, sz, sadj);
    if (opt.has_value()) {
      ans = *opt;
    } else {
      std::cout << "NIE\n";
      return;
    }
  }
  std::cout << "TAK\n";
  std::string val(3 * n, 'R');
  for (int i = 0; i < m; ++i) {
    if (ans[i]) {
      for (int j : gr[i]) {
        val[j] = 'F';
      }
    }
  }
  std::cout << val << '\n';
}

int main() {
#ifndef LOCAL
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
#endif

  int z;
  std::cin >> z;
  for (int i = 0; i < z; ++i) {
    dbg(i + 1);
    solve();
    bar();
  }
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3544kb

input:

2
1 3
1 1 2 2
1 2 3 3
1 1 3 3
1 3
1 1 2 2
2 2 3 3
3 3 1 1

output:

TAK
RRF
NIE

result:

ok good!

Test #2:

score: -100
Runtime Error

input:

10
33333 100000
28701 40192 93418 95143
95902 97908 78378 78461
36823 44196 22268 23996
23977 24786 33315 48829
83965 90411 4923 8445
20235 21177 32543 47454
29598 35414 72477 73049
2014 12632 42163 46466
64305 65518 98825 99552
32331 41625 92772 96224
26500 54122 76990 77126
18249 20335 31165 36080...

output:


result: