QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#369135#6329. Colorful GraphVengeful_Spirit#RE 1ms3856kbC++204.8kb2024-03-27 20:55:112024-03-27 20:55:13

Judging History

你现在查看的是最新测评结果

  • [2024-03-27 20:55:13]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3856kb
  • [2024-03-27 20:55:11]
  • 提交

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,INF);
        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,i+n,INF);
            add_edge(i+n,i,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;
    void 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)continue;
                if(v==x+n)continue;
        //        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: 100
Accepted
time: 0ms
memory: 3856kb

input:

5 5
1 4
2 3
1 3
2 5
5 1

output:

2 2 2 1 2 

result:

ok AC

Test #2:

score: 0
Accepted
time: 0ms
memory: 3816kb

input:

5 7
1 2
2 1
4 3
5 1
5 4
4 1
4 5

output:

1 1 2 2 2 

result:

ok AC

Test #3:

score: 0
Accepted
time: 1ms
memory: 3800kb

input:

8 6
6 1
3 4
3 6
2 3
4 1
6 4

output:

1 1 1 1 2 1 3 4 

result:

ok AC

Test #4:

score: -100
Runtime Error

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:


result: