QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#860181 | #9678. 网友小 Z 的树 | lfxxx | 16 | 771ms | 21324kb | C++17 | 2.8kb | 2025-01-18 11:00:38 | 2025-01-18 11:00:38 |
Judging History
answer
#include "diameter.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
constexpr int inf = 1e9, mod = 998244353;
int id[20][20], a[20][20];
int qpow(int b, int k)
{
int res = 1;
while (k) {
if (k & 1) res = (ll) res * b % mod;
b = (ll) b * b % mod;
k >>= 1;
}
return res;
}
pii find(vector<int>v)
{
if (v.size() == 1) return pii(v[0], v[0]);
if (v.size() == 2) return pii(v[0], v[1]);
if (v.size() == 3) {
if (in(v[0], v[1], v[2])) return pii(v[1], v[2]);
if (in(v[1], v[0], v[2])) return pii(v[0], v[2]);
return pii(v[0], v[1]);
}
assert(v.size() == 5);
// for (int x : v) cerr << x << ' '; cerr << '\n';
int n = 0, cnt = 0;
for (int i = 0; i < 5; ++i) {
for (int j = i + 1; j < 5; ++j) {
id[i][j] = ++cnt;
}
}
for (int i = 0; i < 5; ++i) {
for (int j = i + 1; j < 5; ++j) {
for (int k = j + 1; k < 5; ++k) {
++n;
a[n][id[i][j]] = a[n][id[i][k]] = a[n][id[j][k]] = 1;
a[n][cnt + 1] = query(v[i], v[j], v[k]);
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = i + 1; j <= n; ++j) {
if (a[j][i]) {
swap(a[i], a[j]);
break;
}
}
for (int j = 1; j <= n; ++j) {
if (j == i) continue;
int c = (ll) a[j][i] * qpow(a[i][i], mod - 2) % mod;
for (int k = i; k <= n + 1; ++k) {
a[j][k] = (a[j][k] - (ll) c * a[i][k]) % mod;
}
}
}
for (int i = 1; i <= n; ++i) {
a[i][n + 1] = ((ll) a[i][n + 1] * qpow(a[i][i], mod - 2) % mod + mod) % mod;
a[i][i] = 0;
}
cnt = 0;
pair<int, pii>mx;
for (int i = 0; i < 5; ++i) {
for (int j = i + 1; j < 5; ++j) {
++cnt;
mx = max(mx, make_pair(a[cnt][n + 1], pii(v[i], v[j])));
}
}
return mx.second;
}
pii find_diameter(int subid, int n) {
if (n <= 3) {
vector<int>v;
for (int i = 1; i <= n; ++i) v.emplace_back(i);
return find(v);
}
if (n == 4) {
pair<int, tuple<int, int, int>>t[4];
t[0] = {query(1, 2, 3), {1, 2, 3}};
t[1] = {query(1, 2, 4), {1, 2, 4}};
t[2] = {query(1, 3, 4), {1, 3, 4}};
t[3] = {query(2, 3, 4), {2, 3, 4}};
pair<int, tuple<int, int, int>>mx = *max_element(t, t + 4);
int x = get<0>(mx.second), y = get<1>(mx.second), z = get<2>(mx.second);
int k = 10 - x - y - z;
int d1 = query(x, y, k), d2 = query(x, z, k), d3 = query(y, z, k);
if (max({d1, d2, d3}) == d1) return pii(x, y);
if (max({d1, d2, d3}) == d2) return pii(x, z);
return pii(y, z);
}
vector<int>v(2);
v[0] = 1, v[1] = 2;
int id = 2;
n -= 2;
for (int i = 1; i <= n / 3; ++i) {
v.resize(5);
v[2] = ++id, v[3] = ++id, v[4] = ++id;
auto [x, y] = find(v);
v[0] = x, v[1] = y;
}
v.resize(2);
for (int i = n / 3 * 3 + 1; i <= n; ++i) {
v.emplace_back(++id);
}
id = 0;
while (v.size() < 5) {
++id;
if (id != v[0] && id != v[1]) {
v.emplace_back(id);
}
}
return find(v);
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 16
Accepted
Test #1:
score: 16
Accepted
time: 24ms
memory: 16224kb
input:
1 100 25 1 3 2 18 3 8 4 18 5 14 6 22 7 18 8 10 9 11 10 12 11 25 12 16 13 11 14 17 15 17 16 25 17 2 18 20 19 18 20 12 21 1 22 17 23 14 24 1 50 1 37 2 27 3 10 4 25 5 16 6 17 7 10 8 36 9 16 10 6 11 48 12 2 13 28 14 30 15 10 16 44 17 31 18 1 19 6 20 7 21 30 22 42 23 45 24 23 25 27 26 39 27 45 28 48 29 4...
output:
correct
result:
ok Correct
Subtask #2:
score: 0
Time Limit Exceeded
Dependency #1:
100%
Accepted
Test #2:
score: 15
Accepted
time: 771ms
memory: 21324kb
input:
2 2006 42 1 32 2 4 3 6 4 29 5 1 6 42 7 10 8 16 9 6 10 25 11 42 12 8 13 36 14 8 15 17 16 3 17 6 18 21 19 23 20 31 21 42 22 6 23 32 24 7 25 27 26 34 27 31 28 6 29 41 30 20 31 9 32 7 33 3 34 5 35 5 36 1 37 8 38 14 39 15 40 12 41 22 95 1 94 2 88 3 13 4 71 5 37 6 45 7 87 8 24 9 76 10 54 11 69 12 95 13 90...
output:
correct
result:
ok Correct
Test #3:
score: 0
Time Limit Exceeded
input:
2 100 10000 5442 1084 1084 8984 8984 3299 3299 6385 6385 6079 6079 6806 6806 2300 2300 2996 2996 1765 1765 257 257 5537 5537 2337 2337 5445 5445 2873 2873 336 336 6307 6307 4968 4968 8078 8078 9944 9944 5675 5675 7896 7896 5943 5943 2412 2412 7448 7448 7852 7852 1684 1684 3437 3437 3980 3980 1919 19...
output:
result:
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #5:
0%
Subtask #7:
score: 0
Skipped
Dependency #6:
0%
Subtask #8:
score: 0
Skipped
Dependency #7:
0%
Subtask #9:
score: 0
Skipped
Dependency #8:
0%
Subtask #10:
score: 0
Skipped
Dependency #9:
0%
Subtask #11:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%