QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#369081 | #6329. Colorful Graph | Vengeful_Spirit# | RE | 0ms | 0kb | C++20 | 4.7kb | 2024-03-27 20:21:06 | 2024-03-27 20:21:06 |
answer
#include<bits/stdc++.h>
using namespace std;
const int N=2*7005;
namespace flow {
int S,T,head[N],kl=1,n,INF=1e9,dis[N],cur[N];
struct edge{
int v,flow,next;
}e[2*N];
void add_edge(int x,int y,int w){
e[++kl]={y,w,head[x]};
head[x]=kl;
// cout<<"add "<<x<<" "<<y<<" "<<w<<endl;
}
void add(int x,int y){
y=y+n;
add_edge(x,y,1);
add_edge(y,x,0);
}
void reset(int m){
n=m;
S=2*n+1;
T=2*n+2;
for(int i=1;i<=n;i++){
add_edge(S,i,1);
add_edge(i,S,0);
add_edge(i+n,T,1);
add_edge(T,i+n,0);
}
}
int bfs(){
queue<int>q;
q.push(S);
for(int i=1;i<=T;i++)dis[i]=n+1;;
dis[S]=0;
while(!q.empty()){
int x=q.front();
// cout<<"bfs: "<<x<<endl;
q.pop();
if(x==T)return 1;
for(int i=head[x];i;i=e[i].next){
int v=e[i].v;
if(e[i].flow==0)continue;
// cout<<"v: "<<v<<endl;
if(dis[v]>dis[x]+1){
dis[v]=dis[x]+1;
q.push(v);
}
}
}
return 0;
}
int dfs(int x,int in){
if(x==T)return in;
if(in==0)return 0;
int out=0;
for(int &i=cur[x];i;i=e[i].next){
int v=e[i].v;
if(dis[v]==dis[x]+1){
// cout<<"flow "<<x<<" "<<v<<endl;
int w=dfs(v,min(in,e[i].flow));
in-=w;
out+=w;
// cout<<"modify "<<x<<" "<<e[i].flow<<" "<<e[i^1].flow<<endl;
e[i].flow-=w;
e[i^1].flow+=w;
if(in==0)return out;
}
}
return out;
}
int fa[N];
int find(int x){
if(x==fa[x])return x;
else return fa[x]=find(fa[x]);
}
void merge(int x,int y){
x=find(x);
y=find(y);
fa[x]=y;
}
int mp[N],id=0;
int max_flow(){
int ans=0;
/*
bfs();
for(int i=1;i<=T;i++)cur[i]=head[i];
ans+=dfs(S,2e9);
for(int x=1;x<=T;x++){
for(int i=head[x];i;i=e[i].next){
int v=e[i].v;
cout<<x<<" "<<v<<" "<<e[i].flow<<endl;
}
}
*/
while(bfs()){
for(int i=1;i<=T;i++)cur[i]=head[i];
ans+=dfs(S,2e9);
}
// cout<<"flow: "<<ans<<endl;
for(int i=1;i<=n;i++)fa[i]=i;
for(int x=1;x<=n;x++){
for(int i=head[x];i;i=e[i].next){
int v=e[i].v;
if(v>2*n)break;
// cout<<x<<" "<<v<<" "<<e[i].flow<<" "<<e[i^1].flow<<endl;
if(e[i^1].flow!=0){
merge(x,v-n);
}
}
}
for(int x=1;x<=n;x++){
find(x);
if(mp[fa[x]]==0)mp[fa[x]]=++id;
}
}
}
vector<int>e[N];
int dfn[N],low[N],cc,col[N];
int indec,in[N];
vector<int>bel[N];
vector<int>st;
void dfs(int x){
dfn[x]=low[x]=++indec;
in[x]=1;
st.push_back(x);
for(auto v:e[x]){
if(!dfn[v]){
dfs(v);
low[x]=min(low[x],low[v]);
}
else {
if(in[v])low[x]=min(low[x],dfn[v]);
}
}
if(dfn[x]==low[x]){
++cc;
while(st.back()!=x){
col[st.back()]=cc;
bel[cc].push_back(st.back());
in[st.back()]=0;
st.pop_back();
}
col[st.back()]=cc;
in[st.back()]=0;
bel[cc].push_back(st.back());
st.pop_back();
}
}
int main(){
int n,m;
cin>>n>>m;
map<pair<int,int>,int>mp;
for(int i=1;i<=m;i++){
int x,y;
cin>>x>>y;
e[x].push_back(y);
mp[{x,y}]=1;
}
for(int i=1;i<=n;i++){
if(!dfn[i])dfs(i);
}
flow::reset(cc);
for(int i=1;i<=cc;i++){
for(int j=1;j<=cc;j++){
int ok=0;
if(i==j)continue;
for(auto x:bel[i]){
if(ok)break;
for(auto y:bel[j]){
if(mp.find({x,y})!=mp.end()){
ok=1;break;
}
}
}
if(ok){
// cout<<"add! "<<i<<" "<<j<<endl;
flow::add(i,j);
}
}
}
flow::max_flow();
for(int i=1;i<=n;i++){
cout<<flow::mp[flow::fa[col[i]]]<<" ";
}
}
詳細信息
Test #1:
score: 0
Runtime Error
input:
5 5 1 4 2 3 1 3 2 5 5 1