QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#284566 | #7936. Eliminate Tree | ucup-team1134# | WA | 0ms | 10468kb | C++23 | 1.9kb | 2023-12-16 13:52:33 | 2023-12-16 13:52:33 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005,INF=1<<30;
vector<int> G[MAX];
int dp[MAX],sz[MAX];
int ans=INF;
void make(int u,int p){
int X=0;
for(int to:G[u]){
if(to==p) continue;
make(to,u);
dp[u]+=dp[to];
if(sz[to]) X++;
}
if(X==0){
sz[u]=1;
}else if(X==1){
dp[u]++;
}else{
dp[u]+=(X-1)*2;
}
}
void solve(int u,int p){
dp[u]=0;
int X=0;
for(int to:G[u]){
dp[u]+=dp[to];
if(sz[to]) X++;
}
if(X==0){
chmin(ans,dp[u]+2);
}else{
chmin(ans,dp[u]+(X-1)*2+1);
}
for(int to:G[u]){
if(to==p) continue;
int a=dp[u],b=dp[to],c=sz[u],d=sz[to];
dp[u]-=dp[to];
if(sz[to]) X--;
if(X==0){
sz[u]=1;
}else if(X==1){
sz[u]=0;
dp[u]++;
}else{
sz[u]=0;
dp[u]+=(X-1)*2;
}
solve(to,u);
dp[u]=a;
dp[to]=b;
sz[u]=c;
sz[to]=d;
}
}
int main(){
std::ifstream in("text.txt");
std::cin.rdbuf(in.rdbuf());
cin.tie(0);
ios::sync_with_stdio(false);
int N;cin>>N;
for(int i=0;i<N-1;i++){
int a,b;cin>>a>>b;a--;b--;
G[a].push_back(b);
G[b].push_back(a);
}
make(0,-1);
solve(0,-1);
cout<<ans<<endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 10468kb
input:
5 1 2 2 3 3 4 3 5
output:
2
result:
wrong answer 1st numbers differ - expected: '4', found: '2'