Submission #862292

#TimeUsernameProblemLanguageResultExecution timeMemory
862292guagua0407Love Polygon (BOI18_polygon)C++17
54 / 100
252 ms20556 KiB
//#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int,int> #define f first #define s second #define all(x) x.begin(),x.end() #define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); void setIO(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } const int mxn=1e5+5; map<string,int> mp; vector<int> adj[mxn]; bool visited[mxn]; bool visited2[mxn]; int nxt[mxn]; bool is_cycle[mxn]; bool used[mxn]; int prv[mxn]; vector<int> tmp; int dif; void dfs(int v){ visited[v]=true; for(auto u:adj[v]){ if(is_cycle[u]) continue; dfs(u); if(!used[u] and !used[v]){ used[u]=true; used[v]=true; } } } int main() {_ int n; cin>>n; if(n&1){ cout<<-1<<'\n'; return 0; } int cnt=0; for(int i=0;i<n;i++){ string a,b; cin>>a>>b; if(!mp.count(a)) mp[a]=cnt++; if(!mp.count(b)) mp[b]=cnt++; nxt[mp[a]]=mp[b]; adj[mp[b]].push_back(mp[a]); } for(int i=0;i<n;i++){ if(visited[i]) continue; vector<int> vec; int cur=i; while(!visited[cur]){ vec.push_back(cur); visited[cur]=true; cur=nxt[cur]; } vector<int> cycle; reverse(all(vec)); for(auto v:vec){ is_cycle[v]=true; cycle.push_back(v); if(v==cur){ break; } } for(auto v:cycle){ dfs(v); } int cur2=cur; do{ if(used[cur2]) break; cur2=nxt[cur2]; }while(cur2!=cur); int cur3=cur2; vector<int> tmp2; do{ if(used[cur3]){ for(auto v:tmp2){ used[v]=true; } if((int)tmp2.size()%2==1){ used[tmp2.back()]=false; } tmp2.clear(); } else{ tmp2.push_back(cur3); } cur3=nxt[cur3]; }while(cur3!=cur2); if((int)cycle.size()==2 and (int)tmp2.size()==2){ dif++; } for(auto v:tmp2){ used[v]=true; } if((int)tmp2.size()%2==1){ used[tmp2.back()]=false; } tmp2.clear(); } int cnt2=0; for(int i=0;i<n;i++){ //cout<<used[i]<<' '; if(used[i]) cnt2++; } //cout<<'\n'; cout<<(cnt2/2+n-cnt2-dif)<<'\n'; return 0; } //maybe its multiset not set

Compilation message (stderr)

polygon.cpp: In function 'void setIO(std::string)':
polygon.cpp:12:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
polygon.cpp:13:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...