QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#527278#6329. Colorful GraphMitsukasa_AyaseRE 0ms3880kbC++203.2kb2024-08-22 13:22:022024-08-22 13:22:03

Judging History

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

  • [2024-08-22 13:22:03]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3880kb
  • [2024-08-22 13:22:02]
  • 提交

answer

#include<bits/stdc++.h>
#define N 7005
using namespace std;
int dfn[N],low[N],cc;
stack<int>S; int in[N];
int bel[N],T;
vector<int> V[N];
pair<int,int> E[N];
void tarjan(int x) {
	dfn[x]=low[x]=++cc; S.push(x),in[x]=1;
	for (auto v:V[x]) {
		if (!dfn[v]) tarjan(v),low[x]=min(low[x],low[v]);
		else if (in[v]) low[x]=min(low[x],dfn[v]);
	}
	if (dfn[x]==low[x]) {
		int y; ++T;
		do y=S.top(),S.pop(),in[y]=0,bel[y]=T; while (y!=x);
	}
}
struct edge{
    int to,nxt,w,ww;
}e[N<<4];
int head[N<<2],cnt=1;
void add(int u,int v,int w){
    e[++cnt]=(edge){v,head[u],w,w}; head[u]=cnt;
}
int s,t;
int cur[N<<2],dep[N<<2],gap[N<<2];
int mxfl=0;
queue<int> q;
void bfs(){
    for(int i=0;i<=T*2+2+5;++i) dep[i]=-1,gap[i]=0;
    dep[t]=0,gap[0]=1;
    q.push(t);
    while(!q.empty()){
        int u=q.front(); q.pop();
        for(int i=head[u];i;i=e[i].nxt){
            int v=e[i].to; if(dep[v]!=-1) continue;
            dep[v]=dep[u]+1; ++gap[dep[v]];
            q.push(v);
        }
    }
}
int dfs(int x,int fl){
    if(x==t){
        mxfl+=fl;
        return fl;
    }
    int usd=0;
    for(int i=cur[x];i;cur[x]=i=e[i].nxt){
        int v=e[i].to;
        if(e[i].w==0||dep[x]!=dep[v]+1) continue;
        int tmp=dfs(v,min(fl-usd,e[i].w));
        e[i].w-=tmp,e[i^1].w+=tmp;
        usd+=tmp;
        if(usd==fl) return fl;
    }
    --gap[dep[x]++]==0?dep[s]=T*2+3:++gap[dep[x]];
    return usd;
}
void ISAP(){
    bfs();
    while(dep[s]<=T*2+2){
        for(int i=1;i<=T*2+2;++i) cur[i]=head[i];
        dfs(s,10000);
    }
}
int fa[N];
int find(int x){
    return x==fa[x]?x:fa[x]=find(fa[x]);
}
int C[N],tc;
int st[N],tp;
void dfs(int x){
    // cerr<<"dfs "<<x<<endl;
    st[++tp]=x;
    if(x==s){
        // cerr<<st[2]<<" "<<st[tp-1]<<endl;
        if(st[2]-T<=0){
            cout<<"nani\n";
            exit(0);
        }
        int u=find(st[2]-T),v=find(st[tp-1]);
        fa[u]=v;
        return;
    }
    for(int i=head[x];i;i=e[i].nxt){
        if(e[i].w!=e[i].ww){
            --e[i].w,++e[i^1].w;
            dfs(e[i].to);
            break;
        }
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;++i){
        cin>>E[i].first>>E[i].second;
        V[E[i].first].push_back(E[i].second);
    }
    for(int i=1;i<=n;++i)
        if(!dfn[i])
            tarjan(i);
    // for(int i=1;i<=n;++i)
    //     cerr<<bel[i]<<" \n"[i==n];
    // if(n>=100) return 0;
    for(int i=1;i<=m;++i){
        int a=bel[E[i].first],b=bel[E[i].second];
        if(a!=b){
            add(a,b+T,n);
            add(b+T,a,0);
        }
    }
    for(int a=1;a<=T;++a){
        add(a+T,a,n);
        add(a,a+T,0);
    }
    s=2*T+1,t=2*T+2;
    for(int i=1;i<=T;++i){
        add(s,i,1);
        add(i,s,0);
        add(i+T,t,1);
        add(t,i+T,0);
    }
    ISAP();
    // cerr<<mxfl<<endl;
    for(int i=1;i<=T;++i) fa[i]=i;
    for(int i=1;i<=mxfl;++i){
        tp=0;
        dfs(t);
    }
    for(int i=1;i<=T;++i){
        int f=find(i);
        if(C[f]) C[i]=C[f];
        else{
            C[i]=C[f]=++tc;
        }
    }
    for(int i=1;i<=n;++i)
        cout<<C[bel[i]]<<" \n"[i==n];
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3668kb

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

input:

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

output:

1 1 2 1 1

result:

ok AC

Test #3:

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

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: