QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#722246#9713. Kill the treeNanani#WA 72ms31768kbC++171.7kb2024-11-07 18:17:402024-11-07 18:17:41

Judging History

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

  • [2024-11-07 18:17:41]
  • 评测
  • 测评结果:WA
  • 用时:72ms
  • 内存:31768kb
  • [2024-11-07 18:17:40]
  • 提交

answer

//by 72
#include<bits/stdc++.h>
#define F(i, a, b) for(int i = a; i <= b; i ++)
#define Fd(i, a, b) for(int i = a; i >= b; i --)
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define int long long
using namespace std;

const int mod = 998244353;
const int N = 2e5 + 10;
const int inf = 1e18;
typedef array<int, 3> a3; 
typedef long long ll;

int n, m, fa[N], now[N], sz[N], mx[N], res[N];
vector<int> ed[N];

void dfs(int u, int f) {
    now[u] = u, sz[u] = 1;
    for(auto v : ed[u]) {
        if(v == f) continue;
        res[u] = 0;
        fa[v] = u;
        dfs(v, u);
        sz[u] += sz[v];
        mx[u] = max(mx[u], sz[v]);
        auto cal = [&](int &x) -> int {
            return max(mx[x], sz[u] - sz[x]);
        };
        auto work = [&](int &x) -> void {
            while(x != u && cal(fa[x]) < cal(x)) x = fa[x]; 
        };
        int x1 = now[u], x2 = now[v];
        work(x1), work(x2);
        if(cal(x1) < cal(x2)) {
            if(x1 != u && cal(x1) == cal(fa[x1])) res[u] = 1;
            now[u] = x1;
        } else {
            if(x2 != u && cal(x2) == cal(fa[x2])) res[u] = 1;
            now[u] = x2;
        }
        // cout << u << " " << v << " " << now[u] << " " << res[u] << "??\n";
    }
}

void sol() {
    cin >> n;
    F(i, 1, n - 1) {
        int u, v; cin >> u >> v;
        ed[u].push_back(v), ed[v].push_back(u);
    }
    dfs(1, 1);
    F(i, 1, n) {
        if(res[i]) cout << min(now[i], fa[now[i]]) << " " << max(fa[now[i]], now[i]) << "\n";
        else cout << now[i] << "\n";
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    F(i, 1, t) sol();
    return 0;
}
//sldl

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 72ms
memory: 31768kb

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
Wrong Answer
time: 61ms
memory: 23536kb

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:

52308
2
3
149366
5 39986
6 175524
45794
8
9
192952
11
12 15527
13
66009
15 14252
16
17
18
67537 71401
20
194399
22
23 112561
24
25
96681
27
28
29
30
31
32
33
34 121482
35
36
37 31661
38
39
40 167372
41
42 6258
43
44
121781
46
47
109092
49
50
51 7671
52
53
54 5024
55
56
57 61609
58
54657
79969
196331...

result:

wrong answer 55th numbers differ - expected: '162689', found: '121781'