QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#426364#6329. Colorful Graphzyz07TL 2386ms17696kbC++171.8kb2024-05-31 09:01:082024-05-31 09:01:08

Judging History

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

  • [2024-05-31 09:01:08]
  • 评测
  • 测评结果:TL
  • 用时:2386ms
  • 内存:17696kb
  • [2024-05-31 09:01:08]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define For(Ti,Ta,Tb) for(auto Ti=(Ta);Ti<=(Tb);++Ti)
#define Dec(Ti,Ta,Tb) for(auto Ti=(Ta);Ti>=(Tb);--Ti)
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
using ll=long long;
const int N=7005;
int n,m,tot,bel[N],mth[N],ans[N];
vector<int> g[N],scc[N],g2[N];
bitset<N> bs[N],nvis;
struct{
	int dfn[N],low[N],dfx,stk[N],top,vis[N];
	void tarjan(int u){
		low[u]=dfn[u]=++dfx;
		stk[++top]=u;
		vis[u]=1;
		for(int v:g[u]){
			if(!dfn[v]){
				tarjan(v);
				low[u]=min(low[u],low[v]);
			}else if(vis[v]){
				low[u]=min(low[u],dfn[v]);
			}
		}
		if(dfn[u]==low[u]){
			++tot;
			for(int v=0;v!=u;){
				v=stk[top--];
				bel[v]=tot;
				scc[tot].push_back(v);
				vis[v]=0;
			}
		}
	}
	void operator()(){
		For(u,1,n){
			if(!dfn[u]){
				tarjan(u);
			}
		}
	}
}tarjan;
bool dfs(int u){
	auto nxt=nvis&bs[u];
	for(int v=nxt._Find_first();v<=tot;v=nxt._Find_next(v)){
		nvis.set(v,0);
		if(!mth[v]||dfs(mth[v])){
			mth[v]=u;
			return 1;
		}
	}
	return 0;
}
void color(int u,int c){
	for(int v:scc[u]){
		ans[v]=c;
	}
	for(int v:g2[u]){
		if(!ans[scc[v][0]]){
			color(v,c);
		}
	}
}
int main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	cin>>n>>m;
	For(i,1,m){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
	}
	tarjan();
	For(u,1,tot){
		bs[u].set(u);
		for(int v:scc[u]){
			for(int x:g[v]){
				bs[u]|=bs[bel[x]];
			}
		}
	}
	For(u,1,tot){
		bs[u].set(u,0);
	}
	Dec(u,tot,1){
		nvis.set();
		dfs(u);
	}
	For(u,1,tot){
		if(mth[u]){
			g2[u].push_back(mth[u]);
			g2[mth[u]].push_back(u);
		}
	}
	int cur=0;
	For(u,1,tot){
		if(!ans[scc[u][0]]){
			color(u,++cur);
		}
	}
	For(u,1,n){
		cout<<ans[u]<<" \n"[u==n];
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 5944kb

input:

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

output:

1 1 2 1 2

result:

ok AC

Test #2:

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

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: 1ms
memory: 6096kb

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: 1289ms
memory: 14864kb

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:

873 107 414 655 1585 809 1665 377 961 1505 1272 1196 1147 506 666 62 516 1732 886 25 500 585 1366 255 1146 602 36 917 334 1167 1171 566 34 35 728 1043 286 1733 421 33 1525 614 885 534 31 687 1734 1035 32 1735 881 1168 1292 971 970 187 891 791 1031 29 783 1614 1141 1457 158 30 1479 948 245 27 28 710 ...

result:

ok AC

Test #5:

score: 0
Accepted
time: 1004ms
memory: 13988kb

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:

1748 1313 618 465 1723 1207 886 671 230 2244 2156 554 672 1274 68 2245 1004 1968 1746 2330 2246 1713 2095 1232 1305 1884 848 1896 782 1568 1664 1787 944 1245 2247 2248 1371 819 117 135 2249 237 1187 2250 1846 2251 21 2252 2253 1573 18 1533 2303 204 1811 885 2254 2255 2256 2257 436 1639 2258 2154 805...

result:

ok AC

Test #6:

score: 0
Accepted
time: 1039ms
memory: 15452kb

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:

1224 803 464 319 1300 1053 165 1084 1720 801 1164 802 799 902 986 529 800 385 1158 1690 701 1570 798 1446 1397 266 731 413 1312 796 1792 797 1334 1033 1410 1571 1443 421 94 795 864 2073 1741 1177 386 1175 685 303 1572 47 377 794 1672 1538 1351 323 323 1573 583 5 1052 1304 1574 2183 1827 1894 221 813...

result:

ok AC

Test #7:

score: 0
Accepted
time: 1281ms
memory: 15988kb

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 345 87 1728 250 71 229 2 354 3 175 1596 1695 1039 404 1443 1548 1720 879 4 1345 531 795 607 1367 1504 1428 1007 5 6 727 7 866 1518 190 317 1536 226 1373 699 638 1469 879 826 8 298 9 476 10 397 1646 563 11 12 781 13 1495 1042 783 944 14 78 764 113 959 15 272 1499 1215 1743 325 1572 598 16 1734 1350...

result:

ok AC

Test #8:

score: 0
Accepted
time: 545ms
memory: 14232kb

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:

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

result:

ok AC

Test #9:

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

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: 2386ms
memory: 17696kb

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: 413ms
memory: 11052kb

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:

25 6 1 1 25 24 1 16 1 1 1 1 1 35 1 2 1 26 1 1 26 1 26 1 1 1 1 1 1 1 1 1 36 1 1 7 6 6 1 1 1 32 18 1 3 1 6 1 1 19 1 1 1 1 1 27 1 1 1 6 10 6 2 1 1 1 1 21 6 1 26 26 1 1 15 1 15 26 29 1 25 14 1 1 8 5 1 1 20 1 2 1 1 1 1 31 37 1 33 1 1 1 1 2 1 1 25 1 1 6 1 1 1 28 2 1 24 1 1 1 1 29 18 1 24 1 1 1 1 6 1 26 1 ...

result:

ok AC

Test #12:

score: 0
Accepted
time: 684ms
memory: 8712kb

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 69 1 64 1 1 64 1 7 1 18 23 7 1 1 37 1 1 1 1 1 1 1 1 1 1 55 1 61 84 1 1 1 1 1 1 57 1 7 1 1 1 44 1 1 42 1 1 1 75 68 85 1 1 1 1 1 15 41 1 67 16 1 1 1 36 1 1 78 75 1 56 16 1 1 1 1 1 22 68 1 1 1 1 1 10 1 1 1 1 8 1 1 1 65 1 1 1 1 1 79 1 1 29 26 1 1 56 1 38 53 1 1 74 1 31 1 1 78 1 1 78 1 25 1 1 1 8 1 1 7...

result:

ok AC

Test #13:

score: -100
Time Limit Exceeded

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:


result: