# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
837242 | 2023-08-25T08:34:31 Z | 1075508020060209tc | Love Polygon (BOI18_polygon) | C++14 | 344 ms | 33444 KB |
#include <bits/stdc++.h> using namespace std; #define int long long int n; vector<int>e[200005]; vector<int>ee[200005]; int dgr[200005]; int alrd[200005]; int lve[200005]; int hs[200005]; void init(){ cin>>n; int __id=0; map<string,int>mp; for(int i=1;i<=n;i++){ string s; string t; cin>>s>>t; if(mp[s]==0){ mp[s]=++__id; } if(mp[t]==0){ mp[t]=++__id; } lve[mp[s]]=mp[t]; e[mp[s]].push_back(mp[t]); // ee[mp[s]].push_back(mp[t]); // ee[mp[t]].push_back(mp[s]); dgr[mp[t]]++; } for(int i=1;i<=n;i++){ if(lve[i]!=i&&lve[lve[i]]==i){ alrd[i]=1; hs[i]=1; dgr[i]=0; } } for(int i=1;i<=n;i++){ if(lve[i]){continue;} if(alrd[e[i][0]]){ e[i][0]=i; lve[i]=i; dgr[i]++; } } if(n%2==1){ cout<<"-1";exit(0); } } int vis[200005]; int dp[200005]; int ans; void dfs(int nw,int pa){ vis[nw]=1; for(int i=0;i<ee[nw].size();i++){ int v=ee[nw][i]; if(v==pa){continue;} dfs(v,nw); dp[nw]+=dp[v]; if(hs[v]==0&&hs[nw]==0){ hs[v]=1;hs[nw]=1; dp[nw]++; ans++; } } } int uf[200005]; int sz[200005]; int fin(int x){ if(uf[x]==x){return x;} uf[x]=fin(uf[x]); return uf[x]; } void mrg(int a,int b){ int pa=fin(a);int pb=fin(b); if(pa==pb){return;} uf[pa]=pb; sz[pb]+=sz[pa]; } signed main() { init(); ans=0; queue<int>q; for(int i=1;i<=n;i++){ if(hs[i]){continue;} if(dgr[i]==0){ q.push(i); } } while(q.size()){ int nw=q.front(); q.pop(); for(int i=0;i<e[nw].size();i++){ int v=e[nw][i]; dgr[v]--; if(dgr[v]==0){ q.push(v); } ee[v].push_back(nw); } } for(int i=1;i<=n;i++){ if(alrd[i]){continue;} if(hs[i]){continue;} if(dgr[i]==1){ dfs(i,0); } } for(int i=1;i<=n;i++){ uf[i]=i; sz[i]=1; } for(int i=1;i<=n;i++){ if(hs[i]){continue;} if(hs[lve[i]]){continue;} mrg(i,lve[i]); } for(int i=1;i<=n;i++){ if(hs[i]){continue;} if(fin(i)==i){ ans+=(sz[fin(i)]+1)/2; } } cout<<ans<<endl; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 9684 KB | Output is correct |
2 | Correct | 5 ms | 9724 KB | Output is correct |
3 | Correct | 5 ms | 9684 KB | Output is correct |
4 | Correct | 6 ms | 9684 KB | Output is correct |
5 | Correct | 4 ms | 9684 KB | Output is correct |
6 | Correct | 6 ms | 9724 KB | Output is correct |
7 | Correct | 5 ms | 9684 KB | Output is correct |
8 | Correct | 6 ms | 9684 KB | Output is correct |
9 | Correct | 4 ms | 9684 KB | Output is correct |
10 | Correct | 4 ms | 9684 KB | Output is correct |
11 | Correct | 4 ms | 9684 KB | Output is correct |
12 | Correct | 4 ms | 9720 KB | Output is correct |
13 | Correct | 4 ms | 9684 KB | Output is correct |
14 | Correct | 5 ms | 9684 KB | Output is correct |
15 | Correct | 4 ms | 9640 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 9684 KB | Output is correct |
2 | Correct | 5 ms | 9684 KB | Output is correct |
3 | Correct | 4 ms | 9684 KB | Output is correct |
4 | Correct | 271 ms | 24532 KB | Output is correct |
5 | Correct | 273 ms | 26060 KB | Output is correct |
6 | Correct | 263 ms | 24580 KB | Output is correct |
7 | Correct | 279 ms | 22108 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 344 ms | 26288 KB | Output is correct |
2 | Correct | 290 ms | 27888 KB | Output is correct |
3 | Correct | 226 ms | 24476 KB | Output is correct |
4 | Correct | 252 ms | 22136 KB | Output is correct |
5 | Correct | 287 ms | 33444 KB | Output is correct |
6 | Correct | 287 ms | 26572 KB | Output is correct |
7 | Correct | 281 ms | 26508 KB | Output is correct |
8 | Correct | 270 ms | 26392 KB | Output is correct |
9 | Correct | 255 ms | 26896 KB | Output is correct |
10 | Correct | 199 ms | 25452 KB | Output is correct |
11 | Correct | 5 ms | 9684 KB | Output is correct |
12 | Correct | 5 ms | 9684 KB | Output is correct |
13 | Correct | 5 ms | 9684 KB | Output is correct |
14 | Correct | 5 ms | 9684 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 9684 KB | Output is correct |
2 | Correct | 5 ms | 9724 KB | Output is correct |
3 | Correct | 5 ms | 9684 KB | Output is correct |
4 | Correct | 6 ms | 9684 KB | Output is correct |
5 | Correct | 4 ms | 9684 KB | Output is correct |
6 | Correct | 6 ms | 9724 KB | Output is correct |
7 | Correct | 5 ms | 9684 KB | Output is correct |
8 | Correct | 6 ms | 9684 KB | Output is correct |
9 | Correct | 4 ms | 9684 KB | Output is correct |
10 | Correct | 4 ms | 9684 KB | Output is correct |
11 | Correct | 4 ms | 9684 KB | Output is correct |
12 | Correct | 4 ms | 9720 KB | Output is correct |
13 | Correct | 4 ms | 9684 KB | Output is correct |
14 | Correct | 5 ms | 9684 KB | Output is correct |
15 | Correct | 4 ms | 9640 KB | Output is correct |
16 | Correct | 4 ms | 9684 KB | Output is correct |
17 | Correct | 5 ms | 9684 KB | Output is correct |
18 | Correct | 4 ms | 9684 KB | Output is correct |
19 | Correct | 271 ms | 24532 KB | Output is correct |
20 | Correct | 273 ms | 26060 KB | Output is correct |
21 | Correct | 263 ms | 24580 KB | Output is correct |
22 | Correct | 279 ms | 22108 KB | Output is correct |
23 | Correct | 344 ms | 26288 KB | Output is correct |
24 | Correct | 290 ms | 27888 KB | Output is correct |
25 | Correct | 226 ms | 24476 KB | Output is correct |
26 | Correct | 252 ms | 22136 KB | Output is correct |
27 | Correct | 287 ms | 33444 KB | Output is correct |
28 | Correct | 287 ms | 26572 KB | Output is correct |
29 | Correct | 281 ms | 26508 KB | Output is correct |
30 | Correct | 270 ms | 26392 KB | Output is correct |
31 | Correct | 255 ms | 26896 KB | Output is correct |
32 | Correct | 199 ms | 25452 KB | Output is correct |
33 | Correct | 5 ms | 9684 KB | Output is correct |
34 | Correct | 5 ms | 9684 KB | Output is correct |
35 | Correct | 5 ms | 9684 KB | Output is correct |
36 | Correct | 5 ms | 9684 KB | Output is correct |
37 | Correct | 300 ms | 26240 KB | Output is correct |
38 | Correct | 278 ms | 26464 KB | Output is correct |
39 | Incorrect | 289 ms | 27184 KB | Output isn't correct |
40 | Halted | 0 ms | 0 KB | - |