#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
const int nmax=30005;
const int mmax=100005;
vector<int> v[nmax];
int l[nmax],r[nmax],L[nmax],R[nmax];
int ls[mmax];
bool viz[nmax];
bool changed;
int n,m,i,j,k,a,b,p,cupl,ans,nr;
bool cup(int x)
{
if(viz[x])
return 0;
viz[x]=1;
for(int it=L[x];it<=R[x]&&ls[it]<=i;it++)
if(!r[ls[it]])
{
l[x]=ls[it];
r[ls[it]]=x;
return 1;
}
for(int it=L[x];it<=R[x]&&ls[it]<=i;it++)
if(cup(r[ls[it]]))
{
l[x]=ls[it];
r[ls[it]]=x;
return 1;
}
return 0;
}
void cc()
{
changed=1;
while(changed&&cupl<m)
{
changed=0;
for(j=1; j<=m; j++)
if((!l[j])&&cup(j))
{
cupl++;
changed=1;
}
for(j=1;j<=m;j++)
viz[j]=0;
}
}
int ch,num;
string s;
int getnum()
{
num=0;
while(s[ch]>='0'&&s[ch]<='9')
{
num=num*10+s[ch]-'0';
ch++;
}
ch++;
return num;
}
int main()
{
//freopen("data.in","r",stdin);
ios_base::sync_with_stdio(false);
cin>>n>>m>>k;
getline(cin,s);
for(i=1;i<=k;i++)
{
getline(cin,s);ch=0;
a=getnum();b=getnum();
v[b].push_back(a);
}
for(i=1;i<=m;i++)
{
L[i]=nr+1;sort(v[i].begin(),v[i].end());
for(j=0;j<v[i].size();j++)
{
ls[++nr]=v[i][j];
}
R[i]=nr;
}
p=1;
for(i=1;i<=n;i++)
{
if(i>=m)cc();
while(cupl==m)
{
for(j=1;j<=m;j++)
while(L[j]<=R[j]&&ls[L[j]]<=p)
L[j]++;
if(r[p])
l[r[p]]=0,r[p]=0,cupl--;
p++;
cc();
}
ans+=(p-1);
}
cout<<ans;
return 0;
}
Compilation message
marriage.cpp: In function 'int main()':
marriage.cpp:81:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0;j<v[i].size();j++)
~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1016 KB |
Output is correct |
2 |
Correct |
3 ms |
1152 KB |
Output is correct |
3 |
Correct |
3 ms |
1152 KB |
Output is correct |
4 |
Correct |
3 ms |
1152 KB |
Output is correct |
5 |
Correct |
3 ms |
1224 KB |
Output is correct |
6 |
Correct |
3 ms |
1256 KB |
Output is correct |
7 |
Correct |
3 ms |
1428 KB |
Output is correct |
8 |
Correct |
4 ms |
1428 KB |
Output is correct |
9 |
Correct |
3 ms |
1428 KB |
Output is correct |
10 |
Correct |
3 ms |
1428 KB |
Output is correct |
11 |
Correct |
3 ms |
1428 KB |
Output is correct |
12 |
Correct |
3 ms |
1428 KB |
Output is correct |
13 |
Correct |
3 ms |
1428 KB |
Output is correct |
14 |
Correct |
3 ms |
1428 KB |
Output is correct |
15 |
Correct |
3 ms |
1428 KB |
Output is correct |
16 |
Correct |
3 ms |
1428 KB |
Output is correct |
17 |
Correct |
3 ms |
1428 KB |
Output is correct |
18 |
Correct |
3 ms |
1428 KB |
Output is correct |
19 |
Correct |
4 ms |
1428 KB |
Output is correct |
20 |
Correct |
3 ms |
1428 KB |
Output is correct |
21 |
Correct |
3 ms |
1428 KB |
Output is correct |
22 |
Correct |
3 ms |
1428 KB |
Output is correct |
23 |
Correct |
3 ms |
1428 KB |
Output is correct |
24 |
Correct |
3 ms |
1428 KB |
Output is correct |
25 |
Correct |
11 ms |
1428 KB |
Output is correct |
26 |
Correct |
4 ms |
1516 KB |
Output is correct |
27 |
Correct |
3 ms |
1516 KB |
Output is correct |
28 |
Correct |
3 ms |
1516 KB |
Output is correct |
29 |
Correct |
4 ms |
1516 KB |
Output is correct |
30 |
Correct |
4 ms |
1516 KB |
Output is correct |
31 |
Correct |
80 ms |
1820 KB |
Output is correct |
32 |
Correct |
5 ms |
1820 KB |
Output is correct |
33 |
Correct |
4 ms |
1820 KB |
Output is correct |
34 |
Correct |
7 ms |
1820 KB |
Output is correct |
35 |
Correct |
122 ms |
2172 KB |
Output is correct |
36 |
Correct |
91 ms |
2172 KB |
Output is correct |
37 |
Correct |
243 ms |
2172 KB |
Output is correct |
38 |
Correct |
18 ms |
2172 KB |
Output is correct |
39 |
Correct |
201 ms |
2172 KB |
Output is correct |
40 |
Correct |
79 ms |
2172 KB |
Output is correct |
41 |
Correct |
99 ms |
2172 KB |
Output is correct |
42 |
Correct |
71 ms |
2172 KB |
Output is correct |
43 |
Correct |
73 ms |
2172 KB |
Output is correct |
44 |
Correct |
136 ms |
2332 KB |
Output is correct |
45 |
Correct |
327 ms |
2332 KB |
Output is correct |
46 |
Correct |
496 ms |
2672 KB |
Output is correct |
47 |
Correct |
278 ms |
2672 KB |
Output is correct |
48 |
Correct |
279 ms |
2672 KB |
Output is correct |
49 |
Correct |
492 ms |
2672 KB |
Output is correct |
50 |
Correct |
529 ms |
2672 KB |
Output is correct |