Submission #440876

#TimeUsernameProblemLanguageResultExecution timeMemory
440876Haruto810198Love Polygon (BOI18_polygon)C++17
0 / 100
219 ms10264 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define double long double #define FOR(i, l, r, d) for(int i=(l); i<=(r); i+=(d)) #define szof(x) ((int)(x).size()) #define vi vector<int> #define pii pair<int,int> #define pb push_back #define eb emplace_back #define mkp make_pair const int INF = 2147483647; const int LNF = INF*INF; const int MOD = 1000000007; const int mod = 998244353; //#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") const int MAX = 100010; int n; map<string, int> mp; int edge[MAX]; int indeg[MAX]; bool exist[MAX]; vi qu; int res; int query(string str){ if(mp.find(str) == mp.end()){ mp[str] = szof(mp) + 1; } return mp[str]; } signed main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n; FOR(i, 1, n, 1){ string su, sv; cin>>su>>sv; int u = query(su); int v = query(sv); edge[u] = v; indeg[v]++; } if(n%2 == 1){ cout<<-1<<'\n'; return 0; } FOR(i, 1, n, 1){ exist[i] = 1; if(indeg[i] == 0){ qu.pb(i); } } res = n; FOR(i, 1, n, 1){ if(exist[i]==0) continue; if(edge[edge[i]] == i){ res -= 2; exist[i] = 0; exist[edge[i]] = 0; indeg[i]--; indeg[edge[i]]--; } } while(!qu.empty()){ int u = qu.back(); qu.pop_back(); if(exist[u]==0) continue; int v = edge[u]; if(exist[v]==0) continue; int w = edge[v]; exist[u] = 0; exist[v] = 0; indeg[v]--; indeg[w]--; res--; if(indeg[w] == 0 and exist[w] == 1) qu.pb(w); } FOR(i, 1, n, 1){ if(exist[i]==0) continue; int ptr = i; int cnt = 0; while(exist[ptr] == 1){ exist[ptr] = 0; ptr = edge[ptr]; cnt++; } if(ptr == i){ if(cnt == 2){ res -= 2; } else{ res -= cnt / 2; } } } cout<<res<<'\n'; /* FOR(i, 1, n, 1){ cerr<<exist[i]<<" "; } cerr<<endl; */ return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...