# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
939998 | 2024-03-07T03:37:33 Z | 8pete8 | Love Polygon (BOI18_polygon) | C++17 | 222 ms | 12732 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]; 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]&&!vis[i]){ cnt=0; dfs(i); if(cnt==2)continue; 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
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 2392 KB | Output is correct |
2 | Incorrect | 1 ms | 2396 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 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 | 182 ms | 12732 KB | Output is correct |
5 | Correct | 178 ms | 12628 KB | Output is correct |
6 | Correct | 180 ms | 12660 KB | Output is correct |
7 | Correct | 0 ms | 348 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 222 ms | 12004 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 2392 KB | Output is correct |
2 | Incorrect | 1 ms | 2396 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |