Submission #1038190

# Submission time Handle Problem Language Result Execution time Memory
1038190 2024-07-29T14:12:12 Z codexistent Love Polygon (BOI18_polygon) C++14
0 / 100
2000 ms 33616 KB
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define f first
#define s second
 
int n, ec[MAXN];
string st[MAXN], nx[MAXN];
map<string, int> idx;
set<int> adj[MAXN];
bool d[MAXN];
 
int main(){
    ios::sync_with_stdio(false);
	cin.tie(nullptr);

    cin >> n;
    FOR(i, 1, n) ec[i] = 0;
    FOR(i, 1, n){
        cin >> st[i];
        idx.insert({st[i], i});
        cin >> nx[i];
    }
 
    if(n % 2) {
        cout << -1 << endl;
        return 0;
    }
 
    int r = 0;
    FOR(i, 1, n){
        d[i] = (idx[nx[idx[nx[i]]]] == i && idx[nx[i]] != i);
    }
 
    FOR(i, 1, n){
        if(i == idx[nx[i]] || d[i] || d[idx[nx[i]]]) continue;
        ec[i]++, ec[idx[nx[i]]]++;
    }
 
    FOR(i, 1, n){
        if(i == idx[nx[i]] || d[i] || d[idx[nx[i]]]) continue;
        adj[i].insert(idx[nx[i]]);
        //cout << "DCVVB: " << idx[nx[i]] << endl;
        adj[idx[nx[i]]].insert(i);
    }
 
    set<pair<int, int>> se;
    FOR(i, 1, n) if(!d[i]) se.insert({ec[i], i});
    
    bool v[MAXN];
    FOR(i, 1, n) v[i] = false;
    while(se.size()){
        auto si = *se.begin();
        se.erase(se.begin());
        v[si.s] = true;
 
        if(si.f == 0){
            r++;
            //cout << st[si.s] << " FALLALEUJAH! " << endl;
        }else{
            int j = 0, jmn = INT_MAX;
            for(int i : adj[si.s]){
                if(jmn > ec[i]) j = i, jmn = ec[i];
            }
            r++;
            //cout << st[si.s] << " ~ " << st[j] << " : " << si.s << " " << j << endl;
 
            se.erase({jmn, j});
            v[j] = true;

            for(int j2 : adj[j]) if(!v[j2]) {
                se.erase({ec[j2], j2});
                adj[j2].erase(j), ec[j2]--;
                se.insert({ec[j2], j2});
            }
        }
 
        for(int i2 : adj[si.s]) if(v[i2]) {
            se.erase({ec[i2], i2});
            adj[i2].erase(si.s), ec[i2]--;
            se.insert({ec[i2], i2});
        }
    }

 
    cout << r << "\n";
}
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 11352 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11356 KB Output is correct
2 Correct 2 ms 11356 KB Output is correct
3 Execution timed out 2077 ms 11356 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 377 ms 33616 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 11352 KB Output isn't correct
2 Halted 0 ms 0 KB -