# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
862292 | 2023-10-18T02:24:16 Z | guagua0407 | Love Polygon (BOI18_polygon) | C++17 | 252 ms | 20556 KB |
//#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
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 3840 KB | Output is correct |
2 | Correct | 1 ms | 3676 KB | Output is correct |
3 | Correct | 1 ms | 3672 KB | Output is correct |
4 | Correct | 1 ms | 3676 KB | Output is correct |
5 | Correct | 1 ms | 3672 KB | Output is correct |
6 | Correct | 1 ms | 3676 KB | Output is correct |
7 | Correct | 1 ms | 3676 KB | Output is correct |
8 | Incorrect | 1 ms | 3676 KB | Output isn't correct |
9 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 3676 KB | Output is correct |
2 | Correct | 2 ms | 3676 KB | Output is correct |
3 | Correct | 1 ms | 3676 KB | Output is correct |
4 | Correct | 241 ms | 16164 KB | Output is correct |
5 | Correct | 190 ms | 14696 KB | Output is correct |
6 | Correct | 252 ms | 16160 KB | Output is correct |
7 | Correct | 1 ms | 3672 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 206 ms | 13820 KB | Output is correct |
2 | Correct | 188 ms | 15696 KB | Output is correct |
3 | Correct | 171 ms | 14516 KB | Output is correct |
4 | Correct | 1 ms | 3672 KB | Output is correct |
5 | Correct | 202 ms | 20556 KB | Output is correct |
6 | Correct | 201 ms | 13140 KB | Output is correct |
7 | Correct | 186 ms | 13060 KB | Output is correct |
8 | Correct | 180 ms | 13588 KB | Output is correct |
9 | Correct | 170 ms | 12580 KB | Output is correct |
10 | Correct | 128 ms | 11976 KB | Output is correct |
11 | Correct | 1 ms | 3676 KB | Output is correct |
12 | Correct | 1 ms | 3676 KB | Output is correct |
13 | Correct | 1 ms | 3928 KB | Output is correct |
14 | Correct | 1 ms | 3676 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 3840 KB | Output is correct |
2 | Correct | 1 ms | 3676 KB | Output is correct |
3 | Correct | 1 ms | 3672 KB | Output is correct |
4 | Correct | 1 ms | 3676 KB | Output is correct |
5 | Correct | 1 ms | 3672 KB | Output is correct |
6 | Correct | 1 ms | 3676 KB | Output is correct |
7 | Correct | 1 ms | 3676 KB | Output is correct |
8 | Incorrect | 1 ms | 3676 KB | Output isn't correct |
9 | Halted | 0 ms | 0 KB | - |