QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#363646 | #7936. Eliminate Tree | vmadhu99# | TL | 1ms | 3800kb | C++20 | 1.7kb | 2024-03-24 01:28:54 | 2024-03-24 01:28:55 |
Judging History
answer
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
using ll = long long;
using namespace std;
const int MOD = 1e9 + 7;
vector<vector<int>> adj;
int dfs(int node, int parent, bool wParent);
int main(){
int N;
cin >> N;
if (N == 1) {
cout << 2 << endl;
return 0;
}
for (int i = 0; i < N; ++i)
{
adj.emplace_back();
}
for (int i = 0; i < N-1; ++i) {
int a, b;
cin >> a >> b;
a--; b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
cout << dfs(0, -1, false) << endl;
}
int dfs(int node, int parent, bool wParent) {
if (wParent) {
int sum = 0;
for (int child : adj[node]) {
if (child == parent)
continue;
sum += dfs(child, node, false);
}
return 1 + sum;
} else {
int N = adj[node].size();
if (N == 1 && parent != -1)
return 2;
vector<int> temp(N, 0);
int sum = 0;
for (int i = 0; i < N; ++i) {
if (adj[node][i] == parent)
continue;
int res = dfs(adj[node][i], node, false);
sum += res;
temp[i] = res;
}
int best = 1000000000;
for (int i = 0; i < N; ++i) {
if (adj[node][i] == parent)
continue;
int res = dfs(adj[node][i], node, true);
//cout << node << " " << res << " " << temp[i] << endl;
best = min(best, sum + res - temp[i]);
}
//cout << node << " " << best << endl;
return best;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3800kb
input:
5 1 2 2 3 3 4 3 5
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3564kb
input:
4 1 2 2 3 3 4
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: -100
Time Limit Exceeded
input:
196666 32025 108673 181779 17115 162570 134230 93003 39673 89144 1269 185919 154408 34896 65369 35860 44720 55698 1390 45520 189805 147867 124511 135459 132555 87303 18031 176314 59695 33352 130640 87272 39716 35749 108807 143493 94486 126512 116598 40212 70895 132216 80855 22241 188737 150354 17346...