QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#382074 | #7648. 网格图最大流计数 | ooooxxxx | 0 | 2315ms | 69824kb | C++14 | 6.3kb | 2024-04-08 01:57:08 | 2024-04-08 01:57:10 |
Judging History
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
*/
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 39108kb
input:
7 7 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1111111 1111111 1111111 1111111 1111111 1111111 1111111
output:
7 0 0 0 0 0 0 0
result:
wrong answer 2nd numbers differ - expected: '1', found: '0'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Wrong Answer
Test #31:
score: 0
Wrong Answer
time: 2315ms
memory: 69824kb
input:
73 73 400 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 ...
output:
73 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
wrong answer 2nd numbers differ - expected: '849796347', found: '0'
Subtask #6:
score: 0
Skipped
Dependency #5:
0%
Subtask #7:
score: 0
Skipped
Dependency #1:
0%