Submission #486738

# Submission time Handle Problem Language Result Execution time Memory
486738 2021-11-12T15:12:24 Z FatihSolak Love Polygon (BOI18_polygon) C++17
100 / 100
303 ms 24732 KB
#include <bits/stdc++.h>
#define N 200005
using namespace std;
vector<int> adj[N];
int ans = 0;
int edge[N];
bool cycle[N];
int vis[N];
bool del[N];
int root[N];
void dfs(int v){
    //cout << v << " " << endl;
    vis[v] = 1;
    if(vis[edge[v]] == 1){
        cycle[v] = 1;
        int tmp = edge[v];
        while(tmp != v){
            cycle[tmp] = 1;
            tmp = edge[tmp];
        }
    }
    else if(!vis[edge[v]] && !del[edge[v]]){
        dfs(edge[v]);
    }
    else if(del[edge[v]]){
        cycle[v] = 1;
    }
    vis[v] = 2;
}
void dfs2(int v){
    for(auto u:adj[v]){
        if(cycle[u] || del[u])continue;
        dfs2(u);
        ans += !del[u];
        if(!del[u]){
            del[v] = del[u] = 1;
        }
    }
}
void solve(){
    int n;
    cin >> n;
    int cnt = 1;
    map<string,int>val;
    for(int i=1;i<=n;i++){
        string s,t;
        cin >> s >> t;
        if(!val[s])val[s] = cnt++;
        if(!val[t])val[t] = cnt++;
        edge[val[s]] = val[t];
        if(edge[val[t]] == val[s] && s != t){
            del[val[s]] = 1;
            del[val[t]] = 1;
        }
        else adj[val[t]].push_back(val[s]);
    }
    if(n%2){
        cout << -1 << endl;
        return;
    }
    /*
    for(auto u:val){
        cout << u.first << " "<<u.second << " " << del[u.second] << endl;
    }*/
    for(int i=1;i<=n;i++){
        if(!vis[i] && !del[i]){
            //cout << i << " asdas" << endl;
            dfs(i);
        }
    }
    /*
    for(auto u:val){
        cout << u.first << " "<<u.second << " " << cycle[u.second] << endl;
    }*/
    for(int i=1;i<=n;i++){
        if(cycle[i] && !del[i]){
            dfs2(i);
        }
    }
    for(int i=1;i<=n;i++){
        if(del[i])continue;
        vector<int> v;
        int tmp = i;
        while(v.empty() || tmp != i){
            v.push_back(tmp);
            if(!del[edge[tmp]])
                tmp = edge[tmp];
            else {
                tmp = -1;
                break;
            }
        }
        if(tmp == i){
            if(v.size() != 2){
                ans += v.size()/2 + v.size()%2;
            }
            for(auto u:v)del[u] = 1;
            continue;
        }
        int bef = -1;
        for(auto u:adj[i]){
            if(!del[u])bef = u;
        }
        while(bef != -1){
            v.push_back(bef);
            for(auto u:adj[bef]){
                if(!del[u]){
                    bef = u;
                    break;
                }
            }
            if(bef == v.back())bef = -1;
        }
        for(auto u:v)del[u] = 1;
        ans += v.size()/2 + v.size()%2;
    }
    cout << ans;

}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    #ifdef Local
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    int t=1;
    //cin>>t;
    while(t--){
        solve();
    }
    #ifdef Local
    cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
    #endif
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5020 KB Output is correct
2 Correct 2 ms 4940 KB Output is correct
3 Correct 2 ms 4940 KB Output is correct
4 Correct 2 ms 4940 KB Output is correct
5 Correct 2 ms 4940 KB Output is correct
6 Correct 2 ms 4940 KB Output is correct
7 Correct 2 ms 4940 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 2 ms 4940 KB Output is correct
10 Correct 2 ms 4940 KB Output is correct
11 Correct 2 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 2 ms 4940 KB Output is correct
14 Correct 2 ms 4940 KB Output is correct
15 Correct 2 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5020 KB Output is correct
2 Correct 2 ms 5020 KB Output is correct
3 Correct 2 ms 4992 KB Output is correct
4 Correct 275 ms 20996 KB Output is correct
5 Correct 267 ms 17716 KB Output is correct
6 Correct 291 ms 21460 KB Output is correct
7 Correct 261 ms 17236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 277 ms 17096 KB Output is correct
2 Correct 282 ms 18620 KB Output is correct
3 Correct 226 ms 17768 KB Output is correct
4 Correct 261 ms 15684 KB Output is correct
5 Correct 279 ms 23492 KB Output is correct
6 Correct 272 ms 16208 KB Output is correct
7 Correct 264 ms 16224 KB Output is correct
8 Correct 250 ms 16580 KB Output is correct
9 Correct 246 ms 15736 KB Output is correct
10 Correct 182 ms 15000 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 2 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5020 KB Output is correct
2 Correct 2 ms 4940 KB Output is correct
3 Correct 2 ms 4940 KB Output is correct
4 Correct 2 ms 4940 KB Output is correct
5 Correct 2 ms 4940 KB Output is correct
6 Correct 2 ms 4940 KB Output is correct
7 Correct 2 ms 4940 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 2 ms 4940 KB Output is correct
10 Correct 2 ms 4940 KB Output is correct
11 Correct 2 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 2 ms 4940 KB Output is correct
14 Correct 2 ms 4940 KB Output is correct
15 Correct 2 ms 4940 KB Output is correct
16 Correct 3 ms 5020 KB Output is correct
17 Correct 2 ms 5020 KB Output is correct
18 Correct 2 ms 4992 KB Output is correct
19 Correct 275 ms 20996 KB Output is correct
20 Correct 267 ms 17716 KB Output is correct
21 Correct 291 ms 21460 KB Output is correct
22 Correct 261 ms 17236 KB Output is correct
23 Correct 277 ms 17096 KB Output is correct
24 Correct 282 ms 18620 KB Output is correct
25 Correct 226 ms 17768 KB Output is correct
26 Correct 261 ms 15684 KB Output is correct
27 Correct 279 ms 23492 KB Output is correct
28 Correct 272 ms 16208 KB Output is correct
29 Correct 264 ms 16224 KB Output is correct
30 Correct 250 ms 16580 KB Output is correct
31 Correct 246 ms 15736 KB Output is correct
32 Correct 182 ms 15000 KB Output is correct
33 Correct 3 ms 4940 KB Output is correct
34 Correct 3 ms 4940 KB Output is correct
35 Correct 3 ms 4940 KB Output is correct
36 Correct 2 ms 4940 KB Output is correct
37 Correct 303 ms 16836 KB Output is correct
38 Correct 271 ms 18224 KB Output is correct
39 Correct 264 ms 17604 KB Output is correct
40 Correct 262 ms 17476 KB Output is correct
41 Correct 257 ms 17532 KB Output is correct
42 Correct 264 ms 17872 KB Output is correct
43 Correct 266 ms 17944 KB Output is correct
44 Correct 271 ms 17932 KB Output is correct
45 Correct 280 ms 17988 KB Output is correct
46 Correct 279 ms 17936 KB Output is correct
47 Correct 254 ms 17128 KB Output is correct
48 Correct 282 ms 18396 KB Output is correct
49 Correct 281 ms 19908 KB Output is correct
50 Correct 227 ms 19016 KB Output is correct
51 Correct 251 ms 16952 KB Output is correct
52 Correct 274 ms 24732 KB Output is correct
53 Correct 273 ms 17572 KB Output is correct
54 Correct 262 ms 17600 KB Output is correct
55 Correct 251 ms 17912 KB Output is correct
56 Correct 247 ms 17064 KB Output is correct
57 Correct 212 ms 16268 KB Output is correct
58 Correct 3 ms 4940 KB Output is correct
59 Correct 3 ms 4940 KB Output is correct
60 Correct 2 ms 4940 KB Output is correct
61 Correct 3 ms 5024 KB Output is correct
62 Correct 3 ms 4940 KB Output is correct
63 Correct 2 ms 4940 KB Output is correct
64 Correct 3 ms 5000 KB Output is correct
65 Correct 267 ms 22304 KB Output is correct
66 Correct 274 ms 19140 KB Output is correct
67 Correct 266 ms 22684 KB Output is correct
68 Correct 258 ms 18668 KB Output is correct
69 Correct 3 ms 4940 KB Output is correct
70 Correct 2 ms 5016 KB Output is correct
71 Correct 3 ms 4940 KB Output is correct
72 Correct 2 ms 4992 KB Output is correct
73 Correct 3 ms 4928 KB Output is correct
74 Correct 3 ms 4940 KB Output is correct
75 Correct 3 ms 4940 KB Output is correct
76 Correct 3 ms 4940 KB Output is correct
77 Correct 3 ms 4940 KB Output is correct
78 Correct 2 ms 4940 KB Output is correct
79 Correct 2 ms 5012 KB Output is correct
80 Correct 2 ms 4940 KB Output is correct
81 Correct 3 ms 5020 KB Output is correct
82 Correct 2 ms 4940 KB Output is correct
83 Correct 2 ms 5020 KB Output is correct