QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#632254#9230. Routing K-CodesFDUdululuWA 120ms24752kbC++203.8kb2024-10-12 13:07:202024-10-12 13:07:21

Judging History

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

  • [2024-10-12 13:07:21]
  • 评测
  • 测评结果:WA
  • 用时:120ms
  • 内存:24752kb
  • [2024-10-12 13:07:20]
  • 提交

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;
    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 (dep[y] >= maxn[x]) {
            mexn[x] = maxn[x];
            maxn[x] = dep[y];
        } else if (dep[y] >= mexn[x])
            mexn[x] = dep[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]);
    // if(!(p+q)) b[x]+=1;
    // cout<<"? "<<x<<" "<<k[x]<<" "<<b[x]<<"\n";
}

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];

        k[x] -= 2ll * k[y], b[x] -= b[y];
        if (y == u[x]) {
            if (w[x])
                b[x] -= max(k[v[x]], k[w[x]]);
            else
                b[x] -= k[v[x]];
        } else {
            b[x] -= k[y];
        }
        dep[x]++;
        int len;
        if (maxn[x] == dep[y])
            len = mexn[x];
        else
            len = maxn[x];
        len = len - dep[x] + 1;
        len = max(len, depfa + 1);

        // cout<<"# "<<k[x]<<" "<<k[y]<<"\n";

        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]);
        // if(!(p+q)) b[y]+=1;

        // cout<<"! "<<y<<" "<<k[y]<<" "<<b[y]<<"|"<<o<<" "<<k[o]<<" "<<b[o]<<"|";

        // cout<<ans<<"\n";

        if (deg[y] == 1) {
            if (len - 1 < 32)
                ans = min(ans, k[o] + b[o]);
        } else if (deg[y] == 2) {
            if (len < 32)
                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: 2ms
memory: 9884kb

input:

4 3
1 2
1 3
1 4

output:

6

result:

ok single line: '6'

Test #2:

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

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: 3596kb

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: 120ms
memory: 24752kb

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:

-8511489449355482834

result:

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