QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#369199#6329. Colorful GraphVengeful_Spirit#WA 1336ms6300kbC++205.0kb2024-03-27 21:45:232024-03-27 21:45:24

Judging History

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

  • [2024-03-27 21:45:24]
  • 评测
  • 测评结果:WA
  • 用时:1336ms
  • 内存:6300kb
  • [2024-03-27 21:45:23]
  • 提交

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[8*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+n,i,INF);
            add_edge(i,i+n,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]=INF;
        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<<" "<<dis[v]<<" "<<dis[x]+1<<endl;
              //  if(v==T)return 1;
                if(dis[v]>dis[x]+1){
                    dis[v]=dis[x]+1;
                    q.push(v);
                }
            }
        }
        return 0;
    }
    int mp[N],id=0;
    int fnd(int x){
        if(x==S||x==T)return 0;
        if(x>n)return x-n;
        return x;
    }
    int dfs(int x,int in,int c){
        if(x==T){
            mp[fnd(x)]=c;
            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),c);
                in-=w;
                out+=w;
                if(w)mp[fnd(v)]=c;
            //    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;
            }
        }
        if(out)mp[fnd(x)]=c;
        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;
    }
    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;
            }
        }
        */
       int rnd=0;
        while(bfs()){
            for(int i=1;i<=T;i++)cur[i]=head[i];
            ans+=dfs(S,2e9,++rnd);
         /*   cout<<"round "<<endl;
            for(int x=1;x<=T;x++){
            for(int i=head[x];i;i=e[i].next){
                int v=e[i].v;
                if(!(i&1))
                cout<<x<<" "<<v<<" "<<e[i].flow<<endl;
            }
        }
        */
        }
        for(int i=1;i<=n;i++){
            if(!mp[i])mp[i]=++rnd;
        }
       // for(int i=1;i<=n;i++)cout<<mp[i]<<" ";cout<<endl;
    }
}
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<=n;i++)cout<<col[i]<<" ";
   // cout<<endl;

    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[col[i]]<<" ";
    }
    
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3856kb

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: 3644kb

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: 3796kb

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
Wrong Answer
time: 1336ms
memory: 6300kb

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:

412 1750 1750 1750 1750 1 1478 1750 1750 1 1750 1750 1 1750 1750 1593 1027 1672 1750 41 1750 1161 1750 1750 1750 1 1 1750 1750 1 1 1750 1 1 1750 1750 581 1 1750 1 1 1750 1 1057 1 1363 1 1750 1 1 1750 1 1 1750 1750 363 1750 1 1 1 1750 1750 1750 1750 305 1 1750 1750 1750 1 1 1409 1 1 1 1 1750 1127 1 1...

result:

wrong answer 13 and 6 are not connected