답안 #797933

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
797933 2023-07-30T07:26:32 Z sentheta Love Polygon (BOI18_polygon) C++17
100 / 100
252 ms 25964 KB
// author : sentheta aka vanwij
#ifdef VANWIJ
	#include"/home/user/code/bits/stdc++.h"
	#define DBG 1
#else
	#include"bits/stdc++.h"
	#define DBG 0
#endif
using namespace std;

#define Int long long
#define V vector
#define pii pair<int,int>
#define ff first
#define ss second

static mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define pow2(x) (1LL<<(x))
#define msb(x) (63-__builtin_clzll(x))
#define bitcnt(x) (__builtin_popcountll(x))

#define nl '\n'
#define _ << ' ' <<
#define all(x) (x).begin(), (x).end()
#define rep(i,a,b) for(int i = (int)(a); i < (int)(b); i++)

#define cerr if(DBG) cout
#define dbg(x) {cerr << "?" << #x << " : " << (x) << endl << flush;}

#define int long long
// const Int MOD = 1e9+7;
const Int MOD = 998244353;
Int bpow(Int a,Int b){
	Int ret = 1;
	for(;b; a=a*a%MOD,b/=2) if(b&1) ret = ret*a%MOD;
	return ret;
}
Int inv(Int a){return bpow(a,MOD-2);}

void solve(); int TC, ALLTC;
signed main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(7);
	// cin >> ALLTC; for(TC=1; TC<=ALLTC; TC++) solve(); return 0;
	solve();
}












const int N = 1e5+5;

int n;

int out[N];
set<int> in[N];

bool flag[N];

int namecnt = 0;
string names[N];
map<string,int> mp;

void solve(){
	
	cin >> n;
	if(n%2){
		cout << -1 << nl; return;
	}
	
	rep(i,0,n){
		string a, b;
		cin >> a >> b;
		
		if(!mp.count(a)){
			mp[a] = namecnt;
			names[namecnt++] = a;
		}
		if(!mp.count(b)){
			mp[b] = namecnt;
			names[namecnt++] = b;
		}
		
		int u = mp[a], v = mp[b];
		out[u] = v;
		in[v].insert(u);
		
		cerr << u _ v << nl;
	}
	
	// {-indeg, x}
	priority_queue<pii> pq;
	rep(x,0,n){
		if(out[x]!=x && out[out[x]]==x){
			flag[x] = 1;
		}
		else{
			pq.push({-in[x].size(), x});
		}
	}
	
	int ans = 0, fre = 0;
	while(!pq.empty()){
		auto[deg,x] = pq.top(); pq.pop();
		deg *= -1;
		if(deg!=(int)in[x].size() || flag[x]) continue;
		
		dbg(x);
		
		if(out[x]!=x && !flag[out[x]]){
			flag[x] = flag[out[x]] = 1;
			
			in[out[out[x]]].erase(out[x]);
			pq.push({-in[out[out[x]]].size(), out[out[x]]});
			
			out[out[x]] = x;
			in[out[x]].erase(x);
			ans++;
		}
		else{
			bool found = 0;
			for(int y : in[x]){
				if(y!=x && !found && !flag[y]){
					flag[x] = flag[y] = 1;
					out[x] = y;
					found = 1;
					ans++;
					break;
				}
			}
			assert(!found);
			if(!found){
				fre++;
			}
		}
		
		dbg(out[x]);
		dbg(out[out[x]]);
	}
	ans += fre;
	
	cout << ans << nl;
	
	
	
	
	
	
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 5 ms 8160 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 4 ms 8232 KB Output is correct
6 Correct 5 ms 8080 KB Output is correct
7 Correct 4 ms 8148 KB Output is correct
8 Correct 3 ms 8172 KB Output is correct
9 Correct 3 ms 8148 KB Output is correct
10 Correct 4 ms 8148 KB Output is correct
11 Correct 4 ms 8148 KB Output is correct
12 Correct 3 ms 8148 KB Output is correct
13 Correct 4 ms 8148 KB Output is correct
14 Correct 3 ms 8148 KB Output is correct
15 Correct 4 ms 8164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 208 ms 23588 KB Output is correct
5 Correct 208 ms 25812 KB Output is correct
6 Correct 247 ms 25804 KB Output is correct
7 Correct 4 ms 8148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 220 ms 23600 KB Output is correct
2 Correct 219 ms 25732 KB Output is correct
3 Correct 162 ms 25720 KB Output is correct
4 Correct 5 ms 8148 KB Output is correct
5 Correct 229 ms 25716 KB Output is correct
6 Correct 215 ms 25688 KB Output is correct
7 Correct 252 ms 25688 KB Output is correct
8 Correct 217 ms 25796 KB Output is correct
9 Correct 203 ms 25768 KB Output is correct
10 Correct 162 ms 25716 KB Output is correct
11 Correct 4 ms 8148 KB Output is correct
12 Correct 3 ms 8164 KB Output is correct
13 Correct 3 ms 8112 KB Output is correct
14 Correct 4 ms 8164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 5 ms 8160 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 4 ms 8232 KB Output is correct
6 Correct 5 ms 8080 KB Output is correct
7 Correct 4 ms 8148 KB Output is correct
8 Correct 3 ms 8172 KB Output is correct
9 Correct 3 ms 8148 KB Output is correct
10 Correct 4 ms 8148 KB Output is correct
11 Correct 4 ms 8148 KB Output is correct
12 Correct 3 ms 8148 KB Output is correct
13 Correct 4 ms 8148 KB Output is correct
14 Correct 3 ms 8148 KB Output is correct
15 Correct 4 ms 8164 KB Output is correct
16 Correct 5 ms 8148 KB Output is correct
17 Correct 4 ms 8148 KB Output is correct
18 Correct 4 ms 8148 KB Output is correct
19 Correct 208 ms 23588 KB Output is correct
20 Correct 208 ms 25812 KB Output is correct
21 Correct 247 ms 25804 KB Output is correct
22 Correct 4 ms 8148 KB Output is correct
23 Correct 220 ms 23600 KB Output is correct
24 Correct 219 ms 25732 KB Output is correct
25 Correct 162 ms 25720 KB Output is correct
26 Correct 5 ms 8148 KB Output is correct
27 Correct 229 ms 25716 KB Output is correct
28 Correct 215 ms 25688 KB Output is correct
29 Correct 252 ms 25688 KB Output is correct
30 Correct 217 ms 25796 KB Output is correct
31 Correct 203 ms 25768 KB Output is correct
32 Correct 162 ms 25716 KB Output is correct
33 Correct 4 ms 8148 KB Output is correct
34 Correct 3 ms 8164 KB Output is correct
35 Correct 3 ms 8112 KB Output is correct
36 Correct 4 ms 8164 KB Output is correct
37 Correct 205 ms 25708 KB Output is correct
38 Correct 216 ms 25696 KB Output is correct
39 Correct 222 ms 25804 KB Output is correct
40 Correct 204 ms 25752 KB Output is correct
41 Correct 227 ms 25704 KB Output is correct
42 Correct 223 ms 25800 KB Output is correct
43 Correct 238 ms 25776 KB Output is correct
44 Correct 213 ms 25768 KB Output is correct
45 Correct 230 ms 25772 KB Output is correct
46 Correct 226 ms 25780 KB Output is correct
47 Correct 239 ms 25708 KB Output is correct
48 Correct 210 ms 25740 KB Output is correct
49 Correct 199 ms 25760 KB Output is correct
50 Correct 142 ms 25680 KB Output is correct
51 Correct 3 ms 8148 KB Output is correct
52 Correct 225 ms 25964 KB Output is correct
53 Correct 191 ms 25780 KB Output is correct
54 Correct 191 ms 25792 KB Output is correct
55 Correct 192 ms 25856 KB Output is correct
56 Correct 188 ms 25756 KB Output is correct
57 Correct 144 ms 25792 KB Output is correct
58 Correct 3 ms 8148 KB Output is correct
59 Correct 3 ms 8148 KB Output is correct
60 Correct 3 ms 8164 KB Output is correct
61 Correct 3 ms 8148 KB Output is correct
62 Correct 3 ms 8148 KB Output is correct
63 Correct 5 ms 8148 KB Output is correct
64 Correct 4 ms 8164 KB Output is correct
65 Correct 181 ms 25800 KB Output is correct
66 Correct 190 ms 25796 KB Output is correct
67 Correct 184 ms 25688 KB Output is correct
68 Correct 4 ms 8148 KB Output is correct
69 Correct 3 ms 8148 KB Output is correct
70 Correct 5 ms 8300 KB Output is correct
71 Correct 3 ms 8148 KB Output is correct
72 Correct 3 ms 8148 KB Output is correct
73 Correct 4 ms 8172 KB Output is correct
74 Correct 3 ms 8148 KB Output is correct
75 Correct 3 ms 8148 KB Output is correct
76 Correct 3 ms 8168 KB Output is correct
77 Correct 3 ms 8164 KB Output is correct
78 Correct 4 ms 8164 KB Output is correct
79 Correct 3 ms 8148 KB Output is correct
80 Correct 3 ms 8148 KB Output is correct
81 Correct 4 ms 8096 KB Output is correct
82 Correct 3 ms 8148 KB Output is correct
83 Correct 3 ms 8148 KB Output is correct