Submission #862255

# Submission time Handle Problem Language Result Execution time Memory
862255 2023-10-17T21:04:47 Z AbdelmagedNour Logičari (COCI21_logicari) C++17
110 / 110
150 ms 27384 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
const int N=100005,INF=1e9;
struct DSU{
    int p[N],sz[N];
    void init(int n){
        iota(p,p+n+1,0);
        fill(sz,sz+n+1,1);
    }
    int Find(int u){
        return (p[u]==u?u:p[u]=Find(p[u]));
    }
    bool operator()(int u,int v){
        u=Find(u);v=Find(v);
        if(u==v)return 0;
        if(sz[u]>sz[v])swap(u,v);
        sz[v]+=sz[u];
        p[u]=v;
        return 1;
    }
}dsu;
int n,root,special;
int dp[N][2][2][2][2];//cur,root,par,special
int p[N];
vector<vector<int>>adj(N);
void dfs(int v,int par){
    p[v]=par;
    for(auto u:adj[v]) {
        if(u==par)continue;
        dfs(u,v);
    }
}
int solve(int v,int cur,int par,int rot,int sp){
    int&ret=dp[v][cur][par][rot][sp];
	if(~ret)return ret;
    ret=INF;
    bool flag=1;
    if(v==root&&cur!=rot)flag=0;
    if(v==special&&cur!=sp)flag=0;
    if(v==special&&par&&rot)flag=0;
    if(!flag)return ret;

    bool covered=0;
    if(par)covered=1;
    if(v==root&&sp)covered=1;
    if(v==special&&rot)covered=1;
    ll sum=cur;
    for(auto u:adj[v]){
        if(u==p[v])continue;
        sum+=solve(u,0,cur,rot,sp);
    }
    if(covered){
        ret=min((ll)ret,sum);
    }else{
        for(auto u:adj[v]){
            if(u==p[v])continue;
            ll val=sum-solve(u,0,cur,rot,sp)+solve(u,1,cur,rot,sp);
            ret=min((ll)ret,val);
        }
    }
    return ret;
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	memset(dp,-1,sizeof(dp));
    cin>>n;
    dsu.init(n);
    for(int i=0;i<n;i++){
        int u,v;
        cin>>u>>v;		
        if(!dsu(u,v)){
            root=u;
            special=v;
        }else{
            adj[u].push_back(v);
            adj[v].push_back(u);
        }
    }
	dfs(root,0);
	int res=INF;
	for(auto rot:{0,1})for(auto sp:{0,1}){
		//cout<<rot<<" "<<sp<<" : "<<solve(root,rot,0,rot,sp)<<"\n";
		res=min(res,solve(root,rot,0,rot,sp));
	}
	cout<<(res==INF?-1:res)<<"\n";
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9052 KB Output is correct
2 Correct 1 ms 9052 KB Output is correct
3 Correct 2 ms 9052 KB Output is correct
4 Correct 2 ms 9052 KB Output is correct
5 Correct 117 ms 27268 KB Output is correct
6 Correct 126 ms 27192 KB Output is correct
7 Correct 120 ms 27384 KB Output is correct
8 Correct 150 ms 27216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9308 KB Output is correct
2 Correct 2 ms 9308 KB Output is correct
3 Correct 2 ms 9228 KB Output is correct
4 Correct 3 ms 9308 KB Output is correct
5 Correct 2 ms 9048 KB Output is correct
6 Correct 2 ms 9052 KB Output is correct
7 Correct 2 ms 9052 KB Output is correct
8 Correct 2 ms 9052 KB Output is correct
9 Correct 2 ms 9048 KB Output is correct
10 Correct 2 ms 9052 KB Output is correct
11 Correct 2 ms 9052 KB Output is correct
12 Correct 2 ms 9308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9308 KB Output is correct
2 Correct 2 ms 9308 KB Output is correct
3 Correct 2 ms 9228 KB Output is correct
4 Correct 3 ms 9308 KB Output is correct
5 Correct 2 ms 9048 KB Output is correct
6 Correct 2 ms 9052 KB Output is correct
7 Correct 2 ms 9052 KB Output is correct
8 Correct 2 ms 9052 KB Output is correct
9 Correct 2 ms 9048 KB Output is correct
10 Correct 2 ms 9052 KB Output is correct
11 Correct 2 ms 9052 KB Output is correct
12 Correct 2 ms 9308 KB Output is correct
13 Correct 3 ms 9304 KB Output is correct
14 Correct 3 ms 9308 KB Output is correct
15 Correct 3 ms 9308 KB Output is correct
16 Correct 3 ms 9344 KB Output is correct
17 Correct 2 ms 9304 KB Output is correct
18 Correct 3 ms 9308 KB Output is correct
19 Correct 2 ms 9308 KB Output is correct
20 Correct 3 ms 9308 KB Output is correct
21 Correct 2 ms 9560 KB Output is correct
22 Correct 2 ms 9308 KB Output is correct
23 Correct 2 ms 9308 KB Output is correct
24 Correct 3 ms 9308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9052 KB Output is correct
2 Correct 1 ms 9052 KB Output is correct
3 Correct 2 ms 9052 KB Output is correct
4 Correct 2 ms 9052 KB Output is correct
5 Correct 117 ms 27268 KB Output is correct
6 Correct 126 ms 27192 KB Output is correct
7 Correct 120 ms 27384 KB Output is correct
8 Correct 150 ms 27216 KB Output is correct
9 Correct 2 ms 9308 KB Output is correct
10 Correct 2 ms 9308 KB Output is correct
11 Correct 2 ms 9228 KB Output is correct
12 Correct 3 ms 9308 KB Output is correct
13 Correct 2 ms 9048 KB Output is correct
14 Correct 2 ms 9052 KB Output is correct
15 Correct 2 ms 9052 KB Output is correct
16 Correct 2 ms 9052 KB Output is correct
17 Correct 2 ms 9048 KB Output is correct
18 Correct 2 ms 9052 KB Output is correct
19 Correct 2 ms 9052 KB Output is correct
20 Correct 2 ms 9308 KB Output is correct
21 Correct 3 ms 9304 KB Output is correct
22 Correct 3 ms 9308 KB Output is correct
23 Correct 3 ms 9308 KB Output is correct
24 Correct 3 ms 9344 KB Output is correct
25 Correct 2 ms 9304 KB Output is correct
26 Correct 3 ms 9308 KB Output is correct
27 Correct 2 ms 9308 KB Output is correct
28 Correct 3 ms 9308 KB Output is correct
29 Correct 2 ms 9560 KB Output is correct
30 Correct 2 ms 9308 KB Output is correct
31 Correct 2 ms 9308 KB Output is correct
32 Correct 3 ms 9308 KB Output is correct
33 Correct 63 ms 12892 KB Output is correct
34 Correct 79 ms 12832 KB Output is correct
35 Correct 79 ms 12888 KB Output is correct
36 Correct 72 ms 12892 KB Output is correct
37 Correct 17 ms 10076 KB Output is correct
38 Correct 74 ms 12892 KB Output is correct
39 Correct 7 ms 9564 KB Output is correct
40 Correct 64 ms 12840 KB Output is correct
41 Correct 39 ms 13608 KB Output is correct
42 Correct 50 ms 13772 KB Output is correct
43 Correct 114 ms 27216 KB Output is correct
44 Correct 74 ms 20424 KB Output is correct