QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#215378#6329. Colorful Graphpengpeng_fudanWA 950ms6696kbC++143.0kb2023-10-15 09:23:122023-10-15 09:23:13

Judging History

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

  • [2023-10-15 09:23:13]
  • 评测
  • 测评结果:WA
  • 用时:950ms
  • 内存:6696kb
  • [2023-10-15 09:23:12]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ull=unsigned long long;
using ll=long long;
const int N=7010,M=4*7010;
vector<int> ve[7010];
vector<int> re[7010];
int n,m;
int stk[N],sz[N],scc[N];
int dfn[N]; //时间戳,记录dfs序
int low[N];//追溯值
bool instk[N];
int cnt=0,tot=0,top=0;
void tarjan(int u){
    dfn[u]=low[u]=++tot;
    stk[++top]=u,instk[u]=true;
    for(auto i:ve[u]){
        if(!dfn[i]){
            tarjan(i);
            low[u]=min(low[u],low[i]);
        }
        else if(instk[i])   low[u]=min(low[u],dfn[i]);
    }
    if(dfn[u]==low[u]){
        ++cnt;
        int y;
        do{
            y=stk[top--];
            instk[y]=false;
            scc[y]=cnt;
            sz[cnt]++;
        }while(y!=u);
    }
}
const ll INF=1e18;
struct edge{int v;ll w,pre_w,nxt;}edge[2*M];
int head[2*N],cur[2*N],dep[2*N];
int ctz=1,st,ed;
int pot_sum;
void add(int u,int v,ll w){
    edge[++ctz]={v,w,w,head[u]};
    head[u]=ctz;
    edge[++ctz]={u,0,0,head[v]};
    head[v]=ctz;    
}
bool bfs(){
    copy(head,head+pot_sum+1,cur);
    fill(dep,dep+pot_sum+1,-1);
    queue<int> qe;
    dep[st]=0;qe.push(st);
    while(!qe.empty()){
        int t=qe.front();qe.pop();
        for(int i=head[t];i;i=edge[i].nxt){
            int v=edge[i].v;ll w=edge[i].w;
            if(w>0&&dep[v]==-1){
                dep[v]=dep[t]+1;
                qe.push(v);
            }
        }
    }
    return dep[ed]!=-1;
}
ll dfs(int u=st,ll flow=INF){
    ll rem=flow;
    if(u==ed)   return flow;
    for(int i=cur[u];i&&flow;i=edge[i].nxt){
        cur[u]=i;
        int v=edge[i].v;ll w=edge[i].w;
        if(w>0&&dep[v]==dep[u]+1){
            ll t=dfs(v,min(w,rem));
            rem-=t;
            edge[i].w-=t;
            edge[i^1].w+=t;
        } 
    }
    return flow-rem;
}
ll dinic(){
    ll ans=0;
    while(bfs())    ans+=dfs();
    return ans;
}
int col[N];
int c=0;
int t[N];
int color(int u){
    for(int i=head[u];i;i=edge[i].nxt){
        int v=edge[i].v;ll w=edge[i].pre_w-edge[i].w;
        if(v!=st&&v!=ed&&w>0){
			edge[i].w++;
            int t=color(v-cnt);
            col[u]=t;
            return t;
        }
    } 
    if(!col[u]||t[col[u]]!=u) col[u]=++c;
    return col[u];  
}

void solve() {
    cin>>n>>m;
    int u,v;
    for(int i=1;i<=m;i++){
        cin>>u>>v;
        ve[u].push_back(v);
    } 
    for(int i=1;i<=n;i++){
        if(!dfn[i]) tarjan(i);
    } 
    st=0,ed=2*cnt+1;
    for(int i=1;i<=cnt;i++){add(0,i,1),add(i+cnt,ed,1),add(i+cnt,i,INF);}
    for(int i=1;i<=n;i++){
        for(auto j:ve[i]){
            if(scc[j]!=scc[i])  add(scc[i],scc[j]+cnt,INF);
        }
    }
    pot_sum=2*cnt+1;
    ll ans=dinic();
    int c=0;  
    for(int i=1;i<=cnt;i++){
        if(!col[i]){
			color(i);
			t[col[i]]=i;
		}
    }
    for(int i=1;i<=n;i++)   cout<<col[scc[i]]<<' ';
    cout<<'\n';
}
int main() {
    ios::sync_with_stdio(0),cin.tie(0);
    int _ = 1;
    //cin >> _;
    while(_--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1ms
memory: 3828kb

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: 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
Wrong Answer
time: 950ms
memory: 6696kb

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:

2607 3300 2662 3111 3863 3166 3325 2738 2022 1927 3904 4877 3326 5092 3037 3402 516 3455 1756 25 2509 585 5114 2995 3310 29 3456 4253 2820 28 1365 2382 3457 27 2722 2723 286 3458 5138 26 1958 2236 3459 534 25 687 3460 4189 24 3461 915 23 1577 4895 3275 187 4399 3462 1131 22 2367 4686 3762 4041 158 3...

result:

wrong answer Integer 2607 violates the range [1, 1750]