QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#116703#6526. CanvasITMO_pengzoo#RE 1ms3484kbC++203.9kb2023-06-29 20:29:412023-06-29 20:29:42

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-29 20:29:42]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3484kb
  • [2023-06-29 20:29:41]
  • 提交

answer

//  Nikita Golikov, 2023

#include <bits/stdc++.h>

using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

#ifdef GOLIKOV
  #include "/Users/golikovnik/contests/debug.h"
#else
  #define debug(...) ;
#endif

template <class A, class B>
bool smin(A& x, B&& y) {
  if (y < x) {
    x = y;
    return true;
  }
  return false;
}

template <class A, class B>
bool smax(A& x, B&& y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

void solveTest() {
  int n, m;
  cin >> n >> m;
  struct op {
    int l, x, r, y, id;
  };
  vector<op> ops(m);
  vector<vector<pair<int, int>>> g1(n);
  for (int i = 0; i < m; ++i) {
    cin >> ops[i].l >> ops[i].x >> ops[i].r >> ops[i].y;
    --ops[i].l;
    --ops[i].r;
    if (ops[i].x == 2) {
      swap(ops[i].x, ops[i].y);
      swap(ops[i].l, ops[i].r);
    }
    assert(ops[i].x <= ops[i].y);
    ops[i].id = i;
    if (ops[i].x == 1 && ops[i].y == 2) {
      g1[ops[i].l].emplace_back(ops[i].r, i);
    }
  }
  vector<int> start, finish;
  vector<int> used(m);
  vector<int> good(n);
  for (int i = 0; i < m; ++i) {
    if (ops[i].y == 1) {
      start.push_back(i);
      used[i] = 1;
    }
    if (ops[i].x == 2) {
      finish.push_back(i);
      used[i] = 1;
      good[ops[i].l] = 1;
      good[ops[i].r] = 1;
    }
  }
  vector<int> prefinish;
  queue<int> q;
  for (int i = 0; i < n; ++i) {
    if (good[i]) {
      q.push(i);
    }
  }
  while (!q.empty()) {
    int v = q.front(); q.pop();
    for (auto[u, i] : g1[v]) {
      if (!good[u]) {
        good[u] = 1;
        used[i] = 1;
        prefinish.push_back(i);
        q.push(u);
      }
    }
  }
  reverse(prefinish.begin(), prefinish.end());
  vector<vector<pair<int, int>>> g2(n);
  for (int i = 0; i < m; ++i) {
    if (used[i]) {
      continue;
    }
    assert(ops[i].x == 1 && ops[i].y == 2 && !good[ops[i].l]);
    if (good[ops[i].r]) {
      start.push_back(i);
      used[i] = 1;
    } else {
      g2[ops[i].l].emplace_back(ops[i].r, i);
    }
  }
  vector<bool> dfsUsed(n);
  vector<int> ts;
  auto dfs1 = [&](auto self, int v) -> void {
    dfsUsed[v] = 1;
    for (auto[u, i] : g2[v]) {
      if (!dfsUsed[u]) {
        self(self, u);
      }
    }
    ts.push_back(v);
  };
  vector<int> ans;
  auto dfs2 = [&](auto self, int v) -> void {
    dfsUsed[v] = 1;
    for (auto[u, i] : g2[v]) {
      if (!dfsUsed[u]) {
        used[i] = 1;
        ans.push_back(i);
        self(self, u);
      }
    }
  };
  for (int i = 0; i < n; ++i) {
    if (!dfsUsed[i]) {
      dfs1(dfs1, i);
    }
  }
  reverse(ts.begin(), ts.end());
  fill(dfsUsed.begin(), dfsUsed.end(), 0);
  for (int v : ts) {
    if (!dfsUsed[v]) {
      dfs2(dfs2, v);
    }
  }
  reverse(ans.begin(), ans.end());
  for (int i = 0; i < m; ++i) {
    assert(used[i]);
  }
  vector<int> endVal(n);
  vector<int> ord;
  auto use = [&](int i) {
    endVal[ops[i].l] = ops[i].x;
    endVal[ops[i].r] = ops[i].y;
    ord.push_back(i);
  };
  debug(start, ans, prefinish, finish);
  for (int i : start) {
    use(i);
  }
  for (int i : ans) {
    use(i);
  }
  for (int i : prefinish) {
    use(i);
  }
  for (int i : finish) {
    use(i);
  }
  cout << accumulate(endVal.begin(), endVal.end(), 0) << '\n';
  for (int i : ord) {
    cout << i + 1 << ' ';
  }
  cout << '\n';
}

int main() {
#ifdef GOLIKOV
  assert(freopen("in", "rt", stdin));
  auto _clock_start = chrono::high_resolution_clock::now();
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int tests_;
  cin >> tests_;
  for (int tt_ = 1; tt_ <= tests_; ++tt_) {
    solveTest();
  }

#ifdef GOLIKOV
  cerr << "Executed in " << chrono::duration_cast<chrono::milliseconds>(
      chrono::high_resolution_clock::now()
          - _clock_start).count() << "ms." << endl;
#endif
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3484kb

input:

2
4 4
1 1 2 2
3 2 4 1
1 2 3 2
2 1 4 1
4 2
3 2 4 1
1 2 3 1

output:

7
4 2 1 3 
5
2 1 

result:

ok Correct. (2 test cases)

Test #2:

score: -100
Dangerous Syscalls

input:

1
10 13
1 1 2 2
2 1 3 2
1 2 3 1
3 1 4 2
4 1 5 2
5 1 6 2
4 2 6 1
7 1 8 2
8 1 9 2
7 2 9 1
5 2 9 1
8 2 10 2
1 1 10 1

output:


result: