QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#183236#6329. Colorful Graphneko_nyaa#WA 527ms17388kbC++234.6kb2023-09-19 12:13:352023-09-19 12:13:35

Judging History

你现在查看的是最新测评结果

  • [2023-09-19 12:13:35]
  • 评测
  • 测评结果:WA
  • 用时:527ms
  • 内存:17388kb
  • [2023-09-19 12:13:35]
  • 提交

answer

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

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int MX = 7000;
typedef bitset<MX> BS;

vi val, comp, z, cont;
int Time, ncomps;
template<class G, class F> int dfss(int j, G& g, F& f) {
	int low = val[j] = ++Time, x; z.push_back(j);
	for (auto e : g[j]) if (comp[e] < 0)
		low = min(low, val[e] ?: dfss(e,g,f));

	if (low == val[j]) {
		do {
			x = z.back(); z.pop_back();
			comp[x] = ncomps;
			cont.push_back(x);
		} while (x != j);
		f(cont); cont.clear();
		ncomps++;
	}
	return val[j] = low;
}
template<class G, class F> void sccd(G& g, F f) {
	int n = sz(g);
	val.assign(n, 0); comp.assign(n, -1);
	Time = ncomps = 0;
	rep(i,0,n) if (comp[i] < 0) dfss(i, g, f);
}

bool dfsh(int a, int L, vector<BS>& g, vi& btoa, vi& A, vi& B) {
	if (A[a] != L) return 0;
	A[a] = -1;
	for (int b = 0; b < MX; b++) {
		if (!g[a][b]) continue;
		if (B[b] == L + 1) {
			B[b] = 0;
			if (btoa[b] == -1 || dfsh(btoa[b], L + 1, g, btoa, A, B))
				return btoa[b] = a, 1;
		}
	}
	return 0;
}

int hopcroftKarp(vector<BS>& g, vi& btoa) {
	int res = 0;
	vi A(g.size()), B(btoa.size()), cur, next;
	for (;;) {
		fill(all(A), 0);
		fill(all(B), 0);
		/// Find the starting nodes for BFS (i.e. layer 0).
		cur.clear();
		for (int a : btoa) if(a != -1) A[a] = -1;
		rep(a,0,sz(g)) if(A[a] == 0) cur.push_back(a);
		/// Find all layers using bfs.
		for (int lay = 1;; lay++) {
			bool islast = 0;
			next.clear();
			for (int a : cur) for (int b = 0; b < MX; b++) {
				if (g[a][b]) {
					if (btoa[b] == -1) {
						B[b] = lay;
						islast = 1;
					}
					else if (btoa[b] != a && !B[b]) {
						B[b] = lay;
						next.push_back(btoa[b]);
					}
				}
			} 
			if (islast) break;
			if (next.empty()) return res;
			for (int a : next) A[a] = lay;
			cur.swap(next);
		}
		/// Use DFS to scan for augmenting paths.
		rep(a,0,sz(g))
			res += dfsh(a, 0, g, btoa, A, B);
	}
}

void dfs1(int now, int st, BS &vis, vector<vector<int>> &g, vector<int> &outdeg, vector<BS> &gg) {
	vis[now] = 1;
	if (outdeg[now] == 0) {
		if (now != st) {
			gg[st][now] = 1;
		}
		return;
	}

	for (int u: g[now]) {
		if (!vis[u]) {
			dfs1(u, st, vis, g, outdeg, gg);
		}
	}
}

void dfs2(int now, BS &vis, vector<vector<int>> &g) {
	vis[now] = 1;
	for (int u: g[now]) {
		if (!vis[u]) {
			dfs2(u, vis, g);
		}
	}
}

signed main() {
	ios::sync_with_stdio(0); cin.tie(0);

	int n, m; cin >> n >> m;
	vector<vector<int>> g1(n);
	vector<pair<int, int>> e;
	for (int i = 0; i < m; i++) {
		int x, y; cin >> x >> y; x--, y--;
		g1[x].push_back(y);
		e.emplace_back(x, y);
	}	

	int petr = 0;
	vector<int> scc(n);
	sccd(g1, [&](vi &v) {
		for (int i: v) {
			scc[i] = petr;
		}
		petr++;
	});

	vector<vector<int>> g2(petr);
	vector<int> indeg(petr), outdeg(petr);
	set<pair<int, int>> e2;
	for (auto [x, y]: e) {
		int u = scc[x], v = scc[y];
		if (u != v) {
			if (e2.count({u, v})) continue;
			g2[u].push_back(v);
			e2.insert({u, v});
			indeg[v]++; outdeg[u]++;
		}
	}

	vector<BS> g3(petr);
	for (int i = 0; i < petr; i++) {
		if (indeg[i] == 0) {
			BS vis;
			dfs1(i, i, vis, g2, outdeg, g3);
		}
	}

	vector<BS> reachable(petr);
	for (int i = 0; i < petr; i++) {
		dfs2(i, reachable[i], g2);
	}

	vi btoa(petr, -1); hopcroftKarp(g3, btoa);
	vector<int> colscc(petr); int petrc = 1;

	vector<pair<int, int>> matches;
	vector<int> solo;

	for (int i = 0; i < petr; i++) {
		if (indeg[i] != 0 && outdeg[i] == 0) {
			if (btoa[i] != -1) {
				colscc[i] = petrc;
				colscc[btoa[i]] = petrc;
				matches.emplace_back(btoa[i], i);
			} else {
				colscc[i] = petrc;
				solo.push_back(i);
			}
			petrc++;
		}
	}
	for (int i = 0; i < petr; i++) {
		if (outdeg[i] != 0 && indeg[i] == 0) {
			if (colscc[i] == 0) {
				colscc[i] = petrc;
				solo.push_back(i);
				petrc++;
			}
		}
	}

	for (int i = 0; i < petr; i++) {
		if (colscc[i] == 0) {
			for (auto [a, b]: matches) {
				if (reachable[a][i] && reachable[i][b]) {
					colscc[i] = colscc[a];
					break;
				}
			}
		}
	}
	for (int i = 0; i < petr; i++) {
		if (colscc[i] == 0) {
			for (int u: solo) {
				if (reachable[u][i] || reachable[i][u]) {
					colscc[i] = colscc[u];
					break;
				}
			}
		}
	}
	for (int i = 0; i < petr; i++) {
		if (colscc[i] == 0) {
			colscc[i] = petrc;
			petrc++;
		}
	}

	for (int i = 0; i < n; i++) {
		cout << colscc[scc[i]] << ' ';
	}
	cout << '\n';
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

1 1 2 1 1 

result:

ok AC

Test #2:

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

input:

5 7
1 2
2 1
4 3
5 1
5 4
4 1
4 5

output:

1 1 2 1 1 

result:

ok AC

Test #3:

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

input:

8 6
6 1
3 4
3 6
2 3
4 1
6 4

output:

1 1 1 1 2 1 3 4 

result:

ok AC

Test #4:

score: 0
Accepted
time: 337ms
memory: 16856kb

input:

7000 6999
4365 4296
2980 3141
6820 4995
4781 24
2416 5844
2940 2675
3293 2163
3853 5356
262 6706
1985 1497
5241 3803
353 1624
5838 4708
5452 3019
2029 6161
3849 4219
1095 1453
4268 4567
1184 1857
2911 3977
1662 2751
6353 6496
2002 6628
1407 4623
425 1331
4445 4277
1259 3165
4994 1044
2756 5788
5496 ...

output:

1 107 414 203 3 2 3 377 655 1505 1 1 4 1 232 5 516 6 702 25 498 585 2 253 102 7 8 6 334 9 1171 566 10 11 385 385 286 12 2 13 1525 614 14 534 15 687 16 2 17 18 879 19 1292 3 122 187 1 20 1031 21 575 3 3 2 158 22 2 2 243 23 24 710 25 981 26 1179 6 570 932 2 2 2 427 443 1 451 610 751 603 2 309 6 122 30...

result:

ok AC

Test #5:

score: 0
Accepted
time: 215ms
memory: 16836kb

input:

7000 6999
4832 1603
5984 6985
5355 3687
6007 2170
5984 3486
3267 2189
538 2123
4343 4553
5855 6168
5984 257
4239 2304
5984 2063
3298 1869
5984 6353
5984 2018
5984 5387
5984 3382
3164 3978
2690 2816
4810 2638
5984 3773
5984 1634
5984 2786
5984 3671
5984 5140
2943 5721
5984 414
1105 4060
3093 796
5984...

output:

7 7 618 465 1723 7 886 671 2 40 39 3 3 1274 2 1 3 21 7 2330 2 1713 2095 1232 7 7 848 1896 782 1568 1664 7 3 7 3 4 1371 3 117 135 5 237 1187 6 1846 7 21 8 9 1573 18 1533 2303 204 1811 3 10 11 12 13 2 7 14 39 805 1558 3 617 7 2264 15 3 2 2 16 17 3 3 1607 7 1804 884 18 1891 21 2 670 7 19 39 416 1341 2 ...

result:

ok AC

Test #6:

score: 0
Accepted
time: 309ms
memory: 16788kb

input:

7000 6999
1649 5337
1701 3344
4394 2172
3330 39
5932 1141
5381 5340
5453 3300
125 2172
6810 5263
804 2172
6635 2172
676 4740
3015 1183
1710 5769
611 5915
3419 1581
2094 2172
4508 2172
6604 2433
6113 1466
1604 696
1518 1123
1287 2940
4825 2172
5130 4524
2693 2172
106 2172
5157 2172
3693 2172
5198 217...

output:

1 2 464 319 103 3 165 1084 1720 4 1148 5 6 902 632 529 7 385 1158 1690 701 8 9 1446 551 266 731 413 10 11 1792 12 1334 752 1410 13 411 421 94 14 237 2073 1741 15 385 1175 685 303 16 45 377 17 1672 1538 694 323 323 18 583 5 19 62 20 2183 1827 1894 221 813 21 22 23 24 25 1078 460 1211 26 956 1014 27 8...

result:

ok AC

Test #7:

score: 0
Accepted
time: 329ms
memory: 16768kb

input:

7000 6999
2896 6321
881 2623
5058 2623
4833 2623
4669 2623
4781 5007
1447 2623
4781 4768
4781 3834
2758 4792
797 5055
3784 2623
4781 5510
6606 3040
597 3459
4136 2037
1291 3989
4781 837
4781 4379
5637 2053
1642 2665
4781 4664
4781 952
4924 2511
4781 4201
4781 2352
4781 5362
3901 197
137 2623
2706 19...

output:

1 1 1 1 1 1 1 2 354 3 1 1 1 1 1 1443 1548 1 1 4 1345 1 795 607 1367 1504 1428 1007 5 6 1 7 1 1 1 1 1536 226 1 699 1 1 879 1 8 1 9 1 10 1 1 1 11 12 1 13 1 1 783 1 14 1 1 113 1 15 272 1499 1 1743 1 1 1 16 1 1 1 1 1 1 1 1017 1 17 1052 1145 18 1 1 1 19 1523 1510 20 1 1 1 1 21 22 23 1 1 476 24 1513 1 1 2...

result:

ok AC

Test #8:

score: 0
Accepted
time: 151ms
memory: 16608kb

input:

6999 6998
1269 3969
1269 2429
1269 2609
1269 2515
1269 6166
1269 6614
3108 1269
2105 1269
4670 1269
578 1269
4661 1269
1421 1269
2576 1269
6152 1269
1269 6636
3011 1269
305 1269
5189 1269
1683 1269
6861 1269
1269 5798
1499 1269
282 1269
914 1269
80 1269
677 1269
701 1269
1269 359
6521 1269
1269 1754...

output:

1 2 3 4 1073 5 6 2667 1045 3245 3304 2946 7 434 8 9 10 976 11 12 125 13 14 2692 15 16 17 18 19 1675 20 1067 21 22 3194 23 1614 459 24 515 971 25 26 1204 3088 27 28 29 30 31 2522 1552 3493 32 263 2395 33 34 35 36 37 577 20 38 3396 2833 3337 39 40 41 525 42 730 1745 2103 43 44 2792 45 46 2095 47 2380 ...

result:

ok AC

Test #9:

score: 0
Accepted
time: 35ms
memory: 15880kb

input:

7000 0

output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 ...

result:

ok AC

Test #10:

score: 0
Accepted
time: 316ms
memory: 17388kb

input:

7000 6999
3138 1903
3285 5919
6182 1430
1164 961
1577 6445
1390 3384
935 5723
6614 6387
4799 2877
3915 5128
5366 5455
2287 3941
2053 2326
4022 6993
488 2922
4327 4701
4674 3221
1666 4773
4356 3232
3888 937
4318 6942
577 1299
4491 1938
5154 1254
790 5532
4286 5478
2918 6725
2853 304
2554 5207
5140 77...

output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

result:

ok AC

Test #11:

score: 0
Accepted
time: 70ms
memory: 9792kb

input:

7000 6999
33 3147
5877 4807
3116 4168
1651 2456
624 1740
6440 3058
6414 489
1023 2523
706 93
5523 598
4211 6063
3570 6840
6566 2971
6614 1907
5893 4389
4022 2527
5096 2345
4682 2134
188 5597
695 4285
1344 3832
3534 879
6574 6252
3759 3444
2167 85
5630 6600
3158 4404
6389 689
4871 6719
4295 6008
3437...

output:

7 7 1 1 7 7 1 3 1 1 1 1 1 1 1 3 1 7 1 1 7 1 7 1 1 1 1 1 1 1 1 1 2 1 1 3 7 7 1 1 1 7 7 1 3 1 7 1 1 3 1 1 1 1 1 3 1 1 1 48 7 7 7 1 1 1 1 7 7 1 3 7 1 1 7 1 7 7 7 1 7 7 1 1 3 3 1 1 7 1 7 1 1 1 1 7 4 1 3 1 1 1 1 7 1 1 7 1 1 7 1 1 1 7 7 1 7 1 1 1 1 7 7 1 7 1 1 1 1 48 1 7 1 1 3 7 1 3 3 3 1 1 3 1 1 1 1 7 1 ...

result:

ok AC

Test #12:

score: 0
Accepted
time: 55ms
memory: 8676kb

input:

7000 6999
1247 5150
3318 2013
5686 1615
6145 6521
5717 94
2787 3443
2648 4875
5332 5934
1897 1651
4640 2183
1750 6964
148 5228
745 2814
474 1165
496 6735
180 3412
2723 3374
6200 4361
497 5328
1928 5998
5648 1261
5090 4723
1715 706
2499 897
6569 6204
6039 2787
2882 5044
5767 4256
975 1877
1857 4453
6...

output:

1 1 1 1 1 1 4 1 218 1 16 1 74 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 20 2 1 1 1 1 1 1 4 1 39 1 1 1 16 1 1 1 1 1 1 1 1 3 1 1 1 1 1 4 16 1 1 4 1 1 1 1 1 1 20 20 1 267 20 1 1 1 1 1 4 1 1 1 1 1 1 1 1 1 1 1 20 1 1 1 1 1 1 1 1 1 1 1 1 20 1 1 1 16 1 1 1 1 1 20 1 1 1 1 20 1 1 20 1 1 1 1 1 39 1 1 1 1 1 1 1 1 1 1 5 1 ...

result:

ok AC

Test #13:

score: 0
Accepted
time: 149ms
memory: 13028kb

input:

7000 6999
2349 199
5295 2831
6143 2006
3212 3198
6956 3807
732 4838
5069 1027
5744 3479
6 5301
5687 4452
4201 1151
1353 4884
548 3506
6094 4799
4950 6939
5234 817
652 1314
979 6984
5771 1851
398 1322
2294 4298
847 3929
6833 183
2904 6745
4797 3874
94 315
4282 582
6591 5037
962 147
799 908
2593 5547
...

output:

1 1 2 1 1 2 2 4 57 2 2 1 2 8 2 1 8 2 2 8 2 1 1 3 2 1 1 4 2 1 4 2 2 2 1 2 1 2 2 2 2 1 2 8 1 5 2 1 1 6 2 2 7 8 2 9 2 4 10 2 1 1 2 1 1 2 2 4 2 8 1 1 1 1 2 1 2 1 8 2 1 1 1 2 11 2 1 2 1 2 2 2 1 12 2 1 2 2 2 1 2 1 2 2 2 8 1 1 1 1 1 2 2 13 14 2 2 2 4 1 2 2 2 2 2 1 1 1 1 2 2 2 15 16 1 1 17 1 2 517 2 4 1 2 2...

result:

ok AC

Test #14:

score: 0
Accepted
time: 191ms
memory: 15376kb

input:

7000 6999
3409 1629
2076 6412
4997 1078
6320 626
4501 1104
4173 1774
5507 2375
2299 5115
4321 127
1192 6635
1909 3398
2972 499
862 5024
421 2931
861 1536
902 3813
659 4514
1843 3035
3669 1228
1724 1880
34 706
133 3468
6116 585
5073 1461
5667 3405
715 4834
6915 3007
1736 6108
3264 2870
2393 6474
2108...

output:

16 1 1 4 1 2 1 1 1 3 5 4 1 5 6 1 1 1 1 1 1 7 1 1 8 1 9 10 11 12 1 13 1 14 16 1 1 1470 15 5 4 1322 296 16 1 1 16 509 16 4 620 1 1 1 1 2452 5 10 17 18 1 1 1 19 1 20 1 21 4 22 732 1 23 1468 16 24 1 1 25 26 2043 27 36 16 1 28 1 29 1 1 2 5 1 30 31 32 1 33 10 1 1 1 1 34 145 1 1 35 36 37 38 1 39 40 41 1 14...

result:

ok AC

Test #15:

score: -100
Wrong Answer
time: 527ms
memory: 16640kb

input:

7000 7000
2048 5882
6801 2408
3225 2608
1441 5079
497 6253
557 5589
2535 6257
4800 2595
4713 1286
4759 6636
4303 4296
6195 2048
6994 2987
1249 3044
1036 10
6472 2076
1996 1086
1279 1486
6100 369
4797 3437
2493 4576
2944 5601
197 5582
5488 5035
4023 659
2651 5024
2257 5710
1001 3941
446 4815
687 702
...

output:

1785 1786 2 1789 1790 11 22 1795 1796 24 25 27 28 40 65 1809 153 1830 177 29 178 179 1831 182 183 1841 197 202 203 204 202 227 85 1861 191 262 269 1863 270 1864 271 1866 1867 234 273 274 1868 275 92 276 1869 186 49 167 1870 186 1871 19 284 136 1874 1857 291 292 294 94 233 102 299 300 307 275 290 187...

result:

wrong answer 2313 and 789 are not connected