首页 > 代码库 > 【多校赛第三场】Redraw Beautiful Drawings【网络流】【谜のWA】

【多校赛第三场】Redraw Beautiful Drawings【网络流】【谜のWA】

 

参考题解:http://blog.csdn.net/qian99/article/details/38276887

 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int m,n,K;
const int INF=1<<30;
const int MAX_V=888;
struct edge {int to,cap,rev;};
vector<edge> G[MAX_V];
bool used[MAX_V];
bool vis[MAX_V];
int mat[MAX_V][MAX_V];
int level[MAX_V];
int iter[MAX_V];
void add_edge(int from,int to,int cap){
    G[from].push_back((edge){to,cap,G[to].size()});
    G[to].push_back((edge){from,0,G[from].size()-1});
}
void bfs(int s){
    memset(level,-1,sizeof(level));
    queue<int> que;
    level[s]=0;
    que.push(s);
    while(!que.empty()){
        int v=que.front();que.pop();
        for(int i=0;i<G[v].size();i++){
            edge &e=G[v][i];
            if(e.cap>0 && level[e.to]<0){
                level[e.to]=level[v]+1;
                que.push(e.to);
            }
        }
    }
}
int dfs(int v,int t,int f){
    if(v==t) return f;
    for(int &i=iter[v];i<G[v].size();i++){
        edge &e=G[v][i];
        if(e.cap>0 && level[v]<level[e.to]){
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0){
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}
int max_flow(int s,int t){
    int flow=0;
    for(;;){
        bfs(s);
        if(level[t]<0) return flow;
        memset(iter,0,sizeof(iter));
        int f;
        while((f=dfs(s,t,INF))>0){
            flow+=f;
        }
    }
}
bool mydfs(int now,int fa){
    //vis[now]=true;//为什么我加上这句就WA了?
    for(int i=0;i<G[now].size();i++){
        int to=G[now][i].to;
        if(to==fa) continue;
        if(G[now][i].cap){
            if(vis[to]) return true;
            vis[to]=true;
            if(mydfs(to,now)) return true;
            vis[to]=false;
        }
    }
    return false;
}
void printmat(){
    memset(mat,0,sizeof(mat));
    for(int i=1;i<=n;i++){
        for(int j=0;j<G[i].size();j++){
            mat[i][G[i][j].to-n]=K-G[i][j].cap;
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=n+1;j<=n+m;j++){
            printf("%d%c",mat[i][j-n],j==n+m?'\n':' ');
        }
        //printf("\n");
    }
}
int main(){
        #ifndef ONLINE_JUDGE
        freopen("G:/in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int tmp;
    while(~scanf("%d%d%d",&n,&m,&K)){
        for(int i=0;i<MAX_V;i++){
            G[i].clear();
        }
        memset(used,0,sizeof(used));
        memset(vis,0,sizeof(vis));
        int S=0,T=n+m+1;
        int sumn=0,summ=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&tmp);
            sumn+=tmp;
            add_edge(S,i,tmp);
        }
        for(int i=1;i<=m;i++){
            scanf("%d",&tmp);
            summ+=tmp;
            add_edge(n+i,T,tmp);
        }
        for(int i=1;i<=n;i++){
            for(int j=n+1;j<=n+m;j++){
                add_edge(i,j,K);
            }
        }
        int flow=max_flow(S,T);
        if(summ!=sumn){
            printf("Impossible\n");
            continue;
        }
        if(flow!=summ){
            printf("Impossible\n");
            continue;
        }
        memset(vis,0,sizeof(vis));
        bool flag=false;
        for(int i=1;i<=n;i++){
            if(mydfs(i,S)){
                flag=true;
                break;
            }
        }
        if(flag) printf("Not Unique\n");
        else{
            printf("Unique\n");
            printmat();
        }
    }
}