답안 #540359

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
540359 2022-03-20T06:40:09 Z BadPenalty Logičari (COCI21_logicari) C++14
0 / 110
14 ms 30080 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl '\n'
#define all(x) x.begin(),x.end()
#define yes cout<<"Yes"<<endl
#define no cout<<"No"<<endl
const int N = 2e5,mod = 1e9+7;

vector<int>adj[N];
int head[N],par[N];
int root,special;
ll dp[N][2][2][2][2];
int st(int x)
{
    if(x==head[x])return x;
    return head[x] = st(head[x]);
}

void dfs (int x, int rod) {
    par[x] = rod;
    for (auto sus : adj[x]) {
        if (sus == rod) continue;
        dfs(sus, x);
    }
}

ll calc(int x,int me,int up,int rt,int sp)
{
    if(dp[x][me][up][rt][sp]!=-1)
        return dp[x][me][up][rt][sp];

    ll res = 1e9;

    bool tr = 1;
    if(x==root && me!=rt)tr = 0;
    if(x==special && me!=sp)tr = 0;
    if(x==special && rt &&up)tr = 0;
    if(!tr)
        return dp[x][me][up][rt][sp] = 1e9;

    bool cmp = 0;
    if(up)cmp = 1;
    if(x ==root&& sp)cmp = 1;
    if(x ==special&&rt)cmp = 1;

    ll sum = me;
    for(auto u:adj[x])
    {
        if(u==par[x])continue;
        sum+=calc(u,0,me,rt,sp);
    }

    if(cmp)
    {
        res = min(res,sum);
    }
    else
    {
        for(auto u:adj[x])
        {
            if(u==par[x])continue;
            ll val = sum - calc(u,0,me,rt,sp) + calc(u,1,me,rt,sp);
            res = min(res,val);
        }
    }
    return dp[x][me][up][rt][sp] = res;


}


int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n;
    cin>>n;

    for(int i = 0;i<=n;i++)
        head[i] = i;
    for(int i = 0;i<n;i++)
    {
        int a,b;
        cin>>a>>b;

        int pa = st(a);
        int pb = st(b);

        if(pa==pb)
        {
            root = b;
            special = a;
        }
        else
        {
            head[pa] = pb;
            adj[a].pb(b);
            adj[b].pb(a);
        }
    }
    dfs(root,0);
    ll ans = 1e18;

    memset(dp,-1,sizeof dp);
    for(int rt = 0;rt<=1;rt++)
    {
        for(int sp = 0;sp<=1;sp++)
        {
            ans = min(ans,calc(root,rt,0,rt,sp));
        }
    }
    if(ans==1e18)
        cout<<-1<<endl;
    else
        cout<<ans<<endl;
    return 0;
}
/*

*/
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 30036 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 30080 KB Output is correct
2 Correct 13 ms 30036 KB Output is correct
3 Correct 14 ms 30076 KB Output is correct
4 Correct 13 ms 30036 KB Output is correct
5 Incorrect 13 ms 30016 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 30080 KB Output is correct
2 Correct 13 ms 30036 KB Output is correct
3 Correct 14 ms 30076 KB Output is correct
4 Correct 13 ms 30036 KB Output is correct
5 Incorrect 13 ms 30016 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 30036 KB Output isn't correct
2 Halted 0 ms 0 KB -