QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#214751 | #6329. Colorful Graph | pengpeng_fudan | WA | 1673ms | 7104kb | C++14 | 3.5kb | 2023-10-14 23:36:10 | 2023-10-14 23:36:11 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
using namespace std;
using ull=unsigned long long;
using ll=long long;
const int N=7010,M=4*7010;
vector<int> ve[7010];
vector<int> re[7010];
int n,m;
int stk[N],sz[N],scc[N];
int dfn[N]; //时间戳,记录dfs序
int low[N];//追溯值
bool instk[N];
int cnt=0,tot=0,top=0;
void tarjan(int u){
dfn[u]=low[u]=++tot;
stk[++top]=u,instk[u]=true;
for(auto i:ve[u]){
if(!dfn[i]){
tarjan(i);
low[u]=min(low[u],low[i]);
}
else if(instk[i]) low[u]=min(low[u],dfn[i]);
}
if(dfn[u]==low[u]){
++cnt;
int y;
do{
y=stk[top--];
instk[y]=false;
scc[y]=cnt;
sz[cnt]++;
}while(y!=u);
}
}
const ll INF=1e18;
struct edge{int v;ll w,pre_w,nxt;}edge[2*M];
int head[2*N],cur[2*N],dep[2*N];
int ctz=1,st,ed;
int pot_sum;
void add(int u,int v,ll w){
edge[++ctz]={v,w,w,head[u]};
head[u]=ctz;
edge[++ctz]={u,0,0,head[v]};
head[v]=ctz;
}
bool bfs(){
copy(head,head+pot_sum+1,cur);
fill(dep,dep+pot_sum+1,-1);
queue<int> qe;
dep[st]=0;qe.push(st);
while(!qe.empty()){
int t=qe.front();qe.pop();
for(int i=head[t];i;i=edge[i].nxt){
int v=edge[i].v;ll w=edge[i].w;
if(w>0&&dep[v]==-1){
dep[v]=dep[t]+1;
qe.push(v);
}
}
}
return dep[ed]!=-1;
}
ll dfs(int u=st,ll flow=INF){
ll rem=flow;
if(u==ed) return flow;
for(int i=cur[u];i&&flow;i=edge[i].nxt){
cur[u]=i;
int v=edge[i].v;ll w=edge[i].w;
if(w>0&&dep[v]==dep[u]+1){
ll t=dfs(v,min(w,rem));
rem-=t;
edge[i].w-=t;
edge[i^1].w+=t;
}
}
return flow-rem;
}
ll dinic(){
ll ans=0;
while(bfs()) ans+=dfs();
return ans;
}
int col[N];
int fa[N];
int get(int x){return fa[x]==x?x:fa[x]=get(fa[x]);}
void merge(int u,int v){
u=get(u),v=get(v);
fa[u]=v;
}
bool flag[2*N];
void color(int u,int num){
flag[u]=1;
if(u>cnt) fa[u-cnt]=num;
for(int i=head[u];i;i=edge[i].nxt){
if(i==ed&&edge[i].w==0){
edge[i].w=1;
return ;
}
}
for(int i=head[u];i;i=edge[i].nxt){
int v=edge[i].v;ll w=edge[i].pre_w-edge[i].w;
if(v!=st&&w>0&&!flag[v]){
edge[i].w++;
edge[i^1].w--;
color(v,num);
return ;
}
}
}
void solve() {
cin>>n>>m;
int u,v;
for(int i=1;i<=m;i++){
cin>>u>>v;
ve[u].push_back(v);
}
for(int i=1;i<=n;i++){
if(!dfn[i]) tarjan(i);
}
st=0,ed=2*cnt+1;
for(int i=1;i<=cnt;i++){add(0,i,1),add(i+cnt,ed,1),add(i+cnt,i,INF);}
for(int i=1;i<=n;i++){
for(auto j:ve[i]){
if(scc[j]!=scc[i]) add(scc[i],scc[j]+cnt,INF);
}
}
pot_sum=2*cnt+1;
ll ans=dinic();
int c=0;
for(int i=1;i<=cnt;i++) fa[i]=i;
for(int i=1;i<=cnt;i++){
fill(flag,flag+pot_sum+1,0);
color(i,i);
}
for(int i=1;i<=cnt;i++){
if(!col[get(i)]) col[get(i)]=++c;
col[i]=col[get(i)];
}
for(int i=1;i<=n;i++) cout<<col[scc[i]]<<' ';
cout<<'\n';
}
signed main() {
ios::sync_with_stdio(0),cin.tie(0);
int _ = 1;
//cin >> _;
while(_--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3868kb
input:
5 5 1 4 2 3 1 3 2 5 5 1
output:
2 2 2 1 2
result:
ok AC
Test #2:
score: 0
Accepted
time: 1ms
memory: 3892kb
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: 3812kb
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: 1321ms
memory: 7104kb
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:
1304 1657 1326 1564 173 964 1672 1365 1110 1505 478 851 1673 504 1519 1160 516 599 949 25 1013 585 380 671 1662 29 980 34 1404 28 1171 1198 1169 27 1357 1358 286 1170 836 26 1525 916 598 534 25 687 981 1331 24 597 890 23 1292 800 1647 187 939 789 1031 22 1192 639 1655 288 158 596 1478 298 1092 21 88...
result:
ok AC
Test #5:
score: 0
Accepted
time: 1603ms
memory: 6888kb
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:
586 1021 618 465 1723 1127 886 671 2104 90 178 1780 1662 1274 2266 89 1330 366 588 2330 88 1713 2095 1232 1029 450 848 1896 782 1568 1664 547 1390 1089 87 86 1371 1515 117 135 85 237 1187 84 1846 83 21 82 81 1573 18 1533 2303 204 1811 1449 80 79 78 77 1898 695 76 180 805 1558 1748 617 895 2264 75 15...
result:
ok AC
Test #6:
score: 0
Accepted
time: 1578ms
memory: 6792kb
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:
1313 1581 464 319 2256 1582 165 1084 1720 789 1209 1051 788 1210 1755 529 1137 385 1158 1690 1020 1583 787 1446 1840 266 1643 413 1050 786 1792 1188 1334 1323 1410 1584 1970 421 94 785 2126 2073 1741 1049 1998 1176 1342 303 1585 1056 377 1586 1672 1538 1125 323 1464 784 583 2329 1224 2282 1587 2183 ...
result:
ok AC
Test #7:
score: 0
Accepted
time: 1533ms
memory: 7008kb
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:
1750 1406 1664 23 1501 1680 1522 1749 354 1748 1576 155 56 712 1347 1443 1548 31 872 1747 1345 1220 795 607 1367 1504 1428 1007 1746 1745 1024 1744 885 233 1561 1434 1536 226 378 699 1113 282 879 925 1743 1453 1742 1275 1741 1354 105 1188 1740 1739 970 1738 256 709 783 807 1737 1673 987 113 792 1736...
result:
ok AC
Test #8:
score: 0
Accepted
time: 1673ms
memory: 6412kb
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:
3498 3497 3496 3495 1073 3494 3493 2667 1045 3245 3304 2946 3492 434 3491 3490 3489 976 3488 3487 125 3486 3485 2692 3484 3483 3482 3481 3480 1675 3479 1067 3478 3477 3194 3476 1614 459 3475 515 971 3474 3473 1204 3088 3472 3471 3470 3469 3468 2522 1552 3493 3467 263 2395 3466 3465 3464 3463 3462 57...
result:
ok AC
Test #9:
score: 0
Accepted
time: 3ms
memory: 5872kb
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: 376ms
memory: 6772kb
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: -100
Wrong Answer
time: 57ms
memory: 5604kb
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:
24 30 1 1 20 20 1 7 1 1 1 1 1 31 1 7 1 26 1 1 14 1 30 1 1 1 1 1 1 1 1 1 32 1 1 7 35 34 1 1 1 20 17 1 12 1 14 1 1 7 1 1 1 1 1 7 1 1 1 36 14 35 35 1 1 1 1 13 30 1 5 30 1 1 20 1 21 13 30 1 24 16 1 1 3 6 1 1 16 1 30 1 1 1 1 18 37 1 3 1 1 1 1 34 1 1 26 1 1 21 1 1 1 24 34 1 30 1 1 1 1 21 13 1 34 1 1 1 1 3...
result:
wrong answer Integer 72 violates the range [1, 71]