Submission #741243

# Submission time Handle Problem Language Result Execution time Memory
741243 2023-05-13T22:54:45 Z Pichon5 Logičari (COCI21_logicari) C++17
0 / 110
191 ms 30908 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(ans!=-1)return ans;
    if(nodo==A && color!=cA)return ans=1e9;
    if(nodo==B && color!=cB)return ans=1e9;
    if(nodo==B && cA && cP)return ans=1e9;
    ans=1e9;
    bool ok=true;
    if(nodo==A && cB)ok=false;
    if(nodo==B && cA)ok=false;
    if(cP)ok=false;
 
    //todos tienen que ser cero
    int sum=color;
    for(auto v:G[nodo]){
        if(v==p)continue;
        sum+=f(v,0,color,cA,cB,nodo);
    }
    if(ok==false){
        ans=sum;
    }else{
        //solo tengo que pintar uno
        for(auto v:G[nodo]){
            if(v==p)continue;
            int curr=sum-f(v,0,color,cA,cB,nodo)+f(v,1,color,cA,cB,nodo);
            ans=min(ans,curr);
        }
    }
    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=1e9;
    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++){
            int curr=f(A,i,0,i,j);
            res=min(res,curr);
        }
    }
    // cout<<A<<" "<<B<<endl;
    if(res>=1e9){
        cout <<-1<<endl;
    }else{
        cout<<res<<endl;
    }
    return 0;
}  
//Guess i wont be comming to church on sunday
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12756 KB Output is correct
2 Correct 6 ms 12760 KB Output is correct
3 Correct 5 ms 12756 KB Output is correct
4 Correct 6 ms 12756 KB Output is correct
5 Incorrect 191 ms 30908 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12756 KB Output is correct
2 Correct 6 ms 12756 KB Output is correct
3 Incorrect 5 ms 12756 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12756 KB Output is correct
2 Correct 6 ms 12756 KB Output is correct
3 Incorrect 5 ms 12756 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12756 KB Output is correct
2 Correct 6 ms 12760 KB Output is correct
3 Correct 5 ms 12756 KB Output is correct
4 Correct 6 ms 12756 KB Output is correct
5 Incorrect 191 ms 30908 KB Output isn't correct
6 Halted 0 ms 0 KB -