QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#671585#5088. Two ChoreographiesKowerKoint#WA 1ms5832kbC++232.2kb2024-10-24 13:34:202024-10-24 13:34:20

Judging History

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

  • [2024-10-24 13:34:20]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5832kb
  • [2024-10-24 13:34:20]
  • 提交

answer

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

int N;
int A[200000], B[200000];
vector<int> e[100000];

int doubling[100000][20];
int depth[100000];
set<pair<int, int>> used;
void dfs(int v) {
  for (auto i : e[v]) {
    if (doubling[i][0] != -2) continue;

    depth[i] = depth[v] + 1;
    doubling[i][0] = v;
    for (int j = 1; j < 20; j++) {
      if (doubling[i][j - 1] == -1) {
        doubling[i][j] = -1;
      } else {
        doubling[i][j] = doubling[doubling[i][j - 1]][j - 1];
      }
    }
    used.insert({min(v, i), max(v, i)});
    dfs(i);
  }
}

int lca(int a, int b) {
  if (depth[a] < depth[b]) swap(a, b);
  while (depth[a] > depth[b]) {
    int j = 0;
    int d = 1;
    while (d * 2 <= depth[a] - depth[b]) {
      j++;
      d <<= 1;
    }
    a = doubling[a][j];
  }
  while (a != b) {
    int j = 0;
    while (doubling[a][j + 1] != doubling[b][j + 1]) j++;
    a = doubling[a][j];
    b = doubling[b][j];
  }
  return a;
}

int dist(int a, int b) {
  return depth[a] + depth[b] - 2 * depth[lca(a, b)];
}

void print_path(int a, int b) {
  int l = lca(a, b);
  while (a != l) {
    cout << a + 1 << " ";
    a = doubling[a][0];
  }
  cout << l + 1;
  vector<int> ans;
  while (b != l) {
    ans.push_back(b);
    b = doubling[b][0];
  }
  reverse(ans.begin(), ans.end());
  for (auto i : ans) cout << " " << i + 1;
}

int main() {
  cin >> N;
  for (int i = 0; i < 2 * N - 3; i++) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
    if (A[i] > B[i]) swap(A[i], B[i]);
    e[A[i]].push_back(B[i]);
    e[B[i]].push_back(A[i]);
  }

  for (int i = 0; i < N; i++) doubling[i][0] = -2;
  for (int j = 0; j < 20; j++) doubling[0][j] = -1;
  dfs(0);

  vector<int> inv_dist(N, -1);
  for (int i = 0; i < 2 * N - 3; i++) {
    if (used.contains({A[i], B[i]})) continue;
    int d = dist(A[i], B[i]);
    if (inv_dist[d] == -1) {
      inv_dist[d] = i;
    } else {
      cout << d + 1 << endl;
      // cerr << A[i] << " " << B[i] << " " << A[inv_dist[d]] << " " << B[inv_dist[d]] << endl;
      print_path(A[i], B[i]);
      cout << endl;
      print_path(A[inv_dist[d]], B[inv_dist[d]]);
      cout << endl;
      break;
    }
  }
}

詳細信息

Test #1:

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

input:

4
1 2
1 3
1 4
2 3
2 4

output:

3
1 2 4
1 2 3

result:

ok 

Test #2:

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

input:

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

output:

3
1 2 5
1 2 3

result:

ok 

Test #3:

score: 0
Accepted
time: 1ms
memory: 5672kb

input:

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

output:

5
2 5 6 3 4
1 2 5 6 3

result:

ok 

Test #4:

score: -100
Wrong Answer
time: 1ms
memory: 5608kb

input:

40
1 16
1 40
2 4
2 16
2 36
3 25
3 38
4 1
4 13
5 11
5 27
6 4
7 5
7 11
8 10
8 14
8 24
9 34
10 20
12 35
13 2
14 10
14 20
15 18
15 28
15 31
16 6
16 13
17 5
17 11
17 27
18 9
19 1
19 4
19 16
20 24
21 12
21 33
21 35
22 38
23 12
23 21
25 28
25 31
25 34
25 38
26 14
26 20
27 7
27 11
28 3
28 31
29 16
29 19
30 ...

output:

1
3 -1 38
3 -1 25

result:

wrong answer Integer 1 violates the range [3, 40]