# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1051065 | ivopav | Marriage questions (IZhO14_marriage) | C++17 | 1594 ms | 8556 KiB |
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;
struct edg{
long long int ind;
long long int val;
long long int inv;
};
bool dfs(long long int ind,vector<vector<edg>>& gra,vector<bool>& bio){
//cout << ind << "\n";
if (ind==gra.size()-1){
return 1;
}
for (long long int i=0;i<gra[ind].size();i++){
long long int ind2=gra[ind][i].ind;
if (bio[ind2]==1 || gra[ind][i].val==0){
continue;
}
bio[ind2]=1;
//gra[ind2][gra[ind][i].inv].val=1;
gra[ind][i].val=0;
if (dfs(ind2,gra,bio)){
return 1;
}
bio[ind2]=0;
//gra[ind2][gra[ind][i].inv].val=0;
gra[ind][i].val=1;
}
//cout << "aa\n";
return 0;
}
bool mog(long long int n,long long int m,long long int l,long long int r,vector<vector<long long int>>& gra){
//cout << "a\n";
vector<vector<edg>> gra2(n+m+2,vector<edg>{});
for (long long int i=0;i<n;i++){
for (long long int j=0;j<gra[i].size();j++){
long long int ind=gra[i][j];
if (ind-n>=l && ind-n<=r){
gra2[i].push_back({ind,1,gra2[ind].size()});
gra2[ind].push_back({i,0,gra2[i].size()-1});
}
}
}
for (long long int i=0;i<n;i++){
gra2[i].push_back({n+m,0,gra2[n+m].size()});
gra2[n+m].push_back({i,1,gra2[i].size()-1});
}
for (long long int i=n+l;i<=n+r;i++){
gra2[i].push_back({n+m+1,1,gra2[n+m+1].size()});
gra2[n+m+1].push_back({i,0,gra2[i].size()-1});
}
long long int kol=0;
while (true){
//cout << kol << "\n";
vector<bool> bio(n+m+2,0);
if (!dfs(n+m,gra2,bio)){
break;
}
kol++;
}
//cout << "b\n";
//cout << kol << " " << n << " " << l << " " << r << "\n";
if (kol>=n){
return 1;
}
else {
return 0;
}
}
int main(){
long long int n;
long long int m;
long long int k;
cin >> m >> n >> k;
vector<vector<long long int>> gra(n+m,vector<long long int>{});
for (long long int i=0;i<k;i++){
long long int unos;
long long int unos2;
cin >> unos >> unos2;
gra[--unos+n].push_back(--unos2);
gra[unos2].push_back(unos+n);
}
long long int l=0;
long long int r=0;
long long int rje=0;
for (long long int l=0;l<m;l++){
while (r<m){
bool dob=mog(n,m,l,r,gra);
if (dob){
break;
}
r++;
}
rje+=m-r;
if (r<l){
r++;
}
}
cout << rje << "\n";
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |