QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#768736#9713. Kill the treeIllusionaryDominance#RE 85ms35048kbC++201.9kb2024-11-21 14:07:082024-11-21 14:07:08

Judging History

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

  • [2024-11-21 14:07:08]
  • 评测
  • 测评结果:RE
  • 用时:85ms
  • 内存:35048kb
  • [2024-11-21 14:07:08]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 200000 + 5;

int N;
struct Edge{
    int y, prev;
}e[MAX_N << 1];
int elast[MAX_N], ecnt;
int fa[MAX_N], sz[MAX_N], ms[MAX_N];
vector <int> ans[MAX_N];
char vis[MAX_N];

void Build(int x, int y) {
    ecnt ++;
    e[ecnt].y = y;
    e[ecnt].prev = elast[x];
    elast[x] = ecnt;
}

void dfs(int u, int fath) {
    sz[u] = 1;
    ms[u] = 0;
    fa[u] = fath;
    for (int i = elast[u]; i; i = e[i].prev) {
        int v = e[i].y;
        if (v != fath) {
            dfs(v, u);
            sz[u] += sz[v];
            ms[u] = max(ms[u], sz[v]);
        }
    }
    for (int i = elast[u]; i; i = e[i].prev) {
        int v = e[i].y;
        if (v == fath) continue;
        for (auto x : ans[v]) {
            while (x != u && !vis[fa[x]] && max(ms[x], sz[u] - sz[x]) > sz[u] / 2) {
                vis[x = fa[x]] = 1;
            }
            if (max(ms[x], sz[u] - sz[x]) <= sz[u] / 2) {
                ans[u].emplace_back(x);
                if (!vis[fa[x]] && max(ms[fa[x]], sz[u] - sz[fa[x]]) <= sz[u] / 2) {
                    vis[fa[x]] = 1;
                    ans[u].emplace_back(fa[x]);
                }
            }
        }
    }
    if (ans[u].empty()) {
        vis[u] = 1;
        ans[u].emplace_back(u);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    
    cin >> N;
    for (int i = 1; i < N; i ++) {
        int x, y;
        cin >> x >> y;
        Build(x, y); Build(y, x);
    }
    dfs(1, 0);
    for (int i = 1; i <= N; i ++) {
        assert((int)ans[i].size() <= 2);
        sort(ans[i].begin(), ans[i].end());
        for (int j = 0; j < (int)ans[i].size(); j ++) {
            if (j) cout << ' ';
            cout << ans[i][j];
        }
        cout << '\n';
    }
    
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 85ms
memory: 35048kb

input:

200000
42924 18271
60536 154257
107175 95680
196335 71607
158051 155259
110869 30974
143595 43516
4228 138046
26249 183847
123888 199873
15009 25362
166527 175160
15257 67159
124779 199363
148904 54047
5988 18123
58281 40739
44562 143880
72819 132492
137782 29662
130741 78276
55073 93292
82700 18328...

output:

1 134385
45886 143670
106649 126220
44121 108393
5226 95313
116080 147311
141180
147983
7428 74120
161963
3879
178499
162544 171549
144413
127262
133093 188325
171802
43558
28179
125217 140604
186651
45633 176397
26425
3745 26982
30063
128965
19661 148036
141733
115691
3491
8560 36057
12536 195673
1...

result:

ok 300000 numbers

Test #2:

score: -100
Runtime Error

input:

199996
50563 93215
168370 114785
10263 27279
167398 102988
69456 154440
153717 68545
58173 55699
138929 65580
167561 150907
112977 167988
79291 640
48647 123744
72178 137000
137037 197496
118383 75650
122661 82805
123795 74071
33761 152979
196839 174414
142905 129453
90791 156172
58774 116668
54562 ...

output:


result: