LOADING

加载过慢请开启缓存,浏览器默认开启

daily 2

2023/7/9 daily

CF 963B

int main(){
    int n;
    cin>>n;
    vector<int>a(n+1),deg(n+1);
    vector<vector<int>>E(n+1);
    for(int i=1;i<=n;++i){
       int v;
       cin>>v;
       if(v){
        E[i].push_back(v);
        E[v].push_back(i);
        ++deg[i];
        ++deg[v];
       }
    }
    vector<int>fa(n+1);
    vector<int>st;
    function<void(int,int)>dfs1=[&](int x,int f){
        st.push_back(x);
        fa[x]=f;
        for(int v:E[x]){
            if(v==f)continue;
            dfs1(v,x);
        }
    };
    vector<int>ans;
    vector<int>vis(n+1);
    function<void(int)>dfs2=[&](int x){
        ans.push_back(x);
        vis[x]=1;
        for(int v:E[x]){
            deg[v]--;
            if(v==fa[x]||vis[v])continue;
            if(deg[v]%2==0)dfs2(v);
        }
    };
    dfs1(1,0);
    while(!st.empty()){
        auto cur=st.back();
        st.pop_back();
        if(deg[cur]%2==0){
            dfs2(cur);
        }
    }
    if(ans.size()==n){
        cout<<"YES\n";
        for(int i=0;i<n;++i){
            cout<<ans[i]<<"\n";
        }
    }else{
        cout<<"NO\n";
    }
    return 0;
}

CF 1119E

#define int long long
signed main(){
    int n;
    cin>>n;
    vector<int>a(n+2);
    for(int i=1;i<=n;++i)cin>>a[i];
    int cur=1;
    int ans=0;
    for(int i=1;i<=n;++i){
        ans+=(a[i]/3);
        a[i]%=3;
        if(cur<=i)cur=i+1;
        while(cur<=n&&a[cur]<2)++cur;
        if(a[i]>=1&&a[cur]>=2){
            ans++;
            a[cur]-=2;
            a[i]--;
        }
        while(cur<=n&&a[cur]<2)++cur;
        if(a[i]>=1&&a[cur]>=2){
            ans++;
            a[cur]-=2;
            a[i]--;
        }
    }
    cout<<ans<<'\n';
}