Submission #939994

# Submission time Handle Problem Language Result Execution time Memory
939994 2024-03-07T03:29:45 Z 8pete8 Love Polygon (BOI18_polygon) C++17
29 / 100
257 ms 13392 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];
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);
		if(id[a]==id[b])continue;
		nxt[id[a]]=id[b];
		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]])continue;
		paired[nxt[cur]]=1;
		paired[cur]=1;
		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]&&!deg[i])ans++;
	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 2492 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 Incorrect 1 ms 2396 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Incorrect 1 ms 2392 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 257 ms 12800 KB Output is correct
2 Correct 231 ms 12780 KB Output is correct
3 Correct 156 ms 11088 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 222 ms 12696 KB Output is correct
6 Correct 221 ms 13028 KB Output is correct
7 Correct 219 ms 13004 KB Output is correct
8 Correct 189 ms 13264 KB Output is correct
9 Correct 238 ms 13272 KB Output is correct
10 Correct 165 ms 13392 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 2492 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 Incorrect 1 ms 2396 KB Output isn't correct
8 Halted 0 ms 0 KB -