Submission #440828

# Submission time Handle Problem Language Result Execution time Memory
440828 2021-07-03T10:26:39 Z Haruto810198 Love Polygon (BOI18_polygon) C++17
54 / 100
247 ms 12996 KB
#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;

    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 time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Incorrect 0 ms 204 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 224 ms 11832 KB Output is correct
5 Correct 225 ms 11924 KB Output is correct
6 Correct 239 ms 12008 KB Output is correct
7 Correct 221 ms 11804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 229 ms 12232 KB Output is correct
2 Correct 225 ms 12144 KB Output is correct
3 Correct 178 ms 11876 KB Output is correct
4 Correct 212 ms 11844 KB Output is correct
5 Correct 247 ms 11844 KB Output is correct
6 Correct 245 ms 12532 KB Output is correct
7 Correct 224 ms 12564 KB Output is correct
8 Correct 207 ms 12584 KB Output is correct
9 Correct 210 ms 12996 KB Output is correct
10 Correct 173 ms 12292 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Incorrect 0 ms 204 KB Output isn't correct
9 Halted 0 ms 0 KB -