Submission #837248

# Submission time Handle Problem Language Result Execution time Memory
837248 2023-08-25T08:36:46 Z 1075508020060209tc Love Polygon (BOI18_polygon) C++14
100 / 100
305 ms 35540 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(alrd[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

polygon.cpp: In function 'void dfs(long long int, long long int)':
polygon.cpp:56:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 | for(int i=0;i<ee[nw].size();i++){
      |             ~^~~~~~~~~~~~~~
polygon.cpp: In function 'int main()':
polygon.cpp:96:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |     for(int i=0;i<e[nw].size();i++){
      |                 ~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 5 ms 9648 KB Output is correct
4 Correct 4 ms 9684 KB Output is correct
5 Correct 4 ms 9640 KB Output is correct
6 Correct 4 ms 9684 KB Output is correct
7 Correct 4 ms 9684 KB Output is correct
8 Correct 5 ms 9696 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 5 ms 9696 KB Output is correct
11 Correct 5 ms 9716 KB Output is correct
12 Correct 4 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 6 ms 9684 KB Output is correct
15 Correct 4 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 9684 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 252 ms 24480 KB Output is correct
5 Correct 273 ms 26104 KB Output is correct
6 Correct 266 ms 24536 KB Output is correct
7 Correct 263 ms 22332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 291 ms 26288 KB Output is correct
2 Correct 296 ms 27972 KB Output is correct
3 Correct 229 ms 24584 KB Output is correct
4 Correct 247 ms 22284 KB Output is correct
5 Correct 296 ms 33452 KB Output is correct
6 Correct 293 ms 26552 KB Output is correct
7 Correct 262 ms 26532 KB Output is correct
8 Correct 252 ms 26348 KB Output is correct
9 Correct 239 ms 26820 KB Output is correct
10 Correct 187 ms 25472 KB Output is correct
11 Correct 4 ms 9684 KB Output is correct
12 Correct 4 ms 9684 KB Output is correct
13 Correct 4 ms 9684 KB Output is correct
14 Correct 4 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 5 ms 9648 KB Output is correct
4 Correct 4 ms 9684 KB Output is correct
5 Correct 4 ms 9640 KB Output is correct
6 Correct 4 ms 9684 KB Output is correct
7 Correct 4 ms 9684 KB Output is correct
8 Correct 5 ms 9696 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 5 ms 9696 KB Output is correct
11 Correct 5 ms 9716 KB Output is correct
12 Correct 4 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 6 ms 9684 KB Output is correct
15 Correct 4 ms 9684 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 252 ms 24480 KB Output is correct
20 Correct 273 ms 26104 KB Output is correct
21 Correct 266 ms 24536 KB Output is correct
22 Correct 263 ms 22332 KB Output is correct
23 Correct 291 ms 26288 KB Output is correct
24 Correct 296 ms 27972 KB Output is correct
25 Correct 229 ms 24584 KB Output is correct
26 Correct 247 ms 22284 KB Output is correct
27 Correct 296 ms 33452 KB Output is correct
28 Correct 293 ms 26552 KB Output is correct
29 Correct 262 ms 26532 KB Output is correct
30 Correct 252 ms 26348 KB Output is correct
31 Correct 239 ms 26820 KB Output is correct
32 Correct 187 ms 25472 KB Output is correct
33 Correct 4 ms 9684 KB Output is correct
34 Correct 4 ms 9684 KB Output is correct
35 Correct 4 ms 9684 KB Output is correct
36 Correct 4 ms 9684 KB Output is correct
37 Correct 305 ms 26344 KB Output is correct
38 Correct 277 ms 26400 KB Output is correct
39 Correct 261 ms 27204 KB Output is correct
40 Correct 270 ms 28636 KB Output is correct
41 Correct 270 ms 28616 KB Output is correct
42 Correct 266 ms 28568 KB Output is correct
43 Correct 262 ms 28668 KB Output is correct
44 Correct 279 ms 28556 KB Output is correct
45 Correct 279 ms 28572 KB Output is correct
46 Correct 276 ms 28612 KB Output is correct
47 Correct 257 ms 28876 KB Output is correct
48 Correct 280 ms 28452 KB Output is correct
49 Correct 273 ms 30148 KB Output is correct
50 Correct 261 ms 26692 KB Output is correct
51 Correct 235 ms 24260 KB Output is correct
52 Correct 282 ms 35540 KB Output is correct
53 Correct 285 ms 28660 KB Output is correct
54 Correct 288 ms 28636 KB Output is correct
55 Correct 260 ms 28508 KB Output is correct
56 Correct 246 ms 28992 KB Output is correct
57 Correct 195 ms 27648 KB Output is correct
58 Correct 5 ms 9684 KB Output is correct
59 Correct 4 ms 9684 KB Output is correct
60 Correct 5 ms 9672 KB Output is correct
61 Correct 5 ms 9684 KB Output is correct
62 Correct 5 ms 9684 KB Output is correct
63 Correct 5 ms 9700 KB Output is correct
64 Correct 5 ms 9704 KB Output is correct
65 Correct 253 ms 26684 KB Output is correct
66 Correct 258 ms 28316 KB Output is correct
67 Correct 261 ms 26712 KB Output is correct
68 Correct 241 ms 24272 KB Output is correct
69 Correct 5 ms 9696 KB Output is correct
70 Correct 5 ms 9684 KB Output is correct
71 Correct 5 ms 9684 KB Output is correct
72 Correct 4 ms 9756 KB Output is correct
73 Correct 4 ms 9684 KB Output is correct
74 Correct 5 ms 9684 KB Output is correct
75 Correct 5 ms 9684 KB Output is correct
76 Correct 4 ms 9684 KB Output is correct
77 Correct 5 ms 9656 KB Output is correct
78 Correct 4 ms 9684 KB Output is correct
79 Correct 5 ms 9704 KB Output is correct
80 Correct 6 ms 9812 KB Output is correct
81 Correct 5 ms 9700 KB Output is correct
82 Correct 6 ms 9684 KB Output is correct
83 Correct 5 ms 9684 KB Output is correct