| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1333691 | Tony_Tung | Love Polygon (BOI18_polygon) | C++20 | 143 ms | 9336 KiB |
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fi first
#define se second
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()
using namespace std;
const int N = 1e5 + 5;
int nxt[N], in[N];
bool match[N];
map<string, int> mp;
int getId(string &s)
{
if (mp.find(s) == mp.end()) mp[s] = mp.size();
return mp[s];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#define task ""
if (fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int n; cin >> n;
for (int i = 1; i <= n; i++)
{
string s, t; cin >> s >> t;
nxt[getId(s)] = getId(t);
}
if (n&1)
{
cout << -1;
return 0;
}
int cnt0 = 0, cnt1 = 0;
for (int i = 0; i < n; i++)
{
if (!match[i] && nxt[nxt[i]] == i && i != nxt[i])
{
match[i] = match[nxt[i]] = 1;
cnt0++;
}
}
for (int i = 0; i < n; i++)
if (!match[i]) in[nxt[i]]++;
queue<int> q;
for (int i = 0; i < n; i++)
if (!match[i] && !in[i]) q.push(i);
while (q.size())
{
int u = q.front(); q.pop();
if (match[u]) continue;
int v = nxt[u];
if (!match[v])
{
match[u] = match[v] = 1;
cnt1++;
int v1 = nxt[v];
if (!match[v1])
if (--in[v1] == 0) q.push(v1);
}
}
for (int i = 0; i < n; i++)
{
if (!match[i])
{
int cur = i, len = 0;
while (!match[cur])
{
match[cur] = 1;
len++;
cur = nxt[cur];
}
cnt1 += len/2;
}
}
cout << n-2*cnt0-cnt1;
return 0;
}
Compilation message (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... | ||||
