QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#359286 | #6329. Colorful Graph | ucup-team1004 | WA | 1ms | 4072kb | C++14 | 2.1kb | 2024-03-20 15:53:30 | 2024-03-20 15:53:31 |
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);
}
}
int in[N];
bitset<N>to[N],vis;
void topo(){
queue<int>q;
for(int i=1;i<=sct;i++){
if(!in[i])q.push(i);
}
for(int u;!q.empty();){
u=q.front(),q.pop();
to[u][u]=1;
For(S,i,u,v){
to[v]|=to[u];
if(!--in[v])q.push(v);
}
}
}
int match[N];
bool dfs(int u){
static bitset<N>ret;
for(int v;ret=vis,ret&=to[u],v=ret._Find_first(),v<=sct;){
vis[v]=0;
if(!match[v]||dfs(match[v]))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]),in[u]++;
}
}
}
topo();
for(int i=1;i<=sct;i++)to[i][i]=0;
// for(int i=1;i<=sct;i++){
// for(int j=1;j<=sct;j++){
// if(to[i][j])debug(i,j);
// }
// }
for(int i=1;i<=sct;i++)vis.set(),dfs(i);
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: 0
Wrong Answer
time: 1ms
memory: 4072kb
input:
5 5 1 4 2 3 1 3 2 5 5 1
output:
3 3 1 2 3
result:
wrong answer Integer 3 violates the range [1, 2]