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>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pi;
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define fip(a,b) for(ll i = a; i < b ; i++)
#define fjp(a,b) for(ll j = a; j < b ; j++)
#define fp(k,a,b) for(ll k = a; k < b ; k++)
#define fin(a,b) for(ll i = a; i >= b ; i--)
#define fjn(a,b) for(ll i = a; j >= b ; j--)
#define fn(k,a,b) for(ll k = a; k >= b ; k--)
#define fx(a) for(auto x : a)
#define ordered_set tree<pi,null_type,less<pi>,rb_tree_tag,tree_order_statistics_node_update>
#define total_one(k) __builtin_popcount(k)
#define back_zero(k) __builtin_ctzll(k)
#define front_zero(k) __builtin_clzll(k)
#define nli "\n"
ll n,m,ans,sum,res,cnt,tptp,tp,tp2;
const ll mx = 1e5 + 5;
const ll mod = 1e9 + 7;
unordered_map<string,ll> hashe;
string k1,k2;
vi adj[mx];
vi reverse_adj[mx];
bool vis[mx];
ll find_root(ll k){
if(adj[k].front() == k)
return k;
fx(adj[k])
return find_root(x);
}
ll dfs(ll u,ll root){
vis[u] = 1;
ll sz = 1;
fx(reverse_adj[u])
if(!vis[x]){
sz += dfs(x,root);
}
if(sz>=2){
ans += sz - 1;
sz = 0;
}
if(sz==1 && u == root){
ans++;
}
return sz;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
// #ifdef code
freopen("input1.txt","r",stdin);
freopen("output1.txt","w",stdout);
freopen("error1.txt","w",stderr);
// #endif
cin>>n;
tptp = 0;
ans = 0;
cnt = 0;
fip(1,n+1){
cin>>k1>>k2;
if(!hashe.count(k1)){
cnt++;
hashe[k1] = cnt;
}
if(!hashe.count(k2)){
cnt++;
hashe[k2] = cnt;
}
adj[hashe[k1]].push_back(hashe[k2]);
reverse_adj[hashe[k2]].push_back(hashe[k1]);
}
if(n&1){
cout<<-1<<nli;
return 0;
}
fip(1,n+1){
if(!vis[i]){
tptp = find_root(i);
dfs(tptp,tptp);
}
}
cout<<ans<<nli;
// cerr << "Time elapsed : " << setprecision(6) << 100.00 * clock() / CLOCKS_PER_SEC << "ms\n";
}
Compilation message (stderr)
polygon.cpp: In function 'll find_root(ll)':
polygon.cpp:41:1: warning: control reaches end of non-void function [-Wreturn-type]
41 | }
| ^
polygon.cpp: In function 'int main()':
polygon.cpp:63:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen("input1.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
polygon.cpp:64:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen("output1.txt","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
polygon.cpp:65:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | freopen("error1.txt","w",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... |