This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define MAX 200007
#define int long long
bitset<MAX> visited;
int curSz = 0;
void dfs(int x, int adj[]){
if (visited[x]) return;
curSz++;
visited[x] = 1;
dfs(adj[x], adj);
}
main(){
int n; cin >> n;
if (n % 2 == 1) {cout << -1; return 0;}
map<string, string> init;
map<string, int> indexed;
int adj[n];
for (int x = 0; x < n; x++){
string a, b; cin >> a >> b;
init[a] = b;
}
string name[n]; int ptr = 0;
for (auto y : init){
name[ptr] = y.first;
indexed[y.first] = ptr;
ptr++;
}
int indeg[n]; memset(indeg, 0, sizeof(indeg));
for (auto y : init){
adj[indexed[y.first]] = indexed[y.second];
if (y.first != y.second) indeg[indexed[y.second]]++;
}
bitset<MAX> taken;
queue<int> process;
for (int x = 0; x < n; x++){
if (indeg[x] == 0) process.push(x);
if (x != adj[x] && adj[adj[x]] == x) taken[x] = 1, taken[adj[x]] = 1;
}
int ans = 0;
while (!process.empty()){
int x = process.front();
process.pop();
if (taken[x]) continue;
if (taken[adj[x]]){
adj[x] = x;
//indeg decrease, but doesn't matter since already taken
continue;
}
ans++;
indeg[adj[adj[x]]]--;
if (indeg[adj[adj[x]]] == 0) process.push(adj[adj[x]]);
adj[adj[x]] = x;
taken[x] = 1;
taken[adj[x]] = 1;
}
for (int x = 0; x < n; x++){
if (!taken[x]){
curSz = 0;
dfs(x, adj);
ans += (curSz+1)/2;
}
}
cout << ans;
}
Compilation message (stderr)
polygon.cpp:16:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
16 | main(){
| ^~~~
# | 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... |