답안 #520656

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
520656 2022-01-30T15:58:46 Z Sohsoh84 Love Polygon (BOI18_polygon) C++17
100 / 100
309 ms 79256 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

#define all(x)			(x).begin(),(x).end()
#define X			first
#define Y			second
#define sep			' '
#define endl			'\n'
#define debug(x)		cerr << #x << ": " <<  x << endl;

const ll MAXN = 1e6 + 10;

int n, dp[MAXN][2], ans, deg[MAXN];
bool vis[MAXN], in_cyc[MAXN];
vector<int> cyc, adj[MAXN], adj_r[MAXN], cmp;
vector<pair<string, string>> vec;
map<string, int> mp;
queue<int> q;

void dfs_cmp(int v) {
	vis[v] = true;
	cmp.push_back(v);
	in_cyc[v] = true;
	if (deg[v] == 0) {
		in_cyc[v] = false;
		q.push(v);
	}

	for (int u : adj[v])
		if (!vis[u])
			dfs_cmp(u);

	for (int u : adj_r[v])
		if (!vis[u])
			dfs_cmp(u);
}

void dfs_cyc(int v, int root) {
	cyc.push_back(v);
	for (int u : adj[v])
		if (in_cyc[u] && u != root)
			dfs_cyc(u, root);
}

inline void find_cyc(int v) {
	cmp.clear();
	cyc.clear();

	dfs_cmp(v);
	while (!q.empty()) {
		int v = q.front();
		q.pop();

		for (int u : adj_r[v]) {
			deg[u]--;
			if (deg[u] == 0) {
				in_cyc[u] = false;
				q.push(u);
			}
		}
	}

	for (int v : cmp) {
		if (in_cyc[v]) {
			dfs_cyc(v, v);
			break;
		}
	}
}

void dfs(int v, int p) {
	int delta = 0;

	for (int u : adj[v]) {
		if (u == p || in_cyc[u]) continue;
		dfs(u, v);

		dp[v][0] += dp[u][1];
		dp[v][1] += dp[u][1];
		delta = max(delta, dp[u][0] - dp[u][1] + 1);
	}
	
	dp[v][1] += delta;
}

inline int calc() {
	int f[2] = {-1, 0};
	for (int v : cyc) {
		int d0 = f[1] + dp[v][0];
		f[1] = max({d0, dp[v][1] + f[1], dp[v][0] + f[0] + 1});
		f[0] = d0;
	}

	return f[1];
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n;
	if (n & 1) return cout << -1 << endl, 0;

	ans = n;
	
	for (int i = 0; i < n; i++) {
		string a, b;
		cin >> a >> b;
		mp[a] = i + 1;
		vec.push_back({a, b});
	}

	for (auto e : vec) {
		int u = mp[e.Y], v = mp[e.X];
		adj[u].push_back(v);
		adj_r[v].push_back(u);
		deg[u]++;
	}

	for (int t = 1; t <= n; t++) {
		if (vis[t]) continue;
		find_cyc(t);

		for (int v : cyc)
			dfs(v, 0);

		if (cyc.size() == 1) ans -= dp[cyc[0]][1];
		else if (cyc.size() == 2) ans -= dp[cyc[0]][0] + dp[cyc[1]][0] + 2;
		else {
			int tans = calc();
			cyc.insert(cyc.begin(), cyc.back());
			cyc.pop_back();
			ans -= max(tans, calc());
		}	
	}

	cout << ans << endl;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 47268 KB Output is correct
2 Correct 29 ms 47240 KB Output is correct
3 Correct 25 ms 47312 KB Output is correct
4 Correct 30 ms 47184 KB Output is correct
5 Correct 29 ms 47224 KB Output is correct
6 Correct 23 ms 47184 KB Output is correct
7 Correct 27 ms 47376 KB Output is correct
8 Correct 25 ms 47216 KB Output is correct
9 Correct 25 ms 47224 KB Output is correct
10 Correct 24 ms 47292 KB Output is correct
11 Correct 24 ms 47268 KB Output is correct
12 Correct 23 ms 47312 KB Output is correct
13 Correct 27 ms 47312 KB Output is correct
14 Correct 27 ms 47292 KB Output is correct
15 Correct 25 ms 47280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 47312 KB Output is correct
2 Correct 24 ms 47312 KB Output is correct
3 Correct 23 ms 47248 KB Output is correct
4 Correct 270 ms 77716 KB Output is correct
5 Correct 230 ms 71096 KB Output is correct
6 Correct 261 ms 78648 KB Output is correct
7 Correct 23 ms 47284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 239 ms 70476 KB Output is correct
2 Correct 309 ms 73268 KB Output is correct
3 Correct 168 ms 71072 KB Output is correct
4 Correct 24 ms 47304 KB Output is correct
5 Correct 261 ms 79256 KB Output is correct
6 Correct 260 ms 70584 KB Output is correct
7 Correct 242 ms 69664 KB Output is correct
8 Correct 214 ms 69956 KB Output is correct
9 Correct 250 ms 69568 KB Output is correct
10 Correct 181 ms 68896 KB Output is correct
11 Correct 24 ms 47220 KB Output is correct
12 Correct 24 ms 47204 KB Output is correct
13 Correct 22 ms 47208 KB Output is correct
14 Correct 23 ms 47284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 47268 KB Output is correct
2 Correct 29 ms 47240 KB Output is correct
3 Correct 25 ms 47312 KB Output is correct
4 Correct 30 ms 47184 KB Output is correct
5 Correct 29 ms 47224 KB Output is correct
6 Correct 23 ms 47184 KB Output is correct
7 Correct 27 ms 47376 KB Output is correct
8 Correct 25 ms 47216 KB Output is correct
9 Correct 25 ms 47224 KB Output is correct
10 Correct 24 ms 47292 KB Output is correct
11 Correct 24 ms 47268 KB Output is correct
12 Correct 23 ms 47312 KB Output is correct
13 Correct 27 ms 47312 KB Output is correct
14 Correct 27 ms 47292 KB Output is correct
15 Correct 25 ms 47280 KB Output is correct
16 Correct 23 ms 47312 KB Output is correct
17 Correct 24 ms 47312 KB Output is correct
18 Correct 23 ms 47248 KB Output is correct
19 Correct 270 ms 77716 KB Output is correct
20 Correct 230 ms 71096 KB Output is correct
21 Correct 261 ms 78648 KB Output is correct
22 Correct 23 ms 47284 KB Output is correct
23 Correct 239 ms 70476 KB Output is correct
24 Correct 309 ms 73268 KB Output is correct
25 Correct 168 ms 71072 KB Output is correct
26 Correct 24 ms 47304 KB Output is correct
27 Correct 261 ms 79256 KB Output is correct
28 Correct 260 ms 70584 KB Output is correct
29 Correct 242 ms 69664 KB Output is correct
30 Correct 214 ms 69956 KB Output is correct
31 Correct 250 ms 69568 KB Output is correct
32 Correct 181 ms 68896 KB Output is correct
33 Correct 24 ms 47220 KB Output is correct
34 Correct 24 ms 47204 KB Output is correct
35 Correct 22 ms 47208 KB Output is correct
36 Correct 23 ms 47284 KB Output is correct
37 Correct 267 ms 71196 KB Output is correct
38 Correct 259 ms 71360 KB Output is correct
39 Correct 250 ms 69792 KB Output is correct
40 Correct 252 ms 70540 KB Output is correct
41 Correct 257 ms 69752 KB Output is correct
42 Correct 292 ms 70796 KB Output is correct
43 Correct 248 ms 70512 KB Output is correct
44 Correct 304 ms 70944 KB Output is correct
45 Correct 291 ms 70848 KB Output is correct
46 Correct 254 ms 70960 KB Output is correct
47 Correct 252 ms 69996 KB Output is correct
48 Correct 226 ms 70368 KB Output is correct
49 Correct 258 ms 73228 KB Output is correct
50 Correct 182 ms 71176 KB Output is correct
51 Correct 26 ms 47312 KB Output is correct
52 Correct 277 ms 79208 KB Output is correct
53 Correct 249 ms 70688 KB Output is correct
54 Correct 264 ms 69696 KB Output is correct
55 Correct 202 ms 69892 KB Output is correct
56 Correct 228 ms 69712 KB Output is correct
57 Correct 170 ms 68932 KB Output is correct
58 Correct 25 ms 47184 KB Output is correct
59 Correct 25 ms 47184 KB Output is correct
60 Correct 25 ms 47312 KB Output is correct
61 Correct 24 ms 47244 KB Output is correct
62 Correct 24 ms 47264 KB Output is correct
63 Correct 25 ms 47200 KB Output is correct
64 Correct 24 ms 47284 KB Output is correct
65 Correct 255 ms 77752 KB Output is correct
66 Correct 222 ms 71180 KB Output is correct
67 Correct 261 ms 78804 KB Output is correct
68 Correct 25 ms 47280 KB Output is correct
69 Correct 25 ms 47292 KB Output is correct
70 Correct 24 ms 47184 KB Output is correct
71 Correct 24 ms 47276 KB Output is correct
72 Correct 22 ms 47280 KB Output is correct
73 Correct 24 ms 47272 KB Output is correct
74 Correct 24 ms 47224 KB Output is correct
75 Correct 23 ms 47312 KB Output is correct
76 Correct 25 ms 47312 KB Output is correct
77 Correct 24 ms 47312 KB Output is correct
78 Correct 24 ms 47312 KB Output is correct
79 Correct 24 ms 47300 KB Output is correct
80 Correct 23 ms 47280 KB Output is correct
81 Correct 24 ms 47312 KB Output is correct
82 Correct 23 ms 47312 KB Output is correct
83 Correct 25 ms 47260 KB Output is correct