QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#391257 | #7936. Eliminate Tree | thanhs | WA | 71ms | 17040kb | C++14 | 1.3kb | 2024-04-16 15:06:17 | 2024-04-16 15:06:18 |
Judging History
answer
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define pb push_back
// #define endl '\n'
#define fastIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define setmin(x,y) x=min((x),(y))
#define setmax(x,y) x=max((x),(y))
#define fi first
#define se second
// mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
// int rand(int l,int r){return l+((hdp()%(r-l+1))+r-l+1)%(r-l+1);}
const int N = 2e5+5;
const int mod = 998244353;
vector<int> g[N];
int n,p[N],dp[N][2];
int DP(int u,bool b)
{
if(~dp[u][b]) return dp[u][b];
int sum=0;
for(auto v:g[u]) if(v!=p[u]) sum+=DP(v,0);
if(b)
{
dp[u][b]=(DP(u,0)+1,sum);
return dp[u][b];
}
else
{
dp[u][b]=sum+2;
for(auto v:g[u]) if(v!=p[u]) setmin(dp[u][b],sum-DP(v,0)+DP(v,1)+1);
return dp[u][b];
}
}
void dfs(int u)
{
for(auto v:g[u]) if(v!=p[u])
{
p[v]=u;
dfs(v);
}
}
signed main()
{
fastIO
memset(dp,-1,sizeof dp);
cin>>n;
for(int i=1;i<n;i++)
{
int u,v;
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
}
dfs(1);
cout<<min(DP(1,1)+1,DP(1,0));
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9844kb
input:
5 1 2 2 3 3 4 3 5
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 0ms
memory: 9920kb
input:
4 1 2 2 3 3 4
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: -100
Wrong Answer
time: 71ms
memory: 17040kb
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...
output:
138181
result:
wrong answer 1st numbers differ - expected: '138182', found: '138181'