Submission #940034

# Submission time Handle Problem Language Result Execution time Memory
940034 2024-03-07T04:25:21 Z 8pete8 Love Polygon (BOI18_polygon) C++17
100 / 100
259 ms 13652 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
using namespace std;
#define int long long
#define double long double
const int mod=1e9+7,mxn=1e5+5,lg=30,inf=1e18,minf=-1e18;
void setIO(string name){
	ios_base::sync_with_stdio(0); cin.tie(0);		
	freopen((name+".in").c_str(),"r",stdin);		
	freopen((name+".out").c_str(),"w",stdout);
}	
map<string,int>id;
int t=1;
void put(string a){if(id[a]==0)id[a]=t++;}
int nxt[mxn+10],deg[mxn+10],paired[mxn+10];
bool vis[mxn+10];
int cnt=0;
void dfs(int cur){
	cnt++;
	vis[cur]=1;
	if(!vis[nxt[cur]])dfs(nxt[cur]);
}
int32_t main(){
	int n;cin>>n;
	if(n%2){
		cout<<-1;
		return 0;
	}
	for(int i=0;i<n;i++){
		string a,b;cin>>a>>b;
		put(a);
		put(b);
		nxt[id[a]]=id[b];
		if(nxt[id[b]]==id[a]&&id[a]!=id[b]){
			paired[id[b]]=1;
			paired[id[a]]=1;
		}
		//cout<<id[a]<<" "<<id[b]<<'\n';
		deg[id[b]]++;
	}
	queue<int>q;//with 0 indeg
	for(int i=1;i<=n;i++)if(!deg[i])q.push(i);
	int ans=0;
	while(!q.empty()){
		int cur=q.front();
		q.pop();
		if(paired[nxt[cur]]||paired[cur]){
			nxt[cur]=cur;
			continue;
		}
		paired[nxt[cur]]=1;
		paired[cur]=1;
		//cout<<cur<<" "<<nxt[cur]<<'\n';
		deg[nxt[nxt[cur]]]--;
		if(deg[nxt[nxt[cur]]]==0&&!paired[nxt[nxt[cur]]])q.push(nxt[nxt[cur]]);
		ans++;
	}
	for(int i=1;i<=n;i++)if(!paired[i]&&!vis[i]){
		cnt=0;
		dfs(i);
		ans+=(cnt/2);
		ans+=(cnt%2);
		
	}
	cout<<ans;
}
/*
considering 1 component

we can take the node with no indegree(no likes him :()
then forced the person he likes to likes him back then subtract degree
cost will be 1
we will be left with k people without a pair
then the cost will be 2*k;
*/

Compilation message

polygon.cpp: In function 'void setIO(std::string)':
polygon.cpp:39:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |  freopen((name+".in").c_str(),"r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
polygon.cpp:40:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |  freopen((name+".out").c_str(),"w",stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 242 ms 12676 KB Output is correct
5 Correct 226 ms 12624 KB Output is correct
6 Correct 242 ms 12724 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 231 ms 10768 KB Output is correct
2 Correct 224 ms 12956 KB Output is correct
3 Correct 241 ms 12592 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 257 ms 12628 KB Output is correct
6 Correct 233 ms 13140 KB Output is correct
7 Correct 235 ms 13140 KB Output is correct
8 Correct 235 ms 13140 KB Output is correct
9 Correct 211 ms 13284 KB Output is correct
10 Correct 178 ms 13396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2648 KB Output is correct
19 Correct 242 ms 12676 KB Output is correct
20 Correct 226 ms 12624 KB Output is correct
21 Correct 242 ms 12724 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 231 ms 10768 KB Output is correct
24 Correct 224 ms 12956 KB Output is correct
25 Correct 241 ms 12592 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 257 ms 12628 KB Output is correct
28 Correct 233 ms 13140 KB Output is correct
29 Correct 235 ms 13140 KB Output is correct
30 Correct 235 ms 13140 KB Output is correct
31 Correct 211 ms 13284 KB Output is correct
32 Correct 178 ms 13396 KB Output is correct
33 Correct 1 ms 2396 KB Output is correct
34 Correct 1 ms 2396 KB Output is correct
35 Correct 1 ms 2396 KB Output is correct
36 Correct 1 ms 2396 KB Output is correct
37 Correct 259 ms 12756 KB Output is correct
38 Correct 224 ms 13036 KB Output is correct
39 Correct 238 ms 13048 KB Output is correct
40 Correct 253 ms 13136 KB Output is correct
41 Correct 219 ms 13136 KB Output is correct
42 Correct 231 ms 13148 KB Output is correct
43 Correct 248 ms 13140 KB Output is correct
44 Correct 224 ms 13136 KB Output is correct
45 Correct 235 ms 13036 KB Output is correct
46 Correct 226 ms 13064 KB Output is correct
47 Correct 227 ms 13652 KB Output is correct
48 Correct 221 ms 12884 KB Output is correct
49 Correct 232 ms 12792 KB Output is correct
50 Correct 248 ms 12724 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 225 ms 12716 KB Output is correct
53 Correct 218 ms 13140 KB Output is correct
54 Correct 224 ms 13212 KB Output is correct
55 Correct 243 ms 13136 KB Output is correct
56 Correct 219 ms 13392 KB Output is correct
57 Correct 172 ms 13500 KB Output is correct
58 Correct 1 ms 2400 KB Output is correct
59 Correct 1 ms 2396 KB Output is correct
60 Correct 1 ms 2396 KB Output is correct
61 Correct 1 ms 2396 KB Output is correct
62 Correct 2 ms 2392 KB Output is correct
63 Correct 1 ms 2396 KB Output is correct
64 Correct 1 ms 2396 KB Output is correct
65 Correct 242 ms 12604 KB Output is correct
66 Correct 230 ms 12624 KB Output is correct
67 Correct 246 ms 12680 KB Output is correct
68 Correct 0 ms 344 KB Output is correct
69 Correct 1 ms 2396 KB Output is correct
70 Correct 1 ms 2396 KB Output is correct
71 Correct 1 ms 2396 KB Output is correct
72 Correct 1 ms 2396 KB Output is correct
73 Correct 1 ms 348 KB Output is correct
74 Correct 0 ms 348 KB Output is correct
75 Correct 1 ms 2396 KB Output is correct
76 Correct 1 ms 2396 KB Output is correct
77 Correct 1 ms 2396 KB Output is correct
78 Correct 1 ms 2392 KB Output is correct
79 Correct 1 ms 2392 KB Output is correct
80 Correct 1 ms 2396 KB Output is correct
81 Correct 1 ms 2396 KB Output is correct
82 Correct 1 ms 2396 KB Output is correct
83 Correct 1 ms 2648 KB Output is correct