답안 #386231

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
386231 2021-04-06T07:27:43 Z maximath_1 Love Polygon (BOI18_polygon) C++11
100 / 100
306 ms 12644 KB
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string.h>
#include <numeric>
#include <iostream>
#include <queue>
#include <assert.h>
#include <map>
#include <set>
#include <limits.h>
#include <random>
#include <chrono>
using namespace std;
 
#define ll long long
#define ld long double
const int MX = 200005;
const int BLOCK = 105;
const ll mod = 1e9 + 7;
const ll inv2 = (mod + 1) / 2;
const int dxh[] = {1, 1, -1, -1, 2, 2, -2, -2};
const int dyh[] = {2, -2, 2, -2, 1, -1, 1, -1}; // horse
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1}; // adj
const int dxd[] = {1, 1, 1, 0, -1, -1, -1, 0};
const int dyd[] = {1, 0, -1, -1, -1, 0, 1, 1}; // diag

int n, to[MX], indeg[MX], vis[MX], ans = 0;
vector<pair<int, int> > edge;
map<string, int> mp;
int mpid = 0;

int main(){
	cin.tie(0) -> sync_with_stdio(0);

	cin >> n; 
	if(n % 2){
		cout << -1 << endl;
		return 0;
	}

	string a, b;
	for(int i = 0; i < n; i ++){
		cin >> a >> b;
		if(!mp.count(a)) mp[a] = mpid ++;
		if(!mp.count(b)) mp[b] = mpid ++;
		int u = mp[a], v = mp[b];
		edge.push_back({u, v});

		indeg[v] ++;
		to[u] = v;
	}

	queue<int> lonely;
	for(int i = 0; i < n; i ++){
		if(!indeg[i]) lonely.push(i);
		if(to[i] != i && to[to[i]] == i)
			vis[i] = vis[to[i]] = 1;
	}

	for(; lonely.size();){
		int nw = lonely.front();
		lonely.pop();
		vis[nw] = 1;

		if(!vis[to[nw]]){
			vis[to[nw]] = 1;
			indeg[to[to[nw]]] --;
			if(!indeg[to[to[nw]]])
				lonely.push(to[to[nw]]);
		}
		ans ++;
	}

	for(int i = 0; i < n; i ++) if(!vis[i]){
		vis[i] = 1;
		int nw = i, cnt = 1;

		for(; !vis[to[nw]]; nw = to[nw], cnt ++)
			vis[to[nw]] = 1;

		ans += (cnt + 1) / 2;
	}

	cout << ans << endl;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 400 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 2 ms 364 KB Output is correct
9 Correct 2 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 247 ms 12368 KB Output is correct
5 Correct 253 ms 12364 KB Output is correct
6 Correct 250 ms 12260 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 254 ms 12564 KB Output is correct
2 Correct 252 ms 12428 KB Output is correct
3 Correct 198 ms 12384 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 250 ms 12260 KB Output is correct
6 Correct 241 ms 12556 KB Output is correct
7 Correct 242 ms 12512 KB Output is correct
8 Correct 231 ms 12472 KB Output is correct
9 Correct 228 ms 12628 KB Output is correct
10 Correct 178 ms 12384 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 2 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 400 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 2 ms 364 KB Output is correct
9 Correct 2 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 2 ms 492 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 247 ms 12368 KB Output is correct
20 Correct 253 ms 12364 KB Output is correct
21 Correct 250 ms 12260 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 254 ms 12564 KB Output is correct
24 Correct 252 ms 12428 KB Output is correct
25 Correct 198 ms 12384 KB Output is correct
26 Correct 2 ms 364 KB Output is correct
27 Correct 250 ms 12260 KB Output is correct
28 Correct 241 ms 12556 KB Output is correct
29 Correct 242 ms 12512 KB Output is correct
30 Correct 231 ms 12472 KB Output is correct
31 Correct 228 ms 12628 KB Output is correct
32 Correct 178 ms 12384 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 2 ms 364 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 253 ms 12436 KB Output is correct
38 Correct 243 ms 12496 KB Output is correct
39 Correct 242 ms 12512 KB Output is correct
40 Correct 257 ms 12548 KB Output is correct
41 Correct 245 ms 12624 KB Output is correct
42 Correct 243 ms 12512 KB Output is correct
43 Correct 234 ms 12512 KB Output is correct
44 Correct 257 ms 12512 KB Output is correct
45 Correct 244 ms 12512 KB Output is correct
46 Correct 244 ms 12540 KB Output is correct
47 Correct 233 ms 12640 KB Output is correct
48 Correct 252 ms 12384 KB Output is correct
49 Correct 249 ms 12516 KB Output is correct
50 Correct 198 ms 12464 KB Output is correct
51 Correct 1 ms 364 KB Output is correct
52 Correct 250 ms 12260 KB Output is correct
53 Correct 233 ms 12512 KB Output is correct
54 Correct 254 ms 12488 KB Output is correct
55 Correct 246 ms 12644 KB Output is correct
56 Correct 226 ms 12640 KB Output is correct
57 Correct 186 ms 12384 KB Output is correct
58 Correct 1 ms 364 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 1 ms 364 KB Output is correct
62 Correct 1 ms 364 KB Output is correct
63 Correct 1 ms 364 KB Output is correct
64 Correct 1 ms 364 KB Output is correct
65 Correct 252 ms 12260 KB Output is correct
66 Correct 242 ms 12256 KB Output is correct
67 Correct 306 ms 12276 KB Output is correct
68 Correct 1 ms 364 KB Output is correct
69 Correct 1 ms 364 KB Output is correct
70 Correct 2 ms 364 KB Output is correct
71 Correct 1 ms 364 KB Output is correct
72 Correct 1 ms 364 KB Output is correct
73 Correct 1 ms 364 KB Output is correct
74 Correct 1 ms 364 KB Output is correct
75 Correct 1 ms 364 KB Output is correct
76 Correct 1 ms 364 KB Output is correct
77 Correct 1 ms 364 KB Output is correct
78 Correct 1 ms 364 KB Output is correct
79 Correct 1 ms 364 KB Output is correct
80 Correct 1 ms 364 KB Output is correct
81 Correct 1 ms 384 KB Output is correct
82 Correct 1 ms 364 KB Output is correct
83 Correct 1 ms 364 KB Output is correct