답안 #341266

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
341266 2020-12-29T10:26:53 Z juggernaut 결혼 문제 (IZhO14_marriage) C++14
100 / 100
440 ms 3504 KB
#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x)  cerr<< #x <<" = "<< x<<endl;
using namespace std;
 
typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int vis[MAXN],l,r=1,ver;
vector<int>adj[MAXN];
int match[MAXN];
bool dfs(int nd){
	if(vis[nd]==ver)return 0;
	vis[nd]=ver;
	tr(it,adj[nd])
		if(l<=*it and *it<=r)
			if(match[*it]==-1 or dfs(match[*it])){
				match[*it]=nd;
				return 1;	
			}	
	return 0;
}
int main(){
    //freopen("file.in", "r", stdin);
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=k;i++){
    	int a,b;
		scanf("%d%d",&a,&b);
		adj[b].pb(a);	
    }
    for(int i=1;i<=m;i++)sort(all(adj[i]));
    vector<int>need;
    for(int i=1;i<=m;i++)
    	need.pb(i);
    for(int i=1;i<=n;i++)
    	match[i]=-1;
    ll ans=0;
    for(l=1;max(r,l)<=n;l++){
    	while(!need.empty()){
			int nd=need.back();ver++;
			while(r<=n and !dfs(nd))
				r++,ver++;
			if(r>n)
				break;
			need.ppb();
		}
		ans+=n-r+1;
		if(~match[l])
			need.pb(match[l]),match[l]=-1;
    }
    printf("%lld\n",ans);
	return 0;
}

Compilation message

marriage.cpp: In function 'int main()':
marriage.cpp:37:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   37 |     scanf("%d%d%d",&n,&m,&k);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~
marriage.cpp:40:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   40 |   scanf("%d%d",&a,&b);
      |   ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 2 ms 2668 KB Output is correct
8 Correct 2 ms 2668 KB Output is correct
9 Correct 2 ms 2668 KB Output is correct
10 Correct 2 ms 2668 KB Output is correct
11 Correct 2 ms 2668 KB Output is correct
12 Correct 2 ms 2668 KB Output is correct
13 Correct 2 ms 2668 KB Output is correct
14 Correct 2 ms 2668 KB Output is correct
15 Correct 2 ms 2668 KB Output is correct
16 Correct 2 ms 2668 KB Output is correct
17 Correct 2 ms 2668 KB Output is correct
18 Correct 2 ms 2668 KB Output is correct
19 Correct 3 ms 2668 KB Output is correct
20 Correct 3 ms 2668 KB Output is correct
21 Correct 2 ms 2816 KB Output is correct
22 Correct 2 ms 2668 KB Output is correct
23 Correct 2 ms 2668 KB Output is correct
24 Correct 2 ms 2668 KB Output is correct
25 Correct 21 ms 2796 KB Output is correct
26 Correct 5 ms 2668 KB Output is correct
27 Correct 2 ms 2668 KB Output is correct
28 Correct 2 ms 2668 KB Output is correct
29 Correct 4 ms 2668 KB Output is correct
30 Correct 4 ms 2668 KB Output is correct
31 Correct 149 ms 3052 KB Output is correct
32 Correct 5 ms 2796 KB Output is correct
33 Correct 2 ms 2668 KB Output is correct
34 Correct 3 ms 2668 KB Output is correct
35 Correct 113 ms 3180 KB Output is correct
36 Correct 88 ms 3180 KB Output is correct
37 Correct 440 ms 3052 KB Output is correct
38 Correct 23 ms 3180 KB Output is correct
39 Correct 46 ms 2796 KB Output is correct
40 Correct 5 ms 2796 KB Output is correct
41 Correct 11 ms 2924 KB Output is correct
42 Correct 11 ms 2924 KB Output is correct
43 Correct 17 ms 3052 KB Output is correct
44 Correct 22 ms 3308 KB Output is correct
45 Correct 12 ms 3052 KB Output is correct
46 Correct 226 ms 3436 KB Output is correct
47 Correct 29 ms 3436 KB Output is correct
48 Correct 23 ms 3308 KB Output is correct
49 Correct 332 ms 3504 KB Output is correct
50 Correct 158 ms 3052 KB Output is correct