Submission #741235

# Submission time Handle Problem Language Result Execution time Memory
741235 2023-05-13T22:46:36 Z Pichon5 Logičari (COCI21_logicari) C++17
0 / 110
199 ms 34328 KB
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <string>
#include <sstream>
#include <fstream>
#include <cmath>
#include <queue>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <bits/stdc++.h>
#define lcm(a,b) (a/__gcd(a,b))*b
#define jumanji ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define F first
#define S second
#define int long long
#define vi vector<int>
#define ll long long
using namespace std;
const int tam=1e5+5;
vector<vi>G;
int A;
int B;
bool vis[tam];
//find two nodes inside a cycle
void dfs(int u,int p){
    vis[u]=1;
    for(auto v:G[u]){
        if(v==p)continue;
        if(vis[v]){
            A=u;
            B=v;
            return;
        }
        dfs(v,u);
    }
}
//dp on tree
int dp[tam][2][2][2][2];
int f(int nodo,int color, int cP,int cA, int cB, int p=0){
    int &ans=dp[nodo][color][cP][cA][cB];
    if(nodo==A){
        if(color!=cA)return ans=1e6;
    }
    if(nodo==B){
        if(color!=cB)return ans=1e6;
    }
    if(nodo==B){
        if(cA and cP and p!=A)return ans=1e6;
        if(cA==0 and cP==0)return ans=1e6;
    }
    if(G[nodo].size()==1 and nodo!=A){
        return 0;
    }
    if(ans!=-1)return ans;
    ans=1e6;
    bool ok=true;
    if(nodo==B && cA)ok=false;
 
    if(cP or ok==false){
        int sum=0;
        for(auto v:G[nodo]){
            if(v==p)continue;
            sum+=f(v,0,color,cA,cB,nodo);
        }
        ans=min(ans,sum);
    }
    //cP == 0 entonces exactamente un hijo tiene que ser 1
    
    if(cP==0 && ok){
        // cout<<"mierda  "<<nodo<<endl;
        int sum=0;
        int puedo=0;
        int cant=0;
        for(auto v:G[nodo]){
            if(v==p)continue;
            cant++;
            int curr=f(v,0,color,cA,cB,nodo);
            sum+=curr;
            if(curr==1e6)continue;
            puedo++;
        }
        if(cant-puedo>1)return ans=1e6;
        int diff=cant-puedo;
        for(auto v:G[nodo]){
            if(v==p)continue;
            // cout<<nodo<<" "<<v<<" "<<endl;
            //quiero pintar de 1
            int curr=f(v,1,color,cA,cB,nodo);
            int curr0=f(v,0,color,cA,cB,nodo);
            if(curr==1e6)continue;
            if(diff==0){
                ans=min(ans,sum-curr+curr0+1);
            }else{
                // cout<<nodo<<" "<<sum-curr0+curr+1<<endl;
                if(curr0!=1e6)continue;
                ans=min(ans,sum-curr0+curr+1);
            }
        }
    }
    // cout<<nodo<<" = "<<ans<<endl;
    return ans;
}
signed main(){
    jumanji
    memset(dp,-1,sizeof dp);
    int n,a,b;
    cin>>n;
    G.assign(n+1,vi());
    vector<pair<int,int>>edges;
    for(int i=0;i<=n;i++){
        cin>>a>>b;
        G[a].pb(b);
        G[b].pb(a);
        edges.pb({a,b});
    }
    dfs(1,0);
    G.assign(n+1,vi());
    for(auto e:edges){
        if(e.F==A and e.S==B)continue;
        if(e.F==B and e.S==A)continue;
        // cout<<e.F<<" f "<<e.S<<endl;
        G[e.F].pb(e.S);
        G[e.S].pb(e.F);
    }
    //ambos 1s
    int res=1e6;
    // cout<<f(4,0,0,1,0,3)<<endl;
    // return 0;
    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++){
            int curr=f(A,i,j,i,j)+i;
            res=min(res,curr);
            // if(curr<=2){
            //     cout<<i<<" "<<j<<endl;
            //     cout<<curr<<endl;
            //     return 0;
            // }
        }
    }
    // cout<<A<<" "<<B<<endl;
    if(res==1e6){
        cout <<-1<<endl;
    }else{
        cout<<res<<endl;
    }
    return 0;
}  
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12756 KB Output is correct
2 Correct 6 ms 12856 KB Output is correct
3 Correct 5 ms 12856 KB Output is correct
4 Correct 6 ms 12852 KB Output is correct
5 Incorrect 199 ms 34328 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 12756 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 12756 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12756 KB Output is correct
2 Correct 6 ms 12856 KB Output is correct
3 Correct 5 ms 12856 KB Output is correct
4 Correct 6 ms 12852 KB Output is correct
5 Incorrect 199 ms 34328 KB Output isn't correct
6 Halted 0 ms 0 KB -