Submission #673384

# Submission time Handle Problem Language Result Execution time Memory
673384 2022-12-20T13:11:48 Z Half Logičari (COCI21_logicari) C++17
110 / 110
163 ms 19484 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define REP(i,a,b) for(ll i=(ll) a; i<(ll) b; i++)
#define pb push_back
#define mp make_pair
#define pl pair<ll,ll>
#define ff first
#define ss second
#define whole(x) x.begin(),x.end()
#define DEBUG(i) cout<<"WAFFLES "<<i<<"<\n"
#define INF 10000000LL
#define EPS (0.00000000001L)
#define pi (3.141592653589793L)
#define VV(vvvv,NNNN,xxxx); REP(iiiii,0,NNNN) {vvvv.pb(xxxx);}
ll mod=1000000007LL;

template<class A=ll>
void Out(vector<A> a) {REP(i,0,a.size()) {cout<<a[i]<<" ";} cout<<endl;}

template<class A=ll>
void In(vector<A> &a, ll N) {A cur; REP(i,0,N) {cin>>cur; a.pb(cur);}} 

vector<vector<ll>> adj;
ll n;
vector<bool> vis;
pair<ll, ll> cyc;

pair<ll,ll> dfs(ll prv, ll nd){
	vis[nd] = true;
	pair<ll, ll> sol = {-1, -1};
	for(ll nb : adj[nd]){
		if(nb == prv)
			continue;
		if(vis[nb]){
			return {nd,nb};
		}
		sol = max(sol, dfs(nd, nb));
		if(sol.ff > -1)
			break;
	}
	return sol;
}

bool root_seeing, root_blue, cyc_blue;

vector<array<array<ll,2>,2>> mem;

ll tree_dfs(ll pr, ll nd, bool seeing, bool blue){
	if(mem[nd][seeing][blue] != -1)
		return mem[nd][seeing][blue];
	if(nd == cyc.ss){
		if(blue != root_seeing){
			mem[nd][seeing][blue] = INF;
			return INF;
		}
		if(seeing && root_blue){
			mem[nd][seeing][blue] = INF;
			return INF;
		}
		if(adj[nd].size() == 2){
			if(seeing || root_blue){
				mem[nd][seeing][blue] = blue;
				return blue;
			}else{
				mem[nd][seeing][blue] = INF;
				return INF;
			}
		}
		if(root_blue)
			seeing = true;
	}
	if(adj[nd].size() == 1){
		if(seeing){
			mem[nd][seeing][blue] = blue;
			return blue;
		}else{
			mem[nd][seeing][blue] = INF;
			return INF;
		}
	}
	if(seeing){
		if(blue){
			ll sum = 0;
			for(ll ch : adj[nd]){
				if(ch == pr)
					continue;
				if(ch == cyc.ff)
					continue;
				sum += tree_dfs(nd, ch, true, false);
			}
			mem[nd][seeing][blue] = sum+1;
			return sum + 1;
		}else{
			ll sum = 0;
			for(ll ch : adj[nd]){
				if(ch == pr)
					continue;
				if(ch == cyc.ff)
					continue;
				sum += tree_dfs(nd, ch, false, false);
			}
			mem[nd][seeing][blue] = sum;
			return sum;
		}
	}else{
		if(blue){
			ll sum = 0;
			ll mndf = INF;
			for(ll ch : adj[nd]){
				if(ch == pr)
					continue;
				if(ch == cyc.ff)
					continue;
				ll nb_ntb = tree_dfs(nd, ch, true, false);
				ll nb_b = tree_dfs(nd, ch, true, true);
				sum += nb_ntb;
				mndf = min(mndf, nb_b-nb_ntb);
			}
			mem[nd][seeing][blue] = sum + mndf + 1;
			return sum + mndf + 1;
		}else{
			ll sum = 0;
			ll mndf = INF;
			for(ll ch : adj[nd]){
				if(ch == pr)
					continue;
				if(ch == cyc.ff)
					continue;
				ll nb_ntb = tree_dfs(nd, ch, false, false);
				ll nb_b = tree_dfs(nd, ch, false, true);
				sum += nb_ntb;
				mndf = min(mndf, nb_b-nb_ntb);
			}
			mem[nd][seeing][blue] = sum + mndf;
			return sum + mndf;
		}
	}
}

int main(){
	ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout.precision(20);

	cin >> n;
	adj.assign(n,{});
	ll a, b;
	for(ll i = 0; i < n; ++i){
		cin >> a >> b;
		a--;b--;
		adj[a].pb(b);
		adj[b].pb(a);
	}
	vis.assign(n, 0);
	cyc = dfs(-1, 0);
	mem.assign(n, {array<ll,2>{-1,-1},array<ll,2>{-1,-1}});
	ll mn = INF;
	root_seeing = false;
	root_blue = false;
	mn = min(mn, tree_dfs(cyc.ss, cyc.ff, false, false));
	mem.assign(n, {array<ll,2>{-1,-1},array<ll,2>{-1,-1}});
	root_seeing = false;
	root_blue = true;
	mn = min(mn, tree_dfs(cyc.ss, cyc.ff, false, true));

	mem.assign(n, {array<ll,2>{-1,-1},array<ll,2>{-1,-1}});
	root_seeing = true;
	root_blue = false;
	mn = min(mn, tree_dfs(cyc.ss, cyc.ff, true, false));
	mem.assign(n, {array<ll,2>{-1,-1},array<ll,2>{-1,-1}});
	root_seeing = true;
	root_blue = true;
	mn = min(mn, tree_dfs(cyc.ss, cyc.ff, true, true));
	if(mn >= INF){
		cout << "-1\n";
	}else{
		cout << mn << "\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 160 ms 18344 KB Output is correct
6 Correct 160 ms 19484 KB Output is correct
7 Correct 141 ms 19480 KB Output is correct
8 Correct 163 ms 19484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 272 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 300 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 272 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 300 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 324 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 160 ms 18344 KB Output is correct
6 Correct 160 ms 19484 KB Output is correct
7 Correct 141 ms 19480 KB Output is correct
8 Correct 163 ms 19484 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 272 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 81 ms 9524 KB Output is correct
34 Correct 63 ms 9508 KB Output is correct
35 Correct 68 ms 9532 KB Output is correct
36 Correct 64 ms 9468 KB Output is correct
37 Correct 14 ms 2516 KB Output is correct
38 Correct 85 ms 9548 KB Output is correct
39 Correct 5 ms 1096 KB Output is correct
40 Correct 69 ms 9380 KB Output is correct
41 Correct 43 ms 10776 KB Output is correct
42 Correct 53 ms 10784 KB Output is correct
43 Correct 136 ms 19464 KB Output is correct
44 Correct 76 ms 15148 KB Output is correct