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 <math.h>
//in the name of god,aka allah
//**gray sety orz**
using namespace std;
#define pi pair<long long , long long>
#define pii pair<long long , pair<long long , long long>>
const int maxm = 5e5;
const long long mod = 1e9 + 7 ;
typedef long long ll;
ll l,r,mid;
ll n,m;
ll dis[maxm] , sum[maxm];
bool isval(int mid){
//cout << mid <<" " << mid*mid-mid <<endl;
if (((mid-1)*mid)/2 < m) return 0;
return 1;
}
ll darage[maxm] , ss , mm;
queue<int> q;
vector<pi> g[maxm] , z[maxm];
ll sath[maxm];
bool vis[maxm] , gos[maxm];
ll pedaret[maxm];
ll get_par(ll v){
if (pedaret[v]==v) return v;
return pedaret[v] = get_par(pedaret[v]);
}
void merge(ll r , ll q){
if (get_par(r)!=get_par(q))l+=max(darage[r],darage[q])*1ll*sath[r]*1ll*sath[q];
r = get_par(r) , q = get_par(q);
if (r!=q){
if (sath[r]<sath[q]) swap(r,q);
pedaret[q] = r;
sath[r] += sath[q];
}
return ;
}
ll pars1[maxm] , pars2[maxm];
vector<ll> se[maxm];
set<string> st;
ll rp[maxm];
pi w[maxm];
ll dp[maxm];
//ll rw[maxm][maxm];
map<string,ll> mp;
inline void bfs(){
mid = 0;
while(q.size()){
l = q.front();
q.pop();
mid++;
vis[l]=1;
if(!vis[pedaret[l]]){
if(!vis[pedaret[pedaret[l]]]&&darage[pedaret[pedaret[l]]]-1==0) q.push(pedaret[pedaret[l]]);
vis[pedaret[l]]=1;
}
}
return;
}
int pe(string s){
if(st.find(s)==st.end()) mp.insert({s,mp.size()}) , st.insert(s);
return mp[s];
}
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n;
if(n%2) return cout<<-1,0;
for(int i = 0; i<n; i++){
string s,t;
cin>>s>>t;
l=pe(s);
r=pe(t);
pedaret[l]=r;
darage[r]++;
}
for(int i=0; i<n; i++){
if(pedaret[pedaret[i]]==i && pedaret[i]!=i){
vis[i]=1,vis[pedaret[i]]=1;
}
}
for(int i=0; i<n; i++) {
if(!vis[i] && darage[i]==0) q.push(i);
}
bfs();
for(int i=0; i<n; i++){
if(!vis[i]){
r=1;
ss=pedaret[i];
while (ss!=i) r++ , vis[ss] = 1 , ss=pedaret[ss];
mid+=(r+1)/2;
}
}
cout<<mid;
}
# | 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... |