답안 #887937

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
887937 2023-12-15T14:25:48 Z Mr_Ph Logičari (COCI21_logicari) C++17
110 / 110
152 ms 24148 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;
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)continue;
                e+=ans(i,0,color,1,node);
                xd+=ans(i,0,color,0,node);
            }
            for(auto i:adj[node])
            {
                if(i==v)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=1e9;
                for(auto i:adj[node])
                {
                    if(i==parent)continue;
                    xd+=ans(i,0,color,vc,node);
                }
                for(auto i:adj[node])
                {
                    if(i==parent)continue;
                    if(i==v)continue;
                    e=min(e,xd-ans(i,0,color,vc,node)+ans(i,0,color,vc,node));
                }
            }
            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=1;i<=n;i++)
    {
        int a,b;
        cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    cycle(1,0);
   // cout<<u<<" "<<v<<endl;
    lol=1;
    int x=ans(u,1,0,0,0)+1;
    memset(dp,-1,sizeof dp);
    lol=0;
    x=min(x,ans(u,0,0,0,0));
    //cout<<x<<endl;
    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:157:13: warning: unused variable 'st' [-Wunused-variable]
  157 |     int t=1,st;
      |             ^~
# 결과 실행 시간 메모리 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 105 ms 23176 KB Output is correct
6 Correct 101 ms 23124 KB Output is correct
7 Correct 103 ms 24148 KB Output is correct
8 Correct 103 ms 24144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 2 ms 6488 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 2 ms 6700 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 2 ms 6492 KB Output is correct
9 Correct 2 ms 6492 KB Output is correct
10 Correct 2 ms 6492 KB Output is correct
11 Correct 1 ms 6572 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 2 ms 6488 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 2 ms 6700 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 2 ms 6492 KB Output is correct
9 Correct 2 ms 6492 KB Output is correct
10 Correct 2 ms 6492 KB Output is correct
11 Correct 1 ms 6572 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 2 ms 6748 KB Output is correct
14 Correct 3 ms 6900 KB Output is correct
15 Correct 3 ms 6728 KB Output is correct
16 Correct 3 ms 6748 KB Output is correct
17 Correct 2 ms 6492 KB Output is correct
18 Correct 2 ms 6748 KB Output is correct
19 Correct 2 ms 6492 KB Output is correct
20 Correct 2 ms 6748 KB Output is correct
21 Correct 2 ms 6788 KB Output is correct
22 Correct 2 ms 6748 KB Output is correct
23 Correct 2 ms 6748 KB Output is correct
24 Correct 2 ms 6748 KB Output is correct
# 결과 실행 시간 메모리 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 105 ms 23176 KB Output is correct
6 Correct 101 ms 23124 KB Output is correct
7 Correct 103 ms 24148 KB Output is correct
8 Correct 103 ms 24144 KB Output is correct
9 Correct 2 ms 6488 KB Output is correct
10 Correct 2 ms 6488 KB Output is correct
11 Correct 2 ms 6492 KB Output is correct
12 Correct 2 ms 6700 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 2 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 2 ms 6492 KB Output is correct
17 Correct 2 ms 6492 KB Output is correct
18 Correct 2 ms 6492 KB Output is correct
19 Correct 1 ms 6572 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 2 ms 6748 KB Output is correct
22 Correct 3 ms 6900 KB Output is correct
23 Correct 3 ms 6728 KB Output is correct
24 Correct 3 ms 6748 KB Output is correct
25 Correct 2 ms 6492 KB Output is correct
26 Correct 2 ms 6748 KB Output is correct
27 Correct 2 ms 6492 KB Output is correct
28 Correct 2 ms 6748 KB Output is correct
29 Correct 2 ms 6788 KB Output is correct
30 Correct 2 ms 6748 KB Output is correct
31 Correct 2 ms 6748 KB Output is correct
32 Correct 2 ms 6748 KB Output is correct
33 Correct 75 ms 13136 KB Output is correct
34 Correct 98 ms 13148 KB Output is correct
35 Correct 85 ms 13064 KB Output is correct
36 Correct 90 ms 13184 KB Output is correct
37 Correct 20 ms 8112 KB Output is correct
38 Correct 113 ms 13140 KB Output is correct
39 Correct 8 ms 7096 KB Output is correct
40 Correct 100 ms 13100 KB Output is correct
41 Correct 52 ms 14028 KB Output is correct
42 Correct 70 ms 14080 KB Output is correct
43 Correct 152 ms 24140 KB Output is correct
44 Correct 93 ms 19040 KB Output is correct