QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#359325 | #6329. Colorful Graph | ucup-team1004 | TL | 3498ms | 11172kb | C++14 | 2.1kb | 2024-03-20 16:26:20 | 2024-03-20 16:26:21 |
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];
vector<int>to[N],tr[N];
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(int v:to[u]){
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>a[N],vis;
int match[N],now[N];
bool dfs(int u,int t){
if(now[u]==t)return 0;
now[u]=t;
for(int v=a[u]._Find_first();v<=sct;v=a[u]._Find_next(v)){
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,to[u].push_back(v);
}
for(int i=1;i<=n;i++){
if(!dfn[i])tarjan(i);
}
for(int i=1;i<=n;i++)scc[i]=sct-scc[i]+1;
for(int u=1;u<=n;u++){
for(int v:to[u]){
if(scc[u]!=scc[v]){
tr[scc[v]].push_back(scc[u]);
}
}
}
for(int i=sct;i>=1;i--){
a[i][i]=1;
for(int j:tr[i])a[j]|=a[i];
}
for(int i=1;i<=sct;i++)a[i][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;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 4296kb
input:
5 5 1 4 2 3 1 3 2 5 5 1
output:
1 1 1 2 1
result:
ok AC
Test #2:
score: 0
Accepted
time: 1ms
memory: 6320kb
input:
5 7 1 2 2 1 4 3 5 1 5 4 4 1 4 5
output:
2 2 1 1 1
result:
ok AC
Test #3:
score: 0
Accepted
time: 1ms
memory: 4272kb
input:
8 6 6 1 3 4 3 6 2 3 4 1 6 4
output:
4 4 4 4 3 4 2 1
result:
ok AC
Test #4:
score: 0
Accepted
time: 3498ms
memory: 11172kb
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:
649 137 507 413 882 618 404 715 785 246 508 314 986 556 788 373 1235 325 910 1726 966 1166 1716 753 176 862 1345 499 889 1579 580 582 1012 1746 774 440 1465 947 403 641 226 969 716 1217 1442 1064 1597 457 423 1368 870 642 459 1151 345 1564 223 481 720 719 599 801 815 1451 1593 1382 1063 686 424 1272...
result:
ok AC
Test #5:
score: 0
Accepted
time: 26ms
memory: 10892kb
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:
1171 962 1716 1869 611 855 1448 1663 302 1744 736 1818 1125 1060 1591 792 1850 1597 576 4 1632 621 239 1102 975 1868 1486 438 1552 766 670 110 274 1377 495 1410 963 1860 2217 2199 2021 2097 1147 26 488 852 2313 1961 1502 761 2316 801 31 2130 523 249 831 2205 1080 601 829 342 148 736 1529 776 1159 17...
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...