QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#730123#6559. A Tree and Two Edgesahsoltan#WA 200ms10868kbC++204.8kb2024-11-09 18:47:182024-11-09 18:47:22

Judging History

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

  • [2024-11-09 18:47:22]
  • 评测
  • 测评结果:WA
  • 用时:200ms
  • 内存:10868kb
  • [2024-11-09 18:47:18]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) int((x).size())
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

#ifdef LOCAL
auto operator<<(auto& o, auto x) -> decltype(x.first, o);
auto operator<<(auto& o, auto x) -> decltype(x.end(), o) {
  o << "{";
  for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y;
  return o << "}";
}
auto operator<<(auto& o, auto x) -> decltype(x.first, o) {
  return o << "(" << x.first << ", " << x.second << ")";
}
void __print(auto... x) { ((cerr << x << " "), ...) << endl; }
#define debug(x...) __print("[" #x "]:", x)
#else
#define debug(...) 2137
#endif

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n, q;
  cin >> n >> q;
  vector<vector<int>> graph(n + 1);
  vector<pair<int, int>> kra(n + 1);
  for(auto &[i, j] : kra)
    cin >> i >> j;
  
  vector<pair<int, int>> drz;
  vector<pair<int, int>> rest;

  {
    vector<int> si(n + 1, 1);
    vector<int> re(n + 1, 0);
    iota(all(re), 0);
    function<int(int)> fi = [&] (int x) {
      if(re[x] != x)
        re[x] = fi(re[x]);
      return re[x];
    };
    auto un = [&] (int x, int y) {
      x = fi(x);
      y = fi(y);
      if(x == y)
        return false;
      si[x] += y;
      re[y] = x;
      return true;
    };

    for(auto [i, j] : kra) {
      int czy = un(i, j);
      if(czy)
        drz.emplace_back(i, j);
      else
        rest.emplace_back(i, j);
    }
  }

  for(auto [i, j] : drz) {
    graph[i].emplace_back(j);
    graph[j].emplace_back(i);
  }


  vector par(n + 1, 0), gle(n + 1, 0);
  vector pre(n + 1, 0), post(n + 1, 0);
  int ctr = 0;

  debug(drz);
  debug(graph);

  function<void(int, int)> dfs = [&] (int x, int p) {
    debug("dfs", x, p);
    par[x] = p;
    pre[x] = ctr++;
    for(auto i : graph[x]) {
      if(i != p) {
        gle[i] = gle[x] + 1;
        dfs(i, x);
      }
    }
    post[x] = ctr++;
  };

  dfs(1, 1);

  int ile = 1;
  {
    int skok = 1;
    while(skok <= n) {
      skok *= 2;
      ++ile;
    }
  }


  vector jmp(ile, vector(n + 1, 0));
  jmp[0] = par;
  rep(i, 1, ile) {
    for(int j = 1; j <= n; j++) {
      jmp[i][j] = jmp[i - 1][jmp[i - 1][j]];
    }
  }

  auto lca = [&] (int x, int y) {
    if(gle[x] > gle[y])
      swap(x, y);
    int roz = gle[y] - gle[x];
    for(int i = ile - 1; i >= 0; --i) {
      if(roz & (1 << i))
        y = jmp[i][y];
    }
    if(x == y)
      return x;
    for(int i = ile - 1; i >= 0; --i) {
      int xx = jmp[i][x], yy = jmp[i][y];
      if(xx != yy) {
        x = xx;
        y = yy;
      }
    }
    return jmp[0][x];
  };

  //x pod y
  auto pod = [&] (int x, int y) {
    return pre[y] <= pre[x] && pre[x] <= post[y];
  };

  debug(pre);
  debug(post);


  debug(drz);
  debug(rest);

  const auto [P, Q] = rest[0];
  const auto [R, S] = rest[1];

  rep(_, 0, q) {
    int a, b;
    cin >> a >> b;


    vector<vector<int>> pos = {
      {a, b},
      {a, P, Q, b},
      {a, Q, P, b},
      {a, R, S, b},
      {a, S, R, b},

      {a, P, Q, R, S, b},
      {a, Q, P, R, S, b},
      {a, P, Q, S, R, b},
      {a, Q, P, S, R, b},

      {a, R, S, P, Q, b},
      {a, R, S, Q, P, b},
      {a, S, R, P, Q, b},
      {a, S, R, Q, P, b},
    };

    unordered_map<ll, int> cache;

    auto join = [&](int x, int y) {
      return (ll(x) << 25) | y;
    };

    auto qlca = [&] (int x, int y) {
      ll ki = join(x, y);
      if(cache.count(ki))
        return cache[ki];
      return cache[ki] = lca(x, y);
    };

    int res = 0;

    for(auto path : pos) {
      bool dobrze = 1;
      for(int i = 0; i < sz(path); i += 2) {
        for(int j = i + 2; j < sz(path); j += 2) {
          int x0 = path[i];
          int x1 = path[i + 1];
          
          int y0 = path[j];
          int y1 = path[j + 1];
          
          int lx = qlca(x0, x1);
          int ly = qlca(y0, y1);
          
          bool ok = 1;

          for(auto xx : {x0, x1}) {
            if(pod(xx, ly)) {
              debug(xx, ly, pod(xx, ly));
              if(pod(y0, xx) || pod(y1, xx)) {
                debug(y0, xx, pod(y0, xx));
                debug(y1, xx, pod(y1, xx));
                ok = 0;
              }
            }
          }
          for(auto yy : {y0, y1}) {
            if(pod(yy, lx)) {
              if(pod(x0, yy) || pod(x1, yy)) {
                debug(yy);
                ok = 0;
              }
            }
          }

          if(!ok) {
            debug("ZLE", path, x0, y0);
            debug(lx, ly);
            dobrze = 0;
          }
        }
      }
      res += dobrze;
    }

    cout << res << endl;
 
  }

}


详细

Test #1:

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

input:

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

output:

3
3
3
3
3
4

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 3552kb

input:

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

output:

2
2
4
1

result:

ok 4 lines

Test #3:

score: -100
Wrong Answer
time: 200ms
memory: 10868kb

input:

50000 50000
11561 23122
14261 28523
24407 48814
17947 35894
14803 29607
19557 39115
12415 24830
9583 19167
18397 36794
439 878
18040 36080
17150 34300
7922 15845
18938 37877
18625 37250
6459 12919
9818 19636
3579 7158
21323 42646
23882 47764
13603 27207
8353 16707
15907 31814
20935 41871
11686 23372...

output:

13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
6
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
1...

result:

wrong answer 1st lines differ - expected: '4', found: '13'