QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#793929#8555. Dress to Impressalexz1205RE 427ms4992kbC++146.8kb2024-11-30 05:24:152024-11-30 05:24:15

Judging History

This is the latest submission verdict.

  • [2024-11-30 05:24:15]
  • Judged
  • Verdict: RE
  • Time: 427ms
  • Memory: 4992kb
  • [2024-11-30 05:24:15]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

const int N = 100, TOT = N+N+5;

int n, m, c, k;

int dfs(int i, int lim, int term, vector<int> *level, int *point, int *dist, map<int, int> *con){
	if (i == term){
		return lim;
	}
	if (point[i] == (int)level[i].size()){
		return 0;
	}
	int sum = 0;
	for (; point[i] < (int)level[i].size(); point[i] ++){
		while (con[i][level[i][point[i]]]){
			int v = dfs(level[i][point[i]], min(lim-sum, con[i][level[i][point[i]]]), term, level, point, dist, con);
			sum += v;
			con[i][level[i][point[i]]] -= v;
			con[level[i][point[i]]][i] += v;
			if (v == 0){
				break;
			}
			if (sum == lim){
				return lim;
			}
		}
	}
	return sum;
}

int dinuc(map<int, int> *con, int tot, int src, int term){
	vector<int> level[tot];
	int point[tot], dist[tot];

	memset(point, 0, sizeof(int) * tot);
	memset(dist, -1, sizeof(int) * tot);
	for (int x = 0; x < tot; x ++){
		level[x].clear();
	}

	queue<array<int, 2>> que;
	que.push({src, 0});
	while (!que.empty()){
		array<int, 2> cur = que.front();
		que.pop();
		if (dist[cur[0]] != -1){
			continue;
		}
		dist[cur[0]] = cur[1];
		if (cur[0] == term){
			break;
		}
		for (pair<int, int> e: con[cur[0]]){
			if (e.second > 0){
				que.push({e.first, cur[1]+1});
			}
		}
	}
	for (int x = 0; x < tot; x ++){
		for (pair<int, int> e: con[x]){
			if (e.second > 0 && dist[e.first] > dist[x]){
				level[x].push_back(e.first);
			}
		}
	}
	return dfs(src, 1e9, term, level, point, dist, con);
}

void reduce(set<array<int, 3>> &a, set<array<int, 3>> &b, int tot){
	if (a.size() > b.size()){
		swap(a, b);
	}

	if ((int)a.size() == k || (int)b.size() == k){
		return;
	}

	int mat[tot];
	int rev[tot];
	memset(mat, -1, sizeof(mat));
	memset(rev, -1, sizeof(mat));
	for (array<int, 3> e: a){
		mat[e[0]] = e[1];
		rev[e[1]] = e[0];
	}
	for (array<int, 3> e: b){
		mat[e[1]] = e[0];
		rev[e[0]] = e[1];
	}

	int i = 0;
	while (!((int)a.size() == k || (int)b.size() == k)){
		if (rev[i] == -1){
			int nex = i;
			while (mat[nex] != -1){
				nex = mat[nex];
				if (nex == i){
					break;
				}
			}
			if (nex != i && nex < n){
				while (nex != -1){
					array<int, 3> e1 = {min(mat[nex], nex), max(mat[nex], nex), 0};
					array<int, 3> e2 = {min(rev[nex], nex), max(rev[nex], nex), 0};
					if (nex < n){
						if (mat[nex] != -1) {
							a.erase(e1);
							b.insert(e1);
						}
						if (rev[nex] != -1) {
							b.erase(e2);
							a.insert(e2);
						}
					}else {
						if (mat[nex] != -1) {
							b.erase(e1);
							a.insert(e1);
						}
						if (rev[nex] != -1) {
							a.erase(e2);
							b.insert(e2);
						}
					}
					swap(mat[nex], rev[nex]);
					nex = mat[nex];
				}
			}
		}
		i ++;
		if (i == n){
			break;
		}
	}
}

int main(){
	scanf("%d %d %d %d", &n, &m, &c, &k);
	multiset<array<int, 3>> sig;
	multiset<array<int, 3>> truEdges;
	int lim = 1e9;
	int tot;
	int src, term;
	tot = n+m+2;
	src = n+m;
	term = n+m+1;
	int deg[tot];
	memset(deg, 0, sizeof(deg));
	int res = 1;
	{
		map<int, int> con[tot];
		int flow = 0;

		for (int x = 0; x < c; x ++){
			int a, b;
			scanf("%d %d", &a, &b);
			a --, b --;
			truEdges.insert({a, b+n, x});
			con[a][n+b] += 1;
			deg[a] ++;
			deg[n+b] ++;
		}
		for (int x = 0; x < n; x ++){
			lim = min(lim, deg[x]);
		}

		for (; res <= lim; res ++){
			for (int x = 0; x < n; x ++){
				con[src][x] += 1;
			}
			for (int x = 0; x < m; x ++){
				con[n+x][term] += 1;
			}
			while (true){
				int v = dinuc(con, tot, src, term);
				flow += v;

				if (v == 0) break;
			}
			if (flow < res*k){
				break;
			}
		}
		sig.clear();
		memset(deg, 0, sizeof(deg));
		for (array<int, 3> e: truEdges){
			if (con[e[1]][e[0]]){
				sig.insert(e);
				deg[e[0]] ++;
				deg[e[1]] ++;
				con[e[1]][e[0]] --;
			}
		}
		res --;
		printf("%d\n", res);
	}
	set<array<int, 3>> match[lim];
	for (int K = res; K > 0; K --){
		map<int, int> con1[tot];
		map<int, int> con2[tot];

		for (int x = 0; x < n; x ++){
			if (deg[x] == K) con1[src][x] = 1;
			con2[x][term] = 1;
		}
		for (int x = 0; x < m; x ++){
			con1[n+x][term] = 1;
			if (deg[n+x] == K) con2[src][n+x] = 1;
		}

		for (array<int, 3> e: sig){
			con1[e[0]][e[1]] ++;
			con2[e[1]][e[0]] ++;
		}

		int flow1 = 0, flow2 = 0;
		while (true){
			int v = dinuc(con1, tot, src, term);
			flow1 += v;

			if (v == 0) break;
		}
		while (true){
			int v = dinuc(con2, tot, src, term);
			flow2 += v;

			if (v == 0) break;
		}

		int mat[tot];
		int rev[tot];
		memset(mat, -1, sizeof(mat));
		memset(rev, -1, sizeof(mat));
		for (array<int, 3> e: sig){
			for (int x = 0; x < con1[e[1]][e[0]]; x ++){
				assert(deg[e[0]] == K);
				mat[e[0]] = e[1];
				rev[e[1]] = e[0];
			}
			for (int x = 0; x < con2[e[0]][e[1]]; x ++){
				assert(deg[e[1]] == K);
				mat[e[1]] = e[0];
				rev[e[0]] = e[1];
			}
		}
		for (int x = 0; x < n; x ++){
			if (deg[x] == K){
				assert(mat[x] != -1);
				if (rev[x] != -1){
					int i = rev[x];
					while (rev[i] != -1){
						i = rev[i];
						if (i == x){
							mat[rev[x]] = -1;
							break;
						}
					}
					while (i != -1 && mat[i] == -1){
						int nex = mat[mat[i]];
						mat[mat[i]] = i;
						i = nex;
					}
				}
			}
		}
		for (int x = 0; x < tot; x ++){
			if (deg[x] == K){
				array<int, 2> e = {min(x, mat[x]), max(x, mat[x])};
				auto it = truEdges.lower_bound({e[0], e[1], 0});
				assert(it != truEdges.end());
				array<int, 3> e2 = *it;
				match[K-1].insert(e2);
				truEdges.erase(e2);
				sig.erase(e2);
				deg[x] --;
				deg[mat[x]] --;
			}
		}
	}
	set<int> gr, le;
	for (int x = 0; x < res; x ++){
		if ((int)match[x].size() > k){
			gr.insert(x);
		}else if ((int)match[x].size() < k){
			le.insert(x);
		}
	}
	swap(gr, le);
	while (!gr.empty()){
		vector<int> rem2;
		for (int x: gr){
			vector<int> rem;
			for (int y: le){
				reduce(match[y], match[x], tot);
				if ((int)match[y].size() == k){
					rem.push_back(y);
				}
				if ((int)match[x].size() == k){
					rem2.push_back(x);
					break;
				}
			}
			for (int x: rem){
				le.erase(x);
			}
			if ((int)match[x].size() == k){
				rem2.push_back(x);
			}
		}
		for (int x: rem2){
			gr.erase(x);
		}
	}
	for (int x = 0; x < res; x ++){
		int setV[n];
		memset(setV, -1, sizeof(setV));
		for (array<int, 3> e: match[x]){
			setV[e[0]] = e[2];
		}
		for (int x = 0; x < n; x ++){
			if (setV[x] == -1){
				auto it = truEdges.lower_bound({x, -1, -1});
				while (!(it != truEdges.end())) std::this_thread::sleep_for(10s);
				truEdges.erase(it);
				assert((*it)[0] == x);
				setV[x] = (*it)[2];
			}
		}
		for (int x = 0; x < n; x ++){
			printf("%d ", setV[x] + 1);
		}
		printf("\n");
	}
}


詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3784kb

input:

3 3 10 2
3 1
3 3
3 2
2 3
2 2
2 2
3 3
2 1
1 3
1 3

output:

2
10 5 1 
9 8 3 

result:

ok 

Test #2:

score: 0
Accepted
time: 1ms
memory: 3808kb

input:

3 4 12 3
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
3 1
3 2
3 3
3 4

output:

4
4 7 10 
3 8 9 
2 5 12 
1 6 11 

result:

ok 

Test #3:

score: 0
Accepted
time: 0ms
memory: 3816kb

input:

5 5 20 5
5 2
3 5
3 2
3 3
4 5
4 4
5 5
5 2
1 5
3 4
5 3
3 5
2 2
5 5
3 1
1 1
5 4
4 1
1 2
2 2

output:

2
9 20 15 6 11 
16 13 4 5 17 

result:

ok 

Test #4:

score: 0
Accepted
time: 0ms
memory: 3828kb

input:

10 10 100 9
5 4
4 8
4 5
7 10
5 3
6 6
5 7
1 8
6 8
5 3
1 2
3 6
9 5
6 3
6 9
10 1
7 10
8 8
2 7
9 3
8 7
2 2
5 7
3 9
4 1
1 2
7 2
7 2
4 3
10 4
4 2
5 3
3 4
5 4
10 6
8 5
9 5
3 3
3 8
1 7
3 8
4 7
8 4
4 7
4 5
6 7
6 7
6 9
8 1
4 5
10 10
4 10
4 3
8 9
7 6
8 2
8 8
4 4
9 10
1 7
9 1
10 4
8 8
9 2
3 6
3 8
8 5
10 4
5 6
5...

output:

5
8 98 74 53 23 71 27 85 37 68 
40 22 24 2 10 6 17 49 13 62 
77 19 39 82 5 15 55 67 59 30 
26 100 33 29 7 9 4 36 75 16 
11 96 38 25 69 46 99 18 73 51 

result:

ok 

Test #5:

score: 0
Accepted
time: 13ms
memory: 4320kb

input:

10 100 1000 8
10 95
4 22
8 55
7 11
7 62
2 39
5 17
7 33
7 69
2 49
9 13
1 43
7 87
3 58
8 38
3 25
6 19
7 10
1 94
5 59
3 81
5 55
7 13
8 79
7 73
1 11
7 70
4 73
2 47
9 26
7 48
10 62
4 16
3 55
5 12
9 75
2 27
10 77
1 43
1 72
2 86
4 14
1 43
7 49
2 29
10 54
6 26
10 86
6 38
9 25
10 27
7 44
9 60
10 22
2 39
4 64...

output:

84
915 388 729 798 132 286 995 516 572 506 
866 830 369 744 615 638 793 218 547 99 
525 699 801 775 564 556 825 734 351 597 
58 604 322 225 297 122 13 24 179 761 
908 166 607 954 292 872 332 829 974 214 
528 588 515 683 188 112 73 759 599 408 
362 808 353 119 926 224 979 199 694 383 
943 637 585 690...

result:

ok 

Test #6:

score: 0
Accepted
time: 1ms
memory: 4048kb

input:

20 10 50 3
4 10
6 2
2 10
15 6
9 7
6 7
11 6
17 4
13 9
15 4
17 6
17 5
2 8
20 1
3 10
16 10
3 4
6 7
14 8
9 3
13 8
15 7
6 4
19 4
10 4
2 5
14 4
1 7
12 10
9 2
2 2
4 10
8 7
10 9
20 4
1 8
10 9
18 5
20 10
17 5
18 2
5 4
2 1
3 8
10 2
6 9
1 1
9 4
7 6
6 9

output:

1
47 31 17 1 42 6 49 33 20 34 7 29 21 27 10 16 12 41 24 14 

result:

ok 

Test #7:

score: 0
Accepted
time: 3ms
memory: 3876kb

input:

20 20 500 10
18 19
9 6
19 19
13 1
14 12
6 14
7 11
5 11
9 7
15 17
2 5
1 3
17 16
15 9
17 16
15 18
19 13
5 2
15 14
5 10
14 12
4 16
15 8
3 19
7 18
4 1
4 4
12 19
5 2
18 10
19 12
5 14
10 16
19 7
14 7
13 10
5 17
4 8
10 14
6 2
17 8
8 7
8 12
19 4
7 16
9 15
19 9
4 16
19 18
11 4
11 17
1 18
11 1
9 18
17 12
18 1...

output:

15
420 293 481 257 37 153 45 43 397 236 430 468 120 443 353 454 217 225 383 456 
52 119 401 38 20 408 231 465 180 381 457 441 100 35 446 244 185 406 391 467 
74 187 327 392 138 40 208 380 318 358 50 319 379 334 294 173 15 369 205 410 
160 78 395 174 32 355 191 389 139 326 368 336 221 335 348 487 359...

result:

ok 

Test #8:

score: 0
Accepted
time: 6ms
memory: 3940kb

input:

20 20 1000 19
10 14
19 4
9 15
16 12
10 4
16 9
1 18
3 4
15 11
7 7
4 16
15 20
8 12
7 4
6 19
10 15
20 12
7 11
18 9
1 14
20 1
17 1
5 17
15 13
15 18
6 8
20 8
17 17
1 7
7 20
17 18
17 17
4 17
1 10
12 15
9 1
5 17
14 6
3 13
4 4
5 5
7 1
19 4
9 8
4 14
5 19
2 5
5 13
17 11
11 20
9 16
3 4
16 10
1 15
3 13
11 14
4 ...

output:

32
882 809 71 345 720 930 582 186 3 881 533 222 540 875 876 922 926 488 569 418 
862 155 496 692 727 579 538 945 841 595 971 309 58 749 833 359 432 449 530 918 
492 320 398 467 443 245 485 940 451 391 849 194 108 252 139 879 404 532 495 572 
489 109 801 202 233 796 470 941 491 821 775 886 444 678 74...

result:

ok 

Test #9:

score: 0
Accepted
time: 68ms
memory: 4972kb

input:

100 100 5000 100
40 31
92 15
79 46
81 57
32 96
100 54
93 54
12 89
50 33
89 32
50 90
45 3
26 2
27 9
49 22
44 28
16 9
51 69
9 18
89 93
89 13
46 99
39 13
10 68
54 58
48 32
5 68
25 100
65 40
94 74
41 31
27 49
18 54
71 73
83 35
84 23
55 51
8 13
40 24
2 15
67 80
85 87
26 11
68 58
40 73
12 50
4 88
79 87
34...

output:

36
2801 559 2047 2816 1937 2507 2464 4793 4885 4997 4492 4827 1907 4847 1528 247 2956 3350 4675 4123 2420 4084 2074 775 204 1118 2581 2735 2223 1869 2937 2732 2942 1981 3807 4376 4735 712 1106 3861 1399 4399 2657 212 4769 1512 1839 3204 4533 2052 717 1155 2406 2562 3057 4671 1284 2855 2364 1043 3262...

result:

ok 

Test #10:

score: 0
Accepted
time: 45ms
memory: 4792kb

input:

100 100 5000 99
61 63
13 12
30 42
99 63
27 61
91 39
73 89
61 2
78 39
67 45
83 41
49 1
99 46
69 3
48 39
88 84
35 25
28 40
53 72
18 71
42 53
69 21
12 28
93 78
86 100
90 79
42 35
63 97
33 42
6 4
66 47
75 33
54 45
72 42
99 69
47 20
73 56
67 61
33 28
95 67
54 10
35 3
96 87
48 65
77 38
35 18
3 67
45 86
80...

output:

28
3579 2674 2864 2709 4035 2645 3616 889 1385 860 2932 4929 1845 3291 1144 311 172 2147 84 1258 2781 3057 509 3548 663 2694 2613 711 1004 1917 2965 4369 2834 542 1693 155 2155 2299 3347 3258 4383 1927 709 4044 4217 1149 2584 1215 3704 4968 1601 4573 220 2894 3522 4081 4759 3177 1260 3916 4829 4415 ...

result:

ok 

Test #11:

score: 0
Accepted
time: 75ms
memory: 4984kb

input:

100 100 5000 90
6 35
87 21
10 67
82 86
31 55
47 81
61 66
4 66
38 31
90 22
19 94
65 59
66 42
44 34
54 16
97 20
43 78
41 18
18 16
100 6
68 21
53 9
44 46
97 53
68 49
7 57
43 85
24 53
27 14
63 42
37 93
21 47
60 13
33 7
32 22
29 96
34 51
64 98
10 76
81 2
28 97
67 94
14 100
43 94
97 51
47 82
31 1
32 78
8 ...

output:

38
1182 4596 4738 3144 4183 4908 3155 431 3838 4246 3927 4854 4681 4958 803 1509 346 744 4819 2438 1854 2097 1419 285 2698 2971 2351 4420 1428 4119 4522 4557 2841 3939 314 4703 1004 3421 850 1816 2527 2617 1029 4803 4764 2242 4125 4017 4422 4694 3614 920 1566 4910 3005 4600 1807 4761 4622 3953 4229 ...

result:

ok 

Test #12:

score: 0
Accepted
time: 66ms
memory: 4992kb

input:

100 100 5000 50
44 20
77 81
42 12
83 24
60 27
73 2
31 62
68 29
56 63
93 86
47 25
70 71
5 79
34 35
96 63
100 88
65 86
62 72
40 32
39 53
31 49
14 5
21 52
17 63
85 10
13 22
69 62
35 33
54 29
68 14
74 80
67 11
63 65
9 88
36 77
98 20
65 6
18 21
93 68
63 29
32 39
86 86
38 30
51 3
57 41
93 91
76 46
83 38
2...

output:

36
4104 3893 2793 1997 4135 4786 1654 3557 1228 2374 213 1620 1822 85 1248 3353 3556 4986 3766 4636 3999 101 1262 3919 564 3733 1699 1039 2798 669 4914 2591 1275 2326 2058 1740 4571 4900 1950 3361 1107 975 1078 4452 4009 3957 2448 4958 738 4641 696 4700 3292 645 612 4946 2731 4578 3065 3584 4841 293...

result:

ok 

Test #13:

score: 0
Accepted
time: 427ms
memory: 4680kb

input:

1 100 5000 1
1 60
1 46
1 43
1 42
1 53
1 69
1 15
1 42
1 6
1 18
1 50
1 97
1 8
1 68
1 76
1 79
1 27
1 72
1 4
1 64
1 96
1 26
1 14
1 51
1 10
1 68
1 20
1 52
1 41
1 76
1 18
1 50
1 57
1 8
1 35
1 9
1 84
1 62
1 93
1 40
1 63
1 91
1 11
1 38
1 96
1 79
1 43
1 43
1 83
1 78
1 60
1 25
1 55
1 43
1 10
1 45
1 97
1 11
1 ...

output:

5000
4937 
4885 
4641 
4577 
4517 
4332 
4291 
4158 
3944 
3378 
3161 
3147 
3004 
2981 
2977 
2870 
2827 
2780 
2370 
2291 
2248 
2092 
2036 
1787 
1606 
1577 
1541 
1471 
1416 
1412 
1353 
1342 
997 
665 
657 
557 
514 
355 
305 
287 
266 
134 
4938 
4890 
4876 
4871 
4326 
4099 
4093 
4038 
3935 ...

result:

ok 

Test #14:

score: 0
Accepted
time: 3ms
memory: 4228kb

input:

100 1 5000 1
78 1
44 1
1 1
7 1
1 1
47 1
52 1
49 1
95 1
87 1
11 1
16 1
90 1
9 1
15 1
16 1
56 1
79 1
94 1
30 1
57 1
56 1
45 1
89 1
16 1
22 1
20 1
75 1
47 1
32 1
51 1
80 1
83 1
34 1
21 1
96 1
17 1
88 1
99 1
30 1
41 1
14 1
82 1
25 1
58 1
78 1
72 1
78 1
81 1
37 1
86 1
28 1
99 1
8 1
48 1
64 1
51 1
85 1
63...

output:

36
2813 121 325 75 86 246 4 54 14 143 11 206 216 42 15 12 37 223 222 27 35 26 376 67 44 289 104 52 85 20 162 30 90 34 87 62 50 127 116 133 41 131 124 2 23 161 6 55 8 186 31 7 78 140 149 17 21 45 79 125 97 74 59 56 100 135 283 150 367 142 72 47 107 233 28 153 95 1 18 32 49 43 33 220 58 51 10 38 24 13...

result:

ok 

Test #15:

score: 0
Accepted
time: 194ms
memory: 4500kb

input:

2 50 5000 2
1 10
2 19
1 22
1 19
1 8
2 21
1 2
2 14
1 38
2 36
1 23
2 41
2 40
1 44
2 24
1 50
2 30
2 3
2 22
1 26
1 9
2 13
1 16
2 48
1 17
2 18
1 5
1 12
1 1
2 26
1 27
1 50
2 27
1 12
1 35
2 23
1 4
1 26
1 36
2 31
2 32
1 21
2 49
1 37
2 46
2 42
1 22
1 13
2 35
2 49
2 16
1 27
2 18
2 37
2 42
2 34
1 15
2 2
1 21
1...

output:

2466
4865 3048 
4858 3024 
4744 2246 
4615 1924 
4449 1916 
4397 1856 
4199 1831 
4080 1806 
3930 1686 
3905 1626 
3902 1507 
3843 1505 
3703 1504 
3508 1456 
3458 1314 
3423 1219 
3383 1131 
3275 976 
3189 871 
3033 811 
2818 805 
2747 788 
2743 561 
2704 249 
2590 94 
2338 50 
2239 43 
2161 4877 
...

result:

ok 

Test #16:

score: -100
Runtime Error

input:

100 100 5000 1
28 34
13 11
4 95
1 40
45 67
91 36
6 89
36 33
1 32
27 76
55 84
3 78
61 99
67 85
45 8
5 61
50 50
93 39
26 77
54 60
73 67
33 71
66 15
96 32
45 73
100 68
36 100
8 68
63 10
74 4
94 60
54 75
85 60
78 19
33 14
59 90
79 100
86 60
2 93
64 19
52 61
80 36
31 45
12 73
7 83
98 83
82 16
18 31
68 20...

output:

37
3180 3800 4656 4166 4001 2839 3753 2782 1378 2399 2005 2700 4754 4909 1335 1859 772 3140 2845 733 1958 557 4306 4563 1164 3382 1940 3496 2250 2736 2356 448 916 1097 3693 2012 3325 1629 3631 3643 3556 3154 4801 483 2958 3668 3337 429 4785 2698 4724 1498 4556 184 2649 4870 701 498 1769 1514 3107 45...

result: