Submission #556538

# Submission time Handle Problem Language Result Execution time Memory
556538 2022-05-03T10:13:49 Z new_acc Logičari (COCI21_logicari) C++14
110 / 110
96 ms 45172 KB
#include<bits/stdc++.h>
#define fi first
#define se second
#define pitem item*
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=1e6+10;
const int SS=1<<19;
const int INFi=2e9;
const ll INFl=1e13;
const ll mod2=998244353;
const ll mod=1e9+7;
const ll mod3=1000696969;
const ll p=70032301;
const ull p2=913;
const int L=20;
struct st{
    ll m[2][2];
};
int t[N],n,x;
st dp[N],dp2[N];
vi graf[N],c,__curr;
bitset<N>vis;
void dfs(int v,int o){
    if(vis[v]){
        if(c.size()) return;
        while(__curr[__curr.size()-1]!=v) c.push_back(__curr[__curr.size()-1]),__curr.pop_back();
        x=v;
        return;
    }
    vis[v]=1;
    __curr.push_back(v);
    for(auto u:graf[v]){
        if(u==o) continue;
        dfs(u,v);
    }
    if(c.size()) return;
    __curr.pop_back();
}
void dfst(int v,int o){
    ll naj1=INFi,naj2=INFi,il=0;
    for(auto u:graf[v]){
        if(u==o or vis[u]) continue;
        il=1;
        dfst(u,v);
        dp[v].m[1][1]+=dp[u].m[0][1];
        dp[v].m[0][1]+=dp[u].m[0][0];
        dp[v].m[0][0]+=dp[u].m[0][0];
        dp[v].m[1][0]+=dp[u].m[0][1];
        naj1=min(naj1,dp[u].m[1][0]-dp[u].m[0][0]),naj2=min(naj2,dp[u].m[1][1]-dp[u].m[0][1]);
    }
    if(!il){
        dp[v].m[0][0]=INFi,dp[v].m[0][1]=0,dp[v].m[1][0]=INFi,dp[v].m[1][1]=1;
        return;
    }
    dp[v].m[0][0]+=naj1,dp[v].m[1][0]+=naj2;
    dp[v].m[1][0]++,dp[v].m[1][1]++;
}
ll cnt(vector<st>curr){
    dp2[0]=curr[0];
    for(int i=1;i<curr.size();i++){
        dp2[i].m[0][0]=min(dp2[i-1].m[0][0]+curr[i].m[0][0],dp2[i-1].m[1][0]+curr[i].m[0][1]);
        dp2[i].m[0][1]=curr[i].m[0][1]+dp2[i-1].m[0][0];
        dp2[i].m[1][0]=min(dp2[i-1].m[1][1]+curr[i].m[1][1],dp2[i-1].m[0][1]+curr[i].m[1][0]);
        dp2[i].m[1][1]=curr[i].m[1][1]+dp2[i-1].m[0][1];
    }
    return min(dp2[curr.size()-1].m[0][0],dp2[curr.size()-1].m[1][0]);
}
void solve(){
    cin>>n;
    for(int a,b,i=1;i<=n;i++){
        cin>>a>>b;
        graf[a].push_back(b),graf[b].push_back(a);
    }
    dfs(1,1);
    vis.reset();
    for(int i=0;i<c.size();i++){
        int pop=(i==0?x:c[i-1]),nas=(i==c.size()-1?x:c[i+1]);
        vis[pop]=1,vis[nas]=1;
        dfst(c[i],c[i]); 
        vis[pop]=0,vis[nas]=0;
    }
    vis[c[0]]=1,vis[c[c.size()-1]]=1;
    dfst(x,x);
    vis[c[0]]=0,vis[c[c.size()-1]]=0;
    vector<st>curr;
    for(auto u:c) curr.push_back(dp[u]);
    st *f=&curr[0];
    st *l=&curr[curr.size()-1];
    ll res=INFi;
    //x=0 0
    f->m[1][0]=INFi,f->m[1][1]=INFi,l->m[1][0]=INFi,l->m[1][1]=INFi;
    res=min(res,cnt(curr)+dp[x].m[0][0]); 
    for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
    //x=1 0
    f->m[0][0]=f->m[0][1],f->m[1][0]=INFi,f->m[0][1]=INFi,f->m[1][1]=INFi; 
    l->m[0][0]=l->m[0][1],l->m[1][0]=INFi,l->m[0][1]=INFi,l->m[1][1]=INFi; 
    res=min(res,cnt(curr)+dp[x].m[1][0]);
    for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
    //x=0 1
    //p1 f=1
    f->m[0][1]=INFi,f->m[0][0]=INFi;
    l->m[1][0]=INFi,l->m[1][1]=INFi;      
    res=min(res,cnt(curr)+dp[x].m[0][1]); 
    for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
    //p2 l=1
    f->m[1][0]=INFi,f->m[1][1]=INFi;
    l->m[0][1]=INFi,l->m[0][0]=INFi;
    res=min(res,cnt(curr)+dp[x].m[0][1]); 
    for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
    //x=1 1
    //p1 f=1
    f->m[0][1]=INFi,f->m[0][0]=INFi,f->m[1][0]=f->m[1][1],f->m[1][1]=INFi;
    l->m[1][0]=INFi,l->m[1][1]=INFi,l->m[0][0]=l->m[0][1],l->m[0][1]=INFi;
    res=min(res,cnt(curr)+dp[x].m[1][1]);
    for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
    //p2 l=1
    l->m[0][1]=INFi,l->m[0][0]=INFi,l->m[1][0]=l->m[1][1],l->m[1][1]=INFi;
    f->m[1][0]=INFi,f->m[1][1]=INFi,f->m[0][0]=f->m[0][1],f->m[0][1]=INFi;
    res=min(res,cnt(curr)+dp[x].m[1][1]);
    cout<<(res==INFi?-1:res)<<"\n";
}
int main(){
    ios_base::sync_with_stdio(0),cin.tie(0);
    int tt=1;
    while(tt--) solve();
}

Compilation message

Main.cpp: In function 'll cnt(std::vector<st>)':
Main.cpp:64:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<st>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |     for(int i=1;i<curr.size();i++){
      |                 ~^~~~~~~~~~~~
Main.cpp: In function 'void solve()':
Main.cpp:80:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |     for(int i=0;i<c.size();i++){
      |                 ~^~~~~~~~~
Main.cpp:81:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |         int pop=(i==0?x:c[i-1]),nas=(i==c.size()-1?x:c[i+1]);
      |                                      ~^~~~~~~~~~~~
Main.cpp:97:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<st>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |     for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
      |                 ~^~~~~~~~~~~~
Main.cpp:102:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<st>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |     for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
      |                 ~^~~~~~~~~~~~
Main.cpp:108:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<st>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |     for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
      |                 ~^~~~~~~~~~~~
Main.cpp:113:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<st>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |     for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
      |                 ~^~~~~~~~~~~~
Main.cpp:119:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<st>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |     for(int i=0;i<curr.size();i++) curr[i]=dp[c[i]];
      |                 ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23892 KB Output is correct
2 Correct 13 ms 23832 KB Output is correct
3 Correct 13 ms 23828 KB Output is correct
4 Correct 12 ms 23892 KB Output is correct
5 Correct 81 ms 45116 KB Output is correct
6 Correct 76 ms 45160 KB Output is correct
7 Correct 77 ms 45108 KB Output is correct
8 Correct 96 ms 45172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23892 KB Output is correct
2 Correct 12 ms 23952 KB Output is correct
3 Correct 12 ms 23892 KB Output is correct
4 Correct 13 ms 23888 KB Output is correct
5 Correct 13 ms 23892 KB Output is correct
6 Correct 12 ms 23948 KB Output is correct
7 Correct 12 ms 23892 KB Output is correct
8 Correct 14 ms 23948 KB Output is correct
9 Correct 13 ms 23892 KB Output is correct
10 Correct 13 ms 23920 KB Output is correct
11 Correct 16 ms 23836 KB Output is correct
12 Correct 16 ms 23924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23892 KB Output is correct
2 Correct 12 ms 23952 KB Output is correct
3 Correct 12 ms 23892 KB Output is correct
4 Correct 13 ms 23888 KB Output is correct
5 Correct 13 ms 23892 KB Output is correct
6 Correct 12 ms 23948 KB Output is correct
7 Correct 12 ms 23892 KB Output is correct
8 Correct 14 ms 23948 KB Output is correct
9 Correct 13 ms 23892 KB Output is correct
10 Correct 13 ms 23920 KB Output is correct
11 Correct 16 ms 23836 KB Output is correct
12 Correct 16 ms 23924 KB Output is correct
13 Correct 12 ms 23940 KB Output is correct
14 Correct 14 ms 24020 KB Output is correct
15 Correct 13 ms 23948 KB Output is correct
16 Correct 14 ms 23952 KB Output is correct
17 Correct 13 ms 23948 KB Output is correct
18 Correct 13 ms 24020 KB Output is correct
19 Correct 14 ms 23892 KB Output is correct
20 Correct 14 ms 23952 KB Output is correct
21 Correct 13 ms 23948 KB Output is correct
22 Correct 13 ms 24012 KB Output is correct
23 Correct 13 ms 24060 KB Output is correct
24 Correct 14 ms 23952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23892 KB Output is correct
2 Correct 13 ms 23832 KB Output is correct
3 Correct 13 ms 23828 KB Output is correct
4 Correct 12 ms 23892 KB Output is correct
5 Correct 81 ms 45116 KB Output is correct
6 Correct 76 ms 45160 KB Output is correct
7 Correct 77 ms 45108 KB Output is correct
8 Correct 96 ms 45172 KB Output is correct
9 Correct 13 ms 23892 KB Output is correct
10 Correct 12 ms 23952 KB Output is correct
11 Correct 12 ms 23892 KB Output is correct
12 Correct 13 ms 23888 KB Output is correct
13 Correct 13 ms 23892 KB Output is correct
14 Correct 12 ms 23948 KB Output is correct
15 Correct 12 ms 23892 KB Output is correct
16 Correct 14 ms 23948 KB Output is correct
17 Correct 13 ms 23892 KB Output is correct
18 Correct 13 ms 23920 KB Output is correct
19 Correct 16 ms 23836 KB Output is correct
20 Correct 16 ms 23924 KB Output is correct
21 Correct 12 ms 23940 KB Output is correct
22 Correct 14 ms 24020 KB Output is correct
23 Correct 13 ms 23948 KB Output is correct
24 Correct 14 ms 23952 KB Output is correct
25 Correct 13 ms 23948 KB Output is correct
26 Correct 13 ms 24020 KB Output is correct
27 Correct 14 ms 23892 KB Output is correct
28 Correct 14 ms 23952 KB Output is correct
29 Correct 13 ms 23948 KB Output is correct
30 Correct 13 ms 24012 KB Output is correct
31 Correct 13 ms 24060 KB Output is correct
32 Correct 14 ms 23952 KB Output is correct
33 Correct 62 ms 30664 KB Output is correct
34 Correct 56 ms 31036 KB Output is correct
35 Correct 54 ms 30820 KB Output is correct
36 Correct 52 ms 30956 KB Output is correct
37 Correct 25 ms 25600 KB Output is correct
38 Correct 60 ms 30836 KB Output is correct
39 Correct 17 ms 24532 KB Output is correct
40 Correct 57 ms 31004 KB Output is correct
41 Correct 42 ms 32072 KB Output is correct
42 Correct 44 ms 31804 KB Output is correct
43 Correct 70 ms 41028 KB Output is correct
44 Correct 83 ms 36632 KB Output is correct