Submission #887913

# Submission time Handle Problem Language Result Execution time Memory
887913 2023-12-15T13:18:41 Z Mr_Ph Logičari (COCI21_logicari) C++17
0 / 110
82 ms 24268 KB
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<x, null_type,
ll mod=(ll)1e9+7;
ll mod1=998244353;
///the defines :)
#define endl '\n'
#define vi vector<int>
#define ent(arr) for(int i=0;i<arr.size();i++)cin>>arr[i];
#define all(arr) arr.begin(),arr.end()
#define allr(arr) arr.rbegin(),arr.rend()
#define sz size()
#define int long long
int dp[100002][2][2][2];
bool vs[100002];
vector<vi>adj;
int u,v;
void cycle(int node,int parent)
{
    vs[node]=true;
    for(auto i:adj[node])
    {
        if(i==parent)continue;
        if(vs[i])
        {
            u=node,v=i;
            return;
        }
        cycle(i,node);
    }
}
int lol=0;
map<int,int>mp;
int ans(int node,int color,int cansee,int vc,int parent)
{
    if(node==v)color=vc;
    if(node==v&&cansee&&lol)return 1e9;
    if(node==v)cansee|=lol;
    //cout<<node<<" "<<color<<" "<<cansee<<" "<<vc<<" "<<parent<<endl;
    if(dp[node][color][cansee][vc]!=-1)return dp[node][color][cansee][vc];
    if(cansee)
    {
        bool hi=false;
        for(auto i:adj[node])
            if(i==v&&i!=parent)hi=true;
        if(vc&&hi)return dp[node][color][cansee][vc]=1e9;
        else
        {
            int e=0;
            for(auto i:adj[node])
            {
                if(i==parent||i==u)continue;
                e+=ans(i,0,color,vc,node);
            }
            return dp[node][color][cansee][vc]=e;
        }
    }
    else
    {
            int e=1e9,xd=0;
        if(node==u)
        {
            e=1;
            for(auto i:adj[node])
            {
                if(i==v||i==u)continue;
                e+=ans(i,0,color,1,node);
                xd+=ans(i,0,color,0,node);
            }
            for(auto i:adj[node])
            {
                if(i==v||i==u)continue;
                e=min(e,xd-ans(i,0,color,0,node)+ans(i,1,color,0,node)+1);
            }
        }
        else
        {
            bool hi=false;
            for(auto i:adj[node])if(i==v&&parent!=i)hi=true;
            if(hi&&vc){
                e=0;
                for(auto i:adj[node])if(i!=parent)e+=ans(i,0,color,vc,node);
            }
            else if(hi&&!vc)
            {
                e=0;
                int cnt=0;
                for(auto i:adj[node])
                {
                    if(i==parent)continue;
                    e+=ans(i,0,color,vc,node);
                    cnt++;
                }
                if(cnt==1)return dp[node][color][cansee][vc]=1e9;
            }
            else
            {
                int cnt=0;
                for(auto i:adj[node])
                {
                    if(i==parent||i==u)continue;
                    xd+=ans(i,0,color,vc,node);
                    cnt++;
                }
                for(auto i:adj[node])
                {
                    if(i==parent||i==u)continue;
                    if(i==v)continue;
                    e=min(e,xd-ans(i,0,color,vc,node)+ans(i,1,color,vc,node)+1);
                }
                if(cnt==0)e=1e9;
            }
        }
        return dp[node][color][cansee][vc]=e;
    }

}
void preprocess() {}
void solve()
{
    memset(dp,-1,sizeof dp);
    int n;
    cin>>n;
    adj.resize(n+1);
    for(int i=0;i<n;i++)
    {
        int a,b;
        cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    cycle(1,0);
    lol=1;
    int x=ans(u,1,0,0,0)+1;
    //cout<<x<<endl;
    lol=0;
    x=min(x,ans(u,0,0,0,0));
    if(x>=1e9)cout<<-1<<endl;
    else cout<<x<<endl;
}
signed main()
{
    // freopen("meta_game_input.txt","r",stdin);
    // freopen("otput.txt","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    preprocess();
    int t=1,st;
    //cin>>t;
    while(t--)
        solve();
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:153:13: warning: unused variable 'st' [-Wunused-variable]
  153 |     int t=1,st;
      |             ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 70 ms 24268 KB Output is correct
6 Incorrect 82 ms 24144 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Incorrect 1 ms 6488 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Incorrect 1 ms 6488 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 70 ms 24268 KB Output is correct
6 Incorrect 82 ms 24144 KB Output isn't correct
7 Halted 0 ms 0 KB -