QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#426402 | #6329. Colorful Graph | Kevin5307 | TL | 1ms | 5788kb | C++23 | 1.9kb | 2024-05-31 10:09:43 | 2024-05-31 10:09:43 |
Judging History
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()
{
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: 100
Accepted
time: 1ms
memory: 5788kb
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: 0ms
memory: 3884kb
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: 3888kb
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: -100
Time Limit Exceeded
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 ...