QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#645322 | #9230. Routing K-Codes | ucup-team2432# | WA | 78ms | 24488kb | C++20 | 3.8kb | 2024-10-16 17:45:24 | 2024-10-16 17:45:24 |
Judging History
answer
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#define sz(vec) ((ll)vec.size())
#define all(vec) vec.begin(),vec.end()
#define umap __gnu_pbds::gp_hash_table
using ll = long long;
using i128 = __int128;
using db = double;
#define Emu Smile
using namespace std;
void chmax(auto &a, auto b) { if (a < b) a = b; }
void chmin(auto &a, auto b) { if (a > b) a = b; }
inline int lbt(int x) { return x & (-x); }
const int P = 1e9+7, B = 1000;
const ll inf = 1e16;
void no() {
cout << "NIE\n";
}
struct Info {
ll mind, addd, depd;
Info():mind(0),addd(0),depd(0){}
Info friend operator + (Info li,Info ri) {
Info ret;
ret.depd = max(li.depd, ri.depd) + 1;
ret.mind = li.mind + ri.mind + li.addd + ri.addd + min(li.addd, ri.addd) + 1;
ret.addd = (li.addd + ri.addd) * 2 + 1;
return ret;
}
};
void solve() {
int n,m; cin >> n >> m;
vector eg(n, vector<int>());
for (int i = 0, x, y; i < m; ++i) {
cin >> x >> y; x--, y--;
eg[x].emplace_back(y), eg[y].emplace_back(x);
}
if (m > n-1) { return no(); }
int root = 0;
for (int i = 0; i < n; ++i) {
if (sz(eg[i]) > 3) { return no(); }
if (sz(eg[i]) <= 2) root = i;
}
if (!root) return no();
vector<int> maxdep(n);
{
vector<int> tmp(n); int maxd = 0;
auto dfs1 = [&](auto &self,int u,int fa) -> void {
if (tmp[u] > tmp[maxd]) maxd = u;
for (auto v : eg[u]) {
if (fa == v) continue;
tmp[v] = tmp[u] + 1;
self(self, v, u);
}
};
dfs1(dfs1, root, -1);
auto dfs2 = [&](auto &self,int u,int fa) -> void {
chmax(maxdep[u], tmp[u]);
if (tmp[u] > tmp[maxd]) maxd = u;
for (auto v : eg[u]) {
if (fa == v) continue;
tmp[v] = tmp[u] + 1;
self(self, v, u);
}
};
tmp.assign(n, 0);
dfs2(dfs2, maxd, -1);
tmp.assign(n, 0);
dfs2(dfs2, maxd, -1);
}
root = -1;
for (int i = 0; i < n; ++i) {
if (sz(eg[i]) == 1) {
if (maxdep[i] < 33) {
root = i; break;
}
} else if (sz(eg[i]) == 2) {
if (maxdep[i] < 32) {
root = i; break;
}
}
}
if (root == -1) {
return no();
}
vector<Info> f(n+1);
{
auto dfs = [&](auto &self,int u) -> void {
for (auto v : eg[u]) {
eg[v].erase(find(all(eg[v]), u));
self(self, v);
}
if (sz(eg[u]) == 0) {
f[u].mind = 1, f[u].addd = 1, f[u].depd = 0;
} else if (sz(eg[u]) == 1) {
int v = eg[u][0];
f[u] = f[v] + Info();
} else {
int v1 = eg[u][0], v2 = eg[u][1];
f[u] = f[v1] + f[v2];
}
// cout << u << " " << f[u].mind << " " << f[u].addd << '\n';
};
dfs(dfs, root);
}
ll ret = f[root].mind;
if (sz(eg[root]) == 1) {
int v = eg[root][0];
chmin(ret, f[v].mind);
}
{
vector<Info> g(n);
auto check = [&](int x) {
if (sz(eg[x]) == 0) {
chmin(ret, g[x].mind);
} else {
chmin(ret, (f[x] + g[x]).mind);
}
};
auto dfs = [&](auto &self,int u) -> void {
// cout << u << " " << f[u].mind << " " << g[u].mind << " " << (f[u] + g[u]).mind << '\n';
if (sz(eg[u]) == 1) {
int v = eg[u][0];
g[v] = g[u] + Info();
check(v);
} else if (sz(eg[u]) == 2){
int v1 = eg[u][0], v2 = eg[u][1];
g[v1] = g[u] + f[v2];
check(v1);
g[v2] = g[u] + f[v1];
check(v2);
}
for (auto v : eg[u]) {
if (sz(eg[v]) == 0) {
if (maxdep[v] >= 33) continue;
} else {
if (maxdep[v] >= 32) continue;
}
self(self, v);
}
};
dfs(dfs, root);
}
if (ret == inf) {
return no();
} else {
cout << ret << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
// init();
cout << fixed << setprecision(10);
int OuO = 1;
// cin >> OuO;
for (int piastic = 0; piastic < OuO; ++piastic) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3560kb
input:
4 3 1 2 1 3 1 4
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3560kb
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: 0ms
memory: 3576kb
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: 71ms
memory: 15704kb
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: -100
Wrong Answer
time: 78ms
memory: 24488kb
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:
145899197826
result:
wrong answer 1st lines differ - expected: '20980030044349', found: '145899197826'