QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#359323 | #6329. Colorful Graph | ucup-team1004 | TL | 3898ms | 10736kb | C++14 | 2.2kb | 2024-03-20 16:21:38 | 2024-03-20 16:21:39 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define all(a) (a).begin(),(a).end()
#ifdef DEBUG
template<class T>
ostream& operator << (ostream &out,vector<T> a){
out<<'[';
for(T x:a)out<<x<<',';
return out<<']';
}
template<class T>
vector<T> ary(T *a,int l,int r){
return vector<T>{a+l,a+1+r};
}
template<class T>
void debug(T x){
cerr<<x<<endl;
}
template<class T,class...S>
void debug(T x,S...y){
cerr<<x<<' ',debug(y...);
}
#else
#define debug(...) void()
#endif
const int N=7e3+10,INF=1e9;
int n,m,col[N];
struct Vector{
int k,head[N],val[N],nex[N];
void add(int x,int y){
val[++k]=y,nex[k]=head[x],head[x]=k;
}
}G,S;
#define For(A,i,u,v) for(int i=A.head[u],v;v=A.val[i],i;i=A.nex[i])
int dft,sct,dfn[N],low[N],scc[N];
void tarjan(int u){
static int top,stk[N];
dfn[u]=low[u]=++dft,stk[++top]=u;
For(G,i,u,v){
if(!dfn[v])tarjan(v),low[u]=min(low[u],low[v]);
else if(!scc[v])low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u]){
sct++;
do scc[stk[top]]=sct;while(stk[top--]^u);
}
}
bitset<N>to[N],vis;
int match[N],now[N];
bool dfs(int u,int t){
if(now[u]==t)return 0;
now[u]=t;
for(int i=to[u]._Find_first();i<sct;i=to[u]._Find_next(i)){
int v=sct-i;
if(!match[v]||dfs(match[v],t))return match[v]=u,1;
}
return 0;
}
int main(){
cin>>n>>m;
for(int u,v;m--;){
cin>>u>>v,G.add(u,v);
}
for(int i=1;i<=n;i++){
if(!dfn[i])tarjan(i);
}
for(int u=1;u<=n;u++){
For(G,i,u,v){
if(scc[u]!=scc[v]){
S.add(scc[v],scc[u]);
}
}
}
for(int i=1;i<=sct;i++){
to[i][sct-i]=1;
For(S,k,i,j)to[j]|=to[i];
}
for(int i=1;i<=sct;i++)to[i][sct-i]=0;
// for(int i=1;i<=sct;i++){
// for(int j=1;j<=sct;j++){
// if(to[i][j])debug(i,j);
// }
// }
static int cur[N],is[N];
static mt19937 rnd(time(0));
iota(cur,cur+1+sct,0);
shuffle(cur+1,cur+1+sct,rnd);
for(int T=1;;T++){
int flag=0;
for(int x:cur){
if(!is[x]&&dfs(x,T)){
is[x]=flag=1;
}
}
if(!flag)break;
debug(T);
}
vis.reset();
for(int i=1;i<=sct;i++)vis[match[i]]=1;
for(int i=1,x=0;i<=sct;i++)if(!vis[i]){
x++;
for(int u=i;u;u=match[u])col[u]=x;
}
for(int i=1;i<=n;i++)printf("%d%c",col[scc[i]],"\n "[i<n]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3996kb
input:
5 5 1 4 2 3 1 3 2 5 5 1
output:
2 2 1 2 2
result:
ok AC
Test #2:
score: 0
Accepted
time: 0ms
memory: 4000kb
input:
5 7 1 2 2 1 4 3 5 1 5 4 4 1 4 5
output:
1 1 2 2 2
result:
ok AC
Test #3:
score: 0
Accepted
time: 0ms
memory: 3940kb
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: 3898ms
memory: 10736kb
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:
810 1602 1278 1233 738 828 1650 1332 925 246 1072 1731 1452 1114 1119 288 1235 1644 861 1726 909 1166 1323 1135 1190 145 635 1479 1014 375 580 1154 478 930 1236 1323 1465 146 418 1282 226 1092 1390 1217 1144 1064 1478 996 244 477 861 1507 459 1181 1590 1564 1601 672 720 1645 1095 1458 750 1445 1593 ...
result:
ok AC
Test #5:
score: 0
Accepted
time: 33ms
memory: 10436kb
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:
680 939 1716 1869 611 60 1448 1663 1793 2279 513 328 1514 1060 1881 560 1766 1115 680 4 2002 621 239 1102 1794 1925 1486 438 1552 766 670 722 2305 790 866 1222 963 1583 2217 2199 1193 2097 1147 1799 488 1651 2313 34 1919 761 2316 801 31 2130 523 268 2328 104 1129 309 2096 562 1551 967 1529 776 1130 ...
result:
ok AC
Test #6:
score: -100
Time Limit Exceeded
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...