QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#820441 | #4599. Orgrimmar | SGColin | AC ✓ | 590ms | 81412kb | C++20 | 1.2kb | 2024-12-18 21:27:34 | 2024-12-18 21:27:34 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
inline int rd() {
int x = 0;
bool f = 0;
char c = getchar();
for (; !isdigit(c); c = getchar()) f |= (c == '-');
for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return f ? -x : x;
}
#define N 500007
vector<int> e[N];
int f[N][3];
// f[u][0] : not choose u
// f[u][1] : choose u, not choose u's son
// f[u][2] : choose u, choose u's son
void dfs(int u, int fa) {
int dlt = 0;
f[u][0] = 0;
f[u][1] = 1;
f[u][2] = 1;
for (auto v : e[u])
if (v != fa) {
dfs(v, u);
f[u][0] += max({f[v][0], f[v][1], f[v][2]});
f[u][1] += f[v][0];
dlt = max(dlt, f[v][1] - f[v][0]);
}
f[u][2] = f[u][1] + dlt;
}
inline void work() {
int n = rd();
for (int i = 1; i <= n; ++i) e[i].clear();
for (int i = 1; i < n; ++i) {
int u = rd(), v = rd();
e[u].pb(v); e[v].pb(u);
}
dfs(1, 1);
printf("%d\n", max({f[1][0], f[1][1], f[1][2]}));
}
int main() {
for (int t = rd(); t; --t) work();
exit(0);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 590ms
memory: 81412kb
input:
10 500000 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52 5...
output:
333334 499999 374859 374664 374655 374764 374530 374548 374848 374511
result:
ok 10 lines