QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#426401 | #6329. Colorful Graph | Kevin5307 | RE | 0ms | 0kb | C++23 | 2.0kb | 2024-05-31 10:09:20 | 2024-05-31 10:09:20 |
answer
#include<bits/stdc++.h>
using namespace std;
#define pb emplace_back
int n,m;
vector<int> G[7007],rG[7007],nG[7007];
int vis[7007],scc[7007],tot;
vector<int> ord;
void dfs1(int u)
{
vis[u]=1;
for(auto v:G[u])
if(!vis[v])
dfs1(v);
ord.pb(u);
}
void dfs2(int u,int c)
{
scc[u]=c;
for(auto v:rG[u])
if(!scc[v])
dfs2(v,c);
}
bitset<7007> E[7007];
void dfs(int u,int frm)
{
E[frm][u]=1;
for(auto v:nG[u])
if(!E[frm][v])
dfs(v,frm);
}
int Match[7007],MatchR[7007];
bitset<7007> flag;
int lst[7007];
int match(int x)
{
queue<int> q;
flag=0;
memset(lst,-1,sizeof(lst));
q.push(x);
while(!q.empty())
{
int u=q.front();
q.pop();
bitset<7007> tmp=(E[u]&flag)^E[u];
flag|=E[u];
for(int p=tmp._Find_first();p!=tmp.size();p=tmp._Find_next(p))
{
if(!Match[p])
{
Match[p]=x;
while(u!=x)
{
int v=MatchR[x];
MatchR[x]=p;
p=v;
x=lst[p];
Match[p]=x;
}
MatchR[x]=p;
return 1;
}
lst[p]=u;
q.push(Match[p]);
}
}
return 0;
}
int fa[7007],ind[7007];
inline int anc(int x)
{
while(fa[x]!=x) x=fa[x]=fa[fa[x]];
return x;
}
int main()
{
freopen("color.in","r",stdin);
freopen("color.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=m;i++)
{
int u,v;
cin>>u>>v;
G[u].pb(v);
rG[v].pb(u);
}
for(int i=1;i<=n;i++)
if(!vis[i])
dfs1(i);
reverse(ord.begin(),ord.end());
for(auto x:ord)
if(!scc[x])
dfs2(x,++tot);
for(int i=1;i<=n;i++)
for(auto j:G[i])
if(scc[i]!=scc[j])
nG[scc[i]].pb(scc[j]);
for(int i=1;i<=tot;i++)
dfs(i,i);
for(int i=1;i<=tot;i++)
E[i][i]=0;
int val=0;
for(int i=1;i<=tot;i++)
val+=match(i);
for(int i=1;i<=tot;i++)
fa[i]=i;
for(int i=1;i<=tot;i++)
if(MatchR[i])
fa[anc(i)]=anc(MatchR[i]);
int cnt=0;
for(int i=1;i<=tot;i++)
if(fa[i]==i)
ind[i]=++cnt;
for(int i=1;i<=n;i++)
cout<<ind[anc(scc[i])]<<" ";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
5 5 1 4 2 3 1 3 2 5 5 1