QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#655292#2550. Lion and ZebravaleriuWA 59ms11972kbC++204.6kb2024-10-19 02:34:252024-10-19 02:34:25

Judging History

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

  • [2024-10-19 02:34:25]
  • 评测
  • 测评结果:WA
  • 用时:59ms
  • 内存:11972kb
  • [2024-10-19 02:34:25]
  • 提交

answer

#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;

using ll = long long;
using ld = long double;

//#define int ll
#define sz(x) ((int)(x).size())

using pii = pair<int,int>;
using tii = tuple<int,int,int>;

struct Diam {
   int x, y, D, xdist;
   Diam operator + (const Diam& a) const {
      if(a.D > D) return a;
      return *this;
   }
};
const int nmax = 1e5 + 5;

vector<int> g[nmax];

Diam diam(int node, int f) {
   Diam curr = Diam{node, node, 0, 0};
   for(auto &x : g[node]) {
      if(x == f) continue;
      auto R = diam(x, node);
      R.xdist++;
      Diam U = Diam{R.x, curr.x, curr.xdist + R.xdist, R.xdist};
      Diam V = Diam{curr.x, R.x, curr.xdist + R.xdist, curr.xdist};
      if(R.xdist < curr.xdist) U = V;
      curr = R + curr + U;
   }
   return curr;
}

int occ[nmax];

void mkdist(int node, int f, int D, vector<int> &height, vector<int>& dist) {
   dist[node] = D;
   height[node] = 0;
   for(auto x : g[node]) {
      if(x == f || occ[x]) continue;
      mkdist(x, node, D + 1, height, dist);
      height[node] = max(height[node], height[x] + 1);
   }
   return;
}

int jump[nmax], p[nmax];
void initlca(int node, int f, const vector<int>& d) {
   jump[node] = p[node] = f;
   if(d[jump[jump[f]]] + d[f] == 2 * d[jump[f]]) jump[node] = jump[jump[f]];
   for(auto x : g[node]) {
      if(x == f) continue;
      initlca(x, node, d);
   }
   return;
}

int twoable[nmax];

signed main() {
   cin.tie(0) -> sync_with_stdio(0);
   int n, q;
   cin >> n >> q;
   
   for(int i = 1, x, y; i < n; i++) {
      cin >> x >> y;
      g[x].emplace_back(y);
      g[y].emplace_back(x);
   }
   
   for(int i = 1; i <= n; i++) {
      if(sz(g[i]) == 1) {
         twoable[g[i][0]] = 1;
      }
   }
   
   auto [L, R, a, b] = diam(1, 1);
   //cerr << L << ' ' << R << '\n';
   
   vector<int> Ldist(n + 2), Rdist(n + 2), height(n + 2), tmp(n + 2), H;
   mkdist(L, L, 0, tmp, Ldist);
   mkdist(R, R, 0, tmp, Rdist);
   initlca(L, L, Ldist);
   for(int i = 1; i <= n; i++) {
      if(i == L) continue;
      assert(Ldist[i] == Ldist[p[i]] + 1);
   }
   H = Ldist;
   //cerr << Rdist[L] << '\n';
   
   auto kth = [&](int node, int k) {
      //cerr << "skazhy menya\t" << H[node] << ' ' << k << '\n';;
      int targ = H[node] - k;
      while(Ldist[node] > targ) {
         //if(node != 132)
            //cerr << node << ' ' << jump[node] << ' ' << H[node] << ' ' << targ << '\n';
         if(H[jump[node]] > targ) node = jump[node];
         else node = p[node];
      }
      //cerr << "esli ty menya lyubish'\n";
      return node;
   };
   
   
   for(int i = 1; i <= n; i++) {
      if(Ldist[i] + Rdist[i] == Ldist[R]) {
         occ[i] = 1;
      }
   }
   
   
   for(int i = 1; i <= n; i++) {
      if(Ldist[i] + Rdist[i] == Ldist[R]) {
         for(auto x : g[i]) {
            if(Ldist[x] + Rdist[x] != Ldist[x])
               mkdist(x, i, 0, height, tmp);
         }
      }
   }
   
   for(int i = 0, node, d; i < q; i++) {
      cin >> node >> d;
      
      if(d == 1) {
         cout << "1\n";
      }
      else if(d == 2) {
         if(twoable[node])
            cout << 3 << '\n';
         else
            cout << 2 << '\n';
      }
      else {
         if(Ldist[node] > Rdist[node]) swap(Ldist, Rdist), swap(L, R);
         int tchain = (Rdist[node] + Ldist[node] - Ldist[R]) / 2;
         
         //cerr << "pula\n";
         
         if(Ldist[node] < d)
            cout << d + Ldist[node] - 2 * tchain << '\n';
         else {
         //cerr << "pula2\n";
            if(d - tchain * 2 > 2)
               cout << d + Ldist[node] - 2 * tchain << '\n';
            else {
         //cerr << "pula3\n";
               if(d % 2 == 1) {
                  auto T = kth(node, (d - 3) / 2);
                  cout << 3 + (d - 3) / 2 + height[T] << '\n';
               } 
               else {
         //cerr << "pula4\n";
                  auto T = kth(node, (d - 3) / 2), P = kth(node, (d - 2) / 2);
                  if(height[T] + 1 >= height[P]) {
                     cout << 4 + (d - 4) / 2 + height[T] << '\n';
                  }
                  else if (height[P] + (d - 2) / 2 < d){
                     cout << 2 + (d - 2) / 2 + height[P] << '\n';
                  }
                  else {
                     cout << 4 + (d - 4) / 2 + height[T] << '\n';
                  }
               }
            }
         }
         
      }
   }
   
   
}

/**
      Ovaj podrum ima hladan sjaj\Iako je to bolje od gomile drugih
--
*/ 

详细

Test #1:

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

input:

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

output:

4
1

result:

ok 2 number(s): "4 1"

Test #2:

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

input:

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

output:

2
5

result:

ok 2 number(s): "2 5"

Test #3:

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

input:

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

output:

5
3

result:

ok 2 number(s): "5 3"

Test #4:

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

input:

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

output:

3
2
5
4
5
1
1
4
3

result:

ok 9 numbers

Test #5:

score: 0
Accepted
time: 2ms
memory: 3568kb

input:

175 7862
70 167
102 128
3 76
46 42
104 112
53 46
52 99
172 116
48 158
40 138
11 103
26 8
59 147
163 88
71 133
130 134
98 73
115 104
28 166
5 173
148 61
38 45
173 73
96 10
36 58
124 59
94 73
137 136
79 164
21 11
27 161
9 152
37 101
123 131
145 68
111 156
153 51
61 73
4 93
54 157
33 69
47 12
144 115
1...

output:

1
2
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
1
2
28
29
30
31
32
33
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
1
2
27
28
29
30
31
32
33
34
35
36
38
39
40
4...

result:

ok 7862 numbers

Test #6:

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

input:

121 3803
42 27
26 2
120 41
72 6
39 35
98 53
102 79
21 74
90 98
88 13
18 107
68 91
53 16
52 112
111 39
46 12
116 87
10 77
22 80
115 10
55 17
109 49
89 61
60 11
86 1
65 47
79 24
20 76
35 25
63 84
73 28
47 60
85 88
104 55
54 97
62 118
45 115
110 18
17 75
96 93
37 14
57 67
7 62
30 21
24 78
81 90
25 69
4...

output:

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

result:

ok 3803 numbers

Test #7:

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

input:

97 2467
13 69
63 62
45 53
48 79
28 7
4 25
12 54
94 92
39 10
82 48
47 87
84 9
25 17
46 43
26 8
61 96
88 55
69 86
96 41
2 13
22 45
8 23
74 28
40 89
71 7
15 81
60 49
58 90
19 67
97 4
95 39
55 51
67 46
81 42
44 7
72 66
29 44
36 73
49 36
50 32
91 78
43 75
9 21
27 20
68 12
53 74
65 71
93 29
66 63
1 22
34 ...

output:

1
2
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
1
2
12
13
14
15
16
17
18
19
20
21
22
23
24
25
27
28
29
30
31
32
33
34
1
2
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
1
2
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
29
30
31
32
33
34
1
2
11
12
13
14
15
16
17
18
1...

result:

ok 2467 numbers

Test #8:

score: 0
Accepted
time: 2ms
memory: 3624kb

input:

175 7862
95 145
169 90
166 160
9 168
35 166
149 173
156 85
8 19
120 88
96 117
155 66
101 10
63 94
151 120
92 4
28 129
106 141
135 151
59 121
137 157
103 46
5 135
107 41
18 21
150 63
133 37
26 13
121 112
123 101
162 72
154 106
69 62
19 150
14 146
67 49
21 79
93 64
148 20
172 156
131 17
97 104
108 119...

output:

1
2
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
1
2
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
55
56
57
58
59
60
1
2
3
4
5
6
7
8
9
10
1...

result:

ok 7862 numbers

Test #9:

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

input:

73 1419
48 1
51 28
46 36
33 28
55 17
7 4
50 66
38 53
56 42
47 21
21 30
32 50
19 26
39 45
66 73
37 28
63 10
41 29
68 69
8 24
29 62
53 37
9 72
24 68
18 43
5 65
42 71
58 49
26 8
22 13
57 16
11 22
40 41
65 7
6 56
36 20
3 34
69 70
59 18
4 27
25 19
49 59
10 55
44 35
2 47
61 9
73 58
71 28
16 28
60 63
72 15...

output:

1
2
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1
2
8
9
10
11
12
13
14
15
16
17
18
19
20
21
23
24
25
26
1
2
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2...

result:

ok 1419 numbers

Test #10:

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

input:

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

output:

4

result:

ok 1 number(s): "4"

Test #11:

score: -100
Wrong Answer
time: 59ms
memory: 11972kb

input:

100000 100000
6924 97704
51795 94079
85721 1451
51436 64102
22887 53289
62490 70035
53601 3516
35670 75950
99585 97911
10866 63419
36928 36457
72357 2042
9384 15284
31844 68380
60095 40587
47577 71274
7890 30139
12208 24775
31163 44315
9019 68509
79484 86871
77532 24228
80137 97728
54885 50661
91242...

output:

37
22
38
18
7
5
39
34
4
41
37
5
2
44
21
17
26
37
36
16
21
4
46
1
42
3
1
8
8
39
47
13
41
38
45
44
18
39
45
46
3
9
45
12
47
23
6
41
33
21
9
16
30
40
33
48
36
35
47
33
29
2
19
30
34
31
3
5
21
43
45
35
38
36
40
37
43
35
45
3
19
17
41
5
29
23
25
9
8
35
6
43
39
27
42
43
7
6
15
3
8
14
33
48
22
38
41
33
38
...

result:

wrong answer 2nd numbers differ - expected: '28', found: '22'