QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#632419#9230. Routing K-CodesFDUdululuWA 104ms24196kbC++203.8kb2024-10-12 13:22:052024-10-12 13:22:07

Judging History

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

  • [2024-10-12 13:22:07]
  • 评测
  • 测评结果:WA
  • 用时:104ms
  • 内存:24196kb
  • [2024-10-12 13:22:05]
  • 提交

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]);
    // 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];
        }
        int len;
        if (maxn[x] == maxn[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: 0ms
memory: 9828kb

input:

4 3
1 2
1 3
1 4

output:

6

result:

ok single line: '6'

Test #2:

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

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

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: 76ms
memory: 24012kb

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: 104ms
memory: 24196kb

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:

20981375941798

result:

wrong answer 1st lines differ - expected: '20980030044349', found: '20981375941798'