QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#606242#9230. Routing K-CodesmaxplusWA 228ms23940kbC++202.4kb2024-10-02 23:47:102024-10-02 23:47:10

Judging History

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

  • [2024-10-02 23:47:10]
  • 评测
  • 测评结果:WA
  • 用时:228ms
  • 内存:23940kb
  • [2024-10-02 23:47:10]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

constexpr int N = 2e5;

vector<int> nei[N];
int rad[N], d[N], sz[N], par[N];
uint64_t dpd[N], dpdd[N], dpu[N], dpuu[N];

int calcd(int v, int p, int cd) {
  par[v] = p;
  int f = v; d[v] = 0, sz[v] = 1;
  for (auto u: nei[v]) if (u != p) {
    int fu = calcd(u, v, cd + 1);
    sz[v] += sz[u];
    if (d[v] < d[u] + 1) d[v] = d[u] + 1, f = fu;
  }
  rad[v] = max(rad[v], cd);
  return f;
}

void solved(int v, int p) {
  int c = 0;
  uint64_t cdpd = 1, cdpdd = 1, mn = 1e18;
  for (auto u: nei[v]) if (u != p) {
    solved(u, v);
    cdpd += dpd[u] + dpdd[u];
    cdpdd += dpdd[u] * 2;
    mn = min(mn, dpdd[u]);
    ++c;
  }
  if (c > 1) cdpd += mn;
  dpd[v] = cdpd;
  dpdd[v] = cdpdd;
}

void solveu(int v, int p) {
  uint64_t a = 0, b = 0, c = 0, sdpd = 0, sdpdd = 0;
  for (auto u: nei[v]) {
    auto cur = u == p? dpuu[v]: dpdd[u];
    cur > a? c = b, b = a, a = cur:
    cur > b? c = b, b = cur:
    cur > c? c = cur: 0;
    if (u != p) sdpd += dpd[u], sdpdd += dpdd[u];
    else sdpdd += dpuu[v];
  }
  for (auto u: nei[v]) if (u != p) {
    dpuu[u] = (sdpdd - dpdd[u]) * 2 + 1;
    dpu[u] = dpu[v] + sdpd - dpd[u] + sdpdd - dpdd[u] + (dpdd[u] == c? b: c) + 1;
    solveu(u, v);
  }
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n, m, r; cin >> n >> m;
  for (int i = 0, a, b; i < m; ++i) cin >> a >> b, --a, --b, nei[a].push_back(b), nei[b].push_back(a);
  bool ok = 1;
  for (int i = 0; i < n; ++i) ok &= nei[i].size() < 4;
  if (!ok || m > n - 1 || (r = calcd(0, 0, 0), r = calcd(r, r, 0), calcd(r, r, 0), d[r] > 64)) return cout << "NIE\n", 0;
  solved(r, r);
  solveu(r, r);
  uint64_t ans = 1e18;
  for (int i = 0; i < n; ++i) if (rad[i] <= 32) {
    vector<int> nls;
    int ls = -1;
    for (auto u: nei[i]) !~ls && nei[u].size() == 1? ls = u, void(): nls.push_back(u);
    if (nls.size() > 2) continue;
    uint64_t cans = 0, a = 0, b = 0;
    for (auto u: nls) {
      if (u == par[i]) {
        cans += dpu[i] + dpuu[i];
        dpuu[i] > a? b = a, a = dpuu[i]:
        dpuu[i] > b? b = dpuu[i]: 0;
      } else {
        cans += dpd[u] + dpdd[u];
        dpdd[u] > a? b = a, a = dpdd[u]:
        dpdd[u] > b? b = dpdd[u]: 0;
      }
    }
//    cout << i << ' ' << cans + b + 1 << '\n';
    ans = min(ans, cans + b + 1);
  }
  if (ans == 1e18) return cout << "NIE\n", 0;
  cout << ans;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 9972kb

input:

4 3
1 2
1 3
1 4

output:

6

result:

ok single line: '6'

Test #2:

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

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: 1ms
memory: 3604kb

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: -100
Wrong Answer
time: 228ms
memory: 23940kb

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:

34814606819805

result:

wrong answer 1st lines differ - expected: 'NIE', found: '34814606819805'