QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#632633 | #9230. Routing K-Codes | FDUdululu | WA | 137ms | 24748kb | C++20 | 3.8kb | 2024-10-12 13:41:37 | 2024-10-12 13:41:37 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
const ll inf = 9e18;
int n, m;
pair<int, int> e[N];
vector<int> son[N];
int deg[N];
ll k[N], b[N], ans;
int u[N], v[N], w[N];
int dep[N], maxn[N], mexn[N], dia;
void dfs1(int x, int fa) {
dep[x] = dep[fa] + 1;
k[x] = 1;
b[x] = 0;
maxn[x] = mexn[x] = dep[x];
for (auto y : son[x]) {
if (y == fa)
continue;
dfs1(y, x);
if (v[x])
w[x] = y;
else if (u[x])
v[x] = y;
else
u[x] = y;
if (maxn[y] >= maxn[x]) {
mexn[x] = maxn[x];
maxn[x] = maxn[y];
} else if (maxn[y] >= mexn[x])
mexn[x] = maxn[y];
}
dia = max(dia, maxn[x] - dep[x] + mexn[x] - dep[x] + 1);
if (k[u[x]] < k[v[x]])
swap(u[x], v[x]);
if (k[u[x]] < k[w[x]])
swap(u[x], w[x]);
int o = u[x], p = v[x], q = w[x];
k[x] += 2ll * (k[o] + k[p] + k[q]);
b[x] += (b[o] + b[p] + b[q]) + (k[p] + k[q]);
// cout<<"? "<<x<<" "<<k[x]<<" "<<b[x]<<"\n";
}
void calc(int x, int y) {
int u = 0, v = 0;
for (auto z : son[x]) {
if (z == y)
continue;
if (u)
v = z;
else
u = z;
}
k[x] = 1 + 2ll * (k[u] + k[v]);
b[x] = (b[u] + b[v]) + min(k[u], k[v]);
}
void dfs2(int x, int fa, int depfa) {
// cout<<"? "<<x<<" "<<k[x]<<" "<<b[x]<<" "<<u[x]<<"\n";
for (auto y : son[x]) {
if (y == fa)
continue;
ll old_k_x = k[x], old_b_x = b[x];
calc(x, y);
int len = 0;
if (maxn[x] == maxn[y])
len = mexn[x];
else
len = maxn[x];
len = len - dep[x] + 1;
len = max(len, depfa + 1);
ll old_k_y = k[y], old_b_y = b[y];
// int o = x, p = u[y], q = v[y];
// if (k[o] < k[p])
// swap(o, p);
// if (k[o] < k[q])
// swap(o, q);
// k[y] = 1 + 2ll * (k[o] + k[p] + k[q]);
// b[y] = (b[o] + b[p] + b[q]) + (k[p] + k[q]);
// cout<<"! "<<y<<" "<<k[y]<<" "<<b[y]<<"|"<<o<<" "<<k[o]<<" "<<b[o]<<"|";
// cout<<ans<<"\n";
if (deg[y] == 1) {
if (len - 1 < 32) {
calc(y, -1);
ans = min(ans, k[x] + b[x]);
}
} else if (deg[y] == 2) {
if (len < 32) {
calc(y, -1);
ans = min(ans, k[y] + b[y]);
}
}
dfs2(y, x, len);
k[y] = old_k_y, b[y] = old_b_y;
k[x] = old_k_x, b[x] = old_b_x;
}
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
e[i] = make_pair(x, y);
}
if (m >= n) {
cout << "NIE\n";
return;
}
for (int i = 1; i < n; i++) {
int x = e[i].first;
int y = e[i].second;
son[x].push_back(y);
son[y].push_back(x);
deg[x]++;
deg[y]++;
}
for (int i = 1; i <= n; i++) {
if (deg[i] > 3) {
cout << "NIE\n";
return;
}
}
dfs1(1, 0);
if (dia / 2 >= 32) {
cout << "NIE\n";
return;
}
ans = inf;
if (deg[1] == 1) {
if (maxn[1] - 2 < 32)
ans = min(ans, k[u[1]] + b[u[1]]);
} else if (deg[1] == 2) {
if (maxn[1] - 1 < 32)
ans = min(ans, k[1] + b[1]);
}
dfs2(1, 0, 0);
if (ans == inf) {
cout << "NIE\n";
return;
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n = 1;
// cin>>n;
while (n--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 9816kb
input:
4 3 1 2 1 3 1 4
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 1ms
memory: 5632kb
input:
4 6 1 2 2 3 3 4 4 1 1 3 2 4
output:
NIE
result:
ok single line: 'NIE'
Test #3:
score: 0
Accepted
time: 2ms
memory: 5648kb
input:
10 10 9 3 5 10 1 4 10 8 8 3 7 3 9 6 8 6 7 2 4 5
output:
NIE
result:
ok single line: 'NIE'
Test #4:
score: 0
Accepted
time: 108ms
memory: 24284kb
input:
200000 199999 172774 188052 195063 155577 38023 148303 30605 155047 170238 19344 46835 58255 154268 109062 197059 116041 136424 12058 42062 149807 109545 115380 132322 51106 16706 162612 62234 31319 195735 80435 173898 84051 19876 102910 186924 9136 123094 117097 145054 173049 137364 152731 82662 18...
output:
NIE
result:
ok single line: 'NIE'
Test #5:
score: 0
Accepted
time: 137ms
memory: 24748kb
input:
200000 199999 168192 30733 164413 46673 175210 2329 69389 67102 33991 152553 91061 55265 166724 117726 90148 157176 34831 12213 60756 105488 121891 155192 82202 155666 102083 156661 38968 75200 190004 107321 72436 92732 64314 65004 39210 91106 70455 173568 179742 29844 126232 19552 133509 110602 131...
output:
20980030044349
result:
ok single line: '20980030044349'
Test #6:
score: 0
Accepted
time: 0ms
memory: 9748kb
input:
4 3 1 2 1 3 1 4
output:
6
result:
ok single line: '6'
Test #7:
score: -100
Wrong Answer
time: 125ms
memory: 24004kb
input:
199021 199020 189105 111001 147300 187047 67395 102319 25317 152109 56495 115535 11656 19974 119178 6528 84571 159100 198873 156684 21161 163040 91720 165273 168305 140152 104884 119802 131 177991 35930 74934 27528 278 177640 162738 118439 69472 20365 85043 184995 54207 64542 188847 97881 167717 188...
output:
2438969401639
result:
wrong answer 1st lines differ - expected: 'NIE', found: '2438969401639'