| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1333718 | Hduong | Love Polygon (BOI18_polygon) | C++20 | 27 ms | 13360 KiB |
#include <bits/stdc++.h>
#define task "task"
using namespace std;
const long long INF = 1e18;
const int N = 1e5 + 5;
long long n, love[N], in_degree[N];
bool matched[N];
string names[N];
struct node {
string key;
long long val;
node* next;
};
node* head[N];
long long cnt = 1;
long long get_id(string s) {
unsigned long long h = 0;
for (char c : s) h = h * 31 + c;
long long idx = h % n ;
node* curr = head[idx];
while (curr != nullptr) {
if (curr -> key == s) return curr->val;
curr = curr->next;
}
node* newnode = new node();
newnode -> key = s;
newnode -> val = cnt++;
newnode -> next = head[idx];
head[idx] = newnode;
return newnode -> val;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> n;
if (n % 2 != 0) {
cout << -1 << endl;
return 0;
}
for (int i = 1; i <= n; i++) {
string u_s, v_s;
cin >> u_s >> v_s;
long long u = get_id(u_s), v = get_id(v_s);
love[u] = v;
in_degree[v]++;
}
long long res = 0;
for (int i = 1; i <= n; i++) {
if (!matched[i]) {
int j = love[i];
if (i != j and love[j] == i and !matched[j]) {
matched[i] = true;
matched[j] = true;
}
}
}
queue <long long > q;
for (int i = 1; i <= n; i++) {
if (in_degree[i] == 0) {
q.push(i);
}
}
while (!q.empty()) {
long long u = q.front();
q.pop();
if (matched[u]) continue;
long long v = love[u];
if (!matched[v]) {
matched[u] = true;
matched[v] = true;
res += 1;
}
in_degree[v]--;
if (in_degree[v] == 0) q.push(v);
}
for (int i = 1; i <= n; i++) {
if (!matched[i]) {
long long curr = i, siz = 0;
vector<long long> cycle_nodes;
while (!matched[curr]) {
matched[curr] = true;
cycle_nodes.push_back(curr);
curr = love[curr];
siz++;
}
res += (siz / 2);
if (siz % 2 != 0) {
matched[cycle_nodes.back()] = false;
}
}
}
long long tmp = 0;
for (int i = 1; i <= n; i++) {
if (!matched[i]) tmp++;
}
res += tmp;
cout << res;
}
컴파일 시 표준 에러 (stderr) 메시지
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
