답안 #862176

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
862176 2023-10-17T15:57:37 Z Mizo_Compiler Logičari (COCI21_logicari) C++14
110 / 110
91 ms 30160 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double ld;
#define pb push_back
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define F first
#define S second
const int N = 1e5+5;
int n, dp[N][2][2], dp2[N][2][2][2][2], tr[N], dp3[N][2][2];
vector<int> adj[N], cyc;
bool inc[N], ins[N], vis[N];
 
void get_cyc(int u, int p) {
	if (!cyc.empty())return;
	if (ins[u]) {
		cyc.pb(u);
		int cur = tr[u];
		inc[u] = true;
		while (cur != u) {
			cyc.pb(cur);
			inc[cur] = true;
			cur = tr[cur];
		}
		return;
	}
	if (vis[u])return;
	vis[u] = ins[u] = true;
	for (auto &v : adj[u]) {
		if (v == p)continue;
		tr[u] = v;
		get_cyc(v, u);
		if (!cyc.empty())return;
	}
	ins[u] = false;
}
 
int sol2(int u, int p, int val, int pv) {
	int &ret = dp[u][val][pv];
	if (~ret)return ret;
	ret = val;
	if (!pv) {
		int mn = -1e6;
		int f, s;
		bool fl = false, f2 = false, fx = false;
		for (auto &v : adj[u]) {
			if (v == p || inc[v])continue;
			fx = true;
			f = sol2(v, u, 0, val), s = sol2(v, u, 1, val);
			if (f == s && f == int(1e6))return ret = int(1e6);
			if (f == int(1e6)) {
				if (fl)return ret = int(1e6);
				ret += (mn == -1e6 ? 0 : mn);
				mn = 1e6;
				fl = true;
				ret += s;
				continue;
			}
			if (s == int(1e6)) {
				ret += f;
				continue;
			}
			if (f-s > mn) {
				ret += (mn == -1e6 ? 0 : mn);
				mn = f-s;
				ret += s;
				f2 = true;
			} else {
				ret += f;
			}
		}
		if (!fx || (!fl && !f2))return ret = int(1e6);
	} else {
		for (auto &v : adj[u]) {
			if (v == p || inc[v])continue;
			ret += sol2(v, u, 0, val);
			if (ret > int(1e6))return ret = int(1e6);
		}
	}
	ret = min(ret, int(1e6));
	return ret;
}
 
int get(int u, int x, int val) {
	int &ret = dp3[u][x][val];
	if (~ret)return ret;
	ret = 0;
	bool fx = false;
	if (!x) {
		int mn = -1e6;
		bool fl = false, f2 = false;
		for (auto &v : adj[u]) {
			if (inc[v])continue;
			fx = true;
			int f = sol2(v, u, 0, val), s = sol2(v, u, 1, val);
			if (f == s && f == int(1e6))return ret = int(1e6);
			if (f == int(1e6)) {
				if (fl)return ret = int(1e6);
				ret += (mn == -1e6 ? 0 : mn);
				mn = 1e6;
				fl = true;
				ret += s;
				continue;
			}
			if (s == int(1e6)) {
				ret += f;
				continue;
			}
			if (f-s > mn) {
				ret += (mn == -1e6 ? 0 : mn);
				mn = f-s;
				ret += s;
				f2 = true;
			} else {
				ret += f;
			}
		}
		if (!fx)return ret = int(1e6);
		if (!fl && !f2)return ret = int(1e6);
	} else {
		for (auto &v : adj[u]) {
			if (inc[v])continue;
			ret += sol2(v, u, 0, val);
			if (ret > int(1e6))return ret = int(1e6);
		}
	}
	return ret;
}
 
int sol(int i, int ls, int nls, int f, int nf) {
	int &ret = dp2[i][ls][nls][f][nf];
	if (~ret)return ret;
	if (!i) {
		int c1 = get(cyc[i], 0, 0) + sol(i+1, 0, 1, 0, 1);
		c1 = min({c1, int(1e6), get(cyc[i], 1, 0) + sol(i+1, 0, 0, 0, 0)});
		int c2 = get(cyc[i], 0, 1) + 1 + sol(i+1, 1, 1, 1, 1);
		c2 = min({c2, int(1e6), get(cyc[i], 1, 1) + 1 + sol(i+1, 1, 0, 1, 0)});
		return ret = min({int(1e6), c1, c2});
	} else if (i == sz(cyc) - 1) {
		if (f && ls)return ret = int(1e6);
		if (nf+nls == 1)return ret = int(1e6);
		if (nf + nls == 0) {
			if (f + ls == 0) {
				return ret = min(int(1e6), get(cyc[i], 0, 1) + 1);
			} else {
				return ret = min(int(1e6), 1 + get(cyc[i], 1, 1));
			}
		}
		if (f + ls == 0)return ret = get(cyc[i], 0, 0);
		return ret = get(cyc[i], 1, 0);
	} else if (i == 1) {
		if (ls) {
			if (nf)return ret = min(int(1e6), get(cyc[i], 1, 0) + sol(i+1, 0, 1, f, nf));
			int c1 = get(cyc[i], 1, 1) + 1 + sol(i+1, 1, 1, f, 1);
			int c2 = get(cyc[i], 1, 0) + sol(i+1, 0, 1, f, 0);
			return ret = min({int(1e6), c1, c2});
		}
		if (nf)return ret = min(int(1e6), get(cyc[i], 0, 0) + sol(i+1, 0, 1, f, nf));
		int c1 = get(cyc[i], 0, 1) + 1 + sol(i+1, 1, 1, f, 1);
		c1 = min(c1, get(cyc[i], 1, 1) + 1 + sol(i+1, 1, 0, f, 1));
		int c2 = get(cyc[i], 0, 0) + sol(i+1, 0, 1, f, 0);
		c2 = min(c2, get(cyc[i], 1, 0) + sol(i+1, 0, 0, f, 0));
		return ret = min({int(1e6), c1, c2});
	} else {
		if (!nls) {
			int c1 = get(cyc[i], ls, 1)+1+sol(i+1, 1, 1, f, nf);
			c1 = min(c1, get(cyc[i], 1, 1)+1+sol(i+1, 1, ls, f, nf));
			return ret = min(int(1e6), c1);
		}
		int c1 = get(cyc[i], ls, 0)+sol(i+1, 0, 1, f, nf);
		c1 = min(c1, get(cyc[i], 1, 0)+sol(i+1, 0, ls, f, nf));
		return ret = min(int(1e6), c1);
	}
	return ret;
}
 
int main () {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n;
	for (int i = 0; i < n; i++) {
		int u, v;
		cin >> u >> v;
		--u, --v;
		adj[u].pb(v);
		adj[v].pb(u);
		for (int j = 0; j < 2; j++) {
			for (int k = 0; k < 2; k++) {
				dp[i][j][k] = dp3[i][j][k] = -1;
				for (int l = 0; l < 2; l++) {
					for (int m = 0; m < 2; m++) {
						dp2[i][j][k][l][m] = -1;
					}
				}
			}
		}
	}
	get_cyc(0, 0);
	int x = sol(0, 0, 0, 0, 0);
	cout << (x > n ? -1 : x) << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6620 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 91 ms 30160 KB Output is correct
6 Correct 76 ms 30152 KB Output is correct
7 Correct 86 ms 30160 KB Output is correct
8 Correct 82 ms 29976 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 2 ms 6744 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 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 2 ms 6744 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6580 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 2 ms 6492 KB Output is correct
23 Correct 1 ms 6748 KB Output is correct
24 Correct 2 ms 6744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6620 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 91 ms 30160 KB Output is correct
6 Correct 76 ms 30152 KB Output is correct
7 Correct 86 ms 30160 KB Output is correct
8 Correct 82 ms 29976 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 2 ms 6744 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6580 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 2 ms 6492 KB Output is correct
31 Correct 1 ms 6748 KB Output is correct
32 Correct 2 ms 6744 KB Output is correct
33 Correct 34 ms 15956 KB Output is correct
34 Correct 34 ms 15964 KB Output is correct
35 Correct 28 ms 15960 KB Output is correct
36 Correct 27 ms 15956 KB Output is correct
37 Correct 6 ms 7972 KB Output is correct
38 Correct 30 ms 16212 KB Output is correct
39 Correct 3 ms 7004 KB Output is correct
40 Correct 21 ms 15964 KB Output is correct
41 Correct 22 ms 17364 KB Output is correct
42 Correct 24 ms 17480 KB Output is correct
43 Correct 48 ms 29520 KB Output is correct
44 Correct 35 ms 23424 KB Output is correct