QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#284588#7936. Eliminate Treeucup-team1134#WA 86ms18376kbC++231.8kb2023-12-16 13:57:522023-12-16 13:57:54

Judging History

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

  • [2023-12-16 13:57:54]
  • 评测
  • 测评结果:WA
  • 用时:86ms
  • 内存:18376kb
  • [2023-12-16 13:57:52]
  • 提交

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{
        dp[u]+=(X-1)*2+1;
    }
}

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{
            sz[u]=0;
            dp[u]+=(X-1)*2+1;
        }
        
        solve(to,u);
        
        X++;
        
        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: 100
Accepted
time: 0ms
memory: 10472kb

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: 10548kb

input:

4
1 2
2 3
3 4

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Wrong Answer
time: 86ms
memory: 18376kb

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'