QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#382073#7648. 网格图最大流计数ooooxxxxCompile Error//C++146.3kb2024-04-08 01:56:142024-04-08 01:56:15

Judging History

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

  • [2024-04-08 01:56:15]
  • 评测
  • [2024-04-08 01:56:14]
  • 提交

answer

#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=500;
int n,m,k;
char str[N][N];
int id(int x,int y,int c){
    return (x-1)*k+y+k*k*c;
}
namespace P1{
const int N=4e5;
vector<pair<int,int>>v[N];
int cnt=-1,c[1000000],s,t;

void link(int x,int y,int _c){
    cnt++;v[x].push_back(make_pair(y,cnt));c[cnt]=_c;
    cnt++;v[y].push_back(make_pair(x,cnt));c[cnt]=0;
}    
int dis[N],cur[N];
void bfs(){
    queue<int>q;
    q.push(s);
    // memset(dis,-1,sizeof(dis));
    for(int i=0;i<=k*k*2+3;i++) dis[i]=-1;
    dis[s]=0;
    while(!q.empty()){
        int x=q.front();q.pop();
        for(auto [y,id]:v[x]){
            if(c[id]==0) continue;
            if(dis[y]==-1){
                dis[y]=dis[x]+1;q.push(y);
            }
        }
    }
}

int dfs(int x,int lim){
    if(x==t) return lim;
    if(!lim) return 0;
    int ans=0;
    for(int &i=cur[x];i<v[x].size();i++){
        auto[y,id]=v[x][i];
        if(c[id]==0||dis[y]!=dis[x]+1) continue;
        int res=dfs(y,min(lim,c[id]));
        c[id]-=res,lim-=res,c[id^1]+=res,ans+=res;
        if(!lim) break;
    }return ans;
}

int dinic(int _s,int _t){
    s=_s,t=_t;
    int ans=0;
    while(1){
        bfs();
        for(int i=0;i<=k*k*2+3;i++) cur[i]=0;
        // memset(cur,0,sizeof(cur));
        int res=dfs(s,k*2);ans+=res;
        if(!res) break;
    }return ans;
}
}
vector<int>v[400000];
int deg[400000];
// int n,m,k;
vector<int>A,B;
namespace P2{
int g[400000];
const int mod=1e9+7;
#define int long long
void dec(int &x){
    x-=(x>=mod?mod:0);
}
int dis[600][600],d[500][500][2];
void bfs(int x){
    memset(d,0,sizeof(d));
    d[1][x][0]=1;
    for(int i=1;i<=k;i++){
        for(int j=x;j<=k;j++){
            if(str[i][j]){
                dec(d[i][j][1]+=d[i][j][0]);
            }
            dec(d[i][j+1][0]+=d[i][j][1]);
            dec(d[i+1][j][0]+=d[i][j][1]);
        }
    }
    for(int i=1;i<=k;i++){
        dis[x][i]=d[k][i][1];
    }
}
int a[500][500];
int qpow(int a,int b){
    int ret=1;
    while(b){
        if(b&1) ret=ret*a%mod;
        a=a*a%mod;b>>=1;
    }return ret;
}
int det(int n){
    int ret=1;
    // cerr<<n<<'\n';
    // for(int )
    for(int i=1;i<=n;i++){
        if(!a[i][i]){
            for(int j=1;j<=n;j++){
                if(a[j][i]){
                    swap(a[j],a[i]);break;
                }
            }
            if(a[i][i]==0) return 0;
            ret=-ret;
        }
        int inv=qpow(a[i][i],mod-2);
        for(int j=i+1;j<=n;j++){
            int c=inv*a[j][i]%mod;
            for(int k=i;k<=n;k++){
                (a[j][k]-=a[i][k]*c)%=mod;
            }
        }
    }
    for(int i=1;i<=n;i++) ret=ret*a[i][i]%mod;
    return (ret+mod)%mod;
}
int val(vector<int>A,vector<int>B){
    for(int i=0;i<A.size();i++){
        for(int j=0;j<A.size();j++){
            a[i+1][j+1]=dis[A[i]][B[j]];
        }
    }
    return det(A.size());
}
vector<int>curA,curB;
int ans=0;
void dfs(int x,int pre,int lim){
    if(x==lim){
        // cerr<<"!! ";for(int z:curA) cerr<<z<<" ";cerr<<endl;
        // cerr<<"!! ";for(int z:curB) cerr<<z<<" ";cerr<<endl;
        // cerr<<"?? "<<val(curA,curB)<<endl;
        ans+=val(curA,curB);
        return ;
    }
    for(int i=pre+1;i<B.size();i++){
        if(B[i]<curA[x]) continue;
        curB.push_back(B[i]);
        dfs(x+1,i,lim);
        curB.pop_back();
    }
}
void output(){
    // cerr<<"WOW\n";
    // cerr<<val(vector<int>{1,2},vector<int>{4,4})<<endl;exit(0);
        memset(a,0,sizeof(a));
        static int C[500][500];
        int res=0;
        for(int i=0;i<A.size();i++){
            for(int j=i+1;j<A.size();j++){
                int res=0;
                for(int x=0;x<B.size();x++){
                    for(int y=x;y<B.size();y++){
                        res+=val(vector<int>{A[i],A[j]},vector<int>{B[x],B[y]});res%=mod;

                    }
                }
                C[i+1][j+1]=res;
                C[j+1][i+1]=-res;
            }
        }
        // cerr<<"++++++++++\n";
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++) {
                a[i][j]=C[i][j];
                // cerr<<a[i][j]<<" ";
            }//cerr<<endl;
        cout<<det(n)<<endl;
}
int mian(int flows){
    for(int i:A) bfs(i);
    // cerr<<"?? "<<k<<endl;
    // for(int i=1;i<=k;i++){
    //     for(int j=1;j<=k;j++){
    //         cerr<<dis[i][j]<<" ";
    //     }cerr<<endl;
    // }    
    // exit(0);
    if(flows==n){
        output();exit(0);
    }

    if(A.size()==flows&&B.size()==flows){
        vector<int>u1,u2;
        for(int z:A) u1.push_back(z);
        for(int z:B) u2.push_back(z);
        output();
        cerr<<"?? "<<val(u1,u2)*val(u1,u2)%mod<<endl;
        return val(u1,u2);
    }else{
        for(int s=0;s<(1<<A.size());s++){
            if(__builtin_popcount(s)==flows){
                curA.clear();
                for(int i=0;i<A.size();i++){
                    if((s>>i)&1) curA.push_back(A[i]);
                }
                dfs(0,-1,flows);
            }
        }
    }
    if(flows==n){
        output();
    }


    return ans%mod;
}

#undef int
}
void link(int x,int y){
    v[x].push_back(y);deg[y]++;
    P1::link(x,y,1);
}
int main(){
    cin>>n>>m>>k;
    A.resize(n);B.resize(m);
    for(int i=0;i<n;i++) cin>>A[i];
    for(int i=0;i<m;i++) cin>>B[i];
    for(int i=1;i<=k;i++){
        cin>>(str[i]+1);
        for(int j=1;j<=k;j++) str[i][j]-='0';   
    }
    int s=k*k*2+1,t=k*k*2+2;
    for(int i:A){
        link(s,id(1,i,0));
    }
    for(int i:B){
        link(id(k,i,1),t);
    }
    for(int i=1;i<k;i++){
        for(int j=1;j<=k;j++){
            if(str[i][j]&&str[i+1][j])
                link(id(i,j,1),id(i+1,j,0));
        }
    }
    for(int i=1;i<=k;i++){
        for(int j=1;j<k;j++){
            if(str[i][j]&&str[i][j+1])
                link(id(i,j,1),id(i,j+1,0));
        }
    }
    for(int i=1;i<=k;i++){
        for(int j=1;j<=k;j++){
            if(str[i][j]) link(id(i,j,0),id(i,j,1));
        }
    }
    int flows=P1::dinic(s,t);
    // exit(0);
    // if(flows==)
    cout<<flows<<" "<<P2::mian(flows)<<endl;
}
/*
2 2 4
1 2 
3 4
1110
1111
1111
0111
*/

Details

answer.code: In function ‘void P1::bfs()’:
answer.code:31:18: warning: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   31 |         for(auto [y,id]:v[x]){
      |                  ^
answer.code: In function ‘int P1::dfs(int, int)’:
answer.code:45:13: warning: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   45 |         auto[y,id]=v[x][i];
      |             ^
answer.code: In function ‘void P2::output()’:
answer.code:178:20: error: a function-definition is not allowed here before ‘{’ token
  178 | int mian(int flows){
      |                    ^
answer.code: In function ‘int P2::main()’:
answer.code:259:27: error: ‘mian’ is not a member of ‘P2’; did you mean ‘main’?
  259 |     cout<<flows<<" "<<P2::mian(flows)<<endl;
      |                           ^~~~
      |                           main
answer.code:260:1: warning: no return statement in function returning non-void [-Wreturn-type]
  260 | }
      | ^
answer.code: At global scope:
answer.code:260:2: error: expected ‘}’ at end of input
  260 | }
      |  ^
answer.code:69:13: note: to match this ‘{’
   69 | namespace P2{
      |             ^