传送门:csdn抽风了,发不上link……
网上只有并查集神做法……看不懂,自己YY了半天:
先做两遍差分约束,算出每个的可能的最大最小值和任意两个之间的关系,枚举每一对数的每一种可能性,判断是否合法和结果是否唯一
Code:
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<queue> #include<vector> using namespace std; typedef pair<int,int> pii; vector<pii>G[51]; void add(int u,int v,int w){ G[u].push_back(pii(v,w)); } int d1[51],d2[51]; int mp[51][51]; int cmp[51][51]; int n; int Br; int vis[55]; int d[51][51]; void spfa(int s){ memset(vis,0,sizeof(vis)); memset(d[s],0x7f,sizeof(d[s]));Br=d[s][s]; queue<int>q;q.push(s);d[s][s]=0;vis[s]=1; while(!q.empty()){ int u=q.front();q.pop();vis[u]=0; for(int i=0;i<G[u].size();i++){ int v=G[u][i].first,w=G[u][i].second; if(d[s][v]>d[s][u]+w){ d[s][v]=d[s][u]+w; if(!vis[v]){ vis[v]=1; q.push(v); }}}}} void spfa1(){ memset(vis,0,sizeof(vis)); memset(d1,0xaf,sizeof(d1)); queue<int>q;q.push(0);d1[0]=1;vis[0]=1; while(!q.empty()){ int u=q.front();q.pop();vis[u]=0; for(int i=0;i<G[u].size();i++){ int v=G[u][i].first,w=G[u][i].second; if(d1[v]<d1[u]+w){ d1[v]=d1[u]+w; if(!vis[v]){ vis[v]=1; q.push(v); }}}}} void spfa2(){ memset(vis,0,sizeof(vis)); memset(d2,0x7f,sizeof(d2)); queue<int>q;q.push(0);d2[0]=3;vis[0]=1; while(!q.empty()){ int u=q.front();q.pop();vis[u]=0; for(int i=0;i<G[u].size();i++){ int v=G[u][i].first,w=G[u][i].second; if(d2[v]>d2[u]+w){ d2[v]=d2[u]+w; if(!vis[v]){ vis[v]=1; q.push(v); }}}}} int small(int a,int b){ if(d[a][b]>0)return 1; if(d[a][b]==0)return 0; return -1; } bool ok(int A,int B,int i,int j,int da,int db,int di,int dj){ //check A,B if(d[A][B]!=Br){ if(da>db&&d[A][B]>=0)return false; if(da==db&&d[A][B]!=0)return false; if(da<db&&d[A][B]<=0)return false; } //check A,i if(d[A][i]!=Br){ if(da>di&&d[A][i]>=0)return false; if(da==di&&d[A][i]!=0)return false; if(da<di&&d[A][i]<=0)return false; } //check A,j if(d[A][j]!=Br){ if(da>dj&&d[A][j]>=0)return false; if(da==dj&&d[A][j]!=0)return false; if(da<dj&&d[A][j]<=0)return false; } //check B,i if(d[B][i]!=Br){ if(db>di&&d[B][i]>=0)return false; if(db==di&&d[B][i]!=0)return false; if(db<di&&d[B][i]<=0)return false; } //check B,j if(d[B][j]!=Br){ if(db>dj&&d[B][j]>=0)return false; if(db==dj&&d[B][j]!=0)return false; if(db<dj&&d[B][j]<=0)return false; } //check i,j if(d[i][j]!=Br){ if(di>dj&&d[i][j]>=0)return false; if(di==dj&&d[i][j]!=0)return false; if(di<dj&&d[i][j]<=0)return false; } return true; } int Cmp(int A,int B,int i,int j){ int c1=0,c2=0,c3=0; for(int I=d1[A];I<=d2[A];I++) for(int J=d1[B];J<=d2[B];J++) for(int K=d1[i];K<=d2[i];K++) for(int L=d1[j];L<=d2[j];L++){ if(ok(A,B,i,j,I,J,K,L)){ if(I+J>K+L)c1++; if(I+J==K+L)c2++; if(I+J<K+L)c3++; } } if(c1&&!c2&&!c3)return 1; if(!c1&&c2&&!c3)return 0; if(!c1&&!c2&&c3)return -1; return 2; } int A,B; int main(){ scanf("%d%d%d",&n,&A,&B); for(int i=1;i<=n;i++){ char str[55]; scanf("%s",str+1); for(int j=1;j<=n;j++){ if(str[j]=='+')mp[i][j]=1; if(str[j]=='-')mp[i][j]=2; if(str[j]=='=')mp[i][j]=3; } } for(int i=1;i<=n;i++) add(0,i,0); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ if(mp[i][j]==1)add(j,i,1); if(mp[i][j]==2)add(i,j,1); if(mp[i][j]==3)add(i,j,0),add(j,i,0); } spfa1(); for(int i=1;i<=n;i++) spfa(i); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(d[i][j]!=Br&&i!=j) d[j][i]=-d[i][j]; for(int i=1;i<=n;i++)G[i].clear(); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ if(mp[i][j]==1)add(i,j,-1); if(mp[i][j]==2)add(j,i,-1); if(mp[i][j]==3)add(i,j,0),add(j,i,0); } spfa2(); int c1=0,c2=0,c3=0; for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++){ if(i==A||i==B||j==A||j==B)continue; int opt=Cmp(A,B,i,j); if(opt==1)c1++; if(opt==0)c2++; if(opt==-1)c3++; } printf("%d %d %d\n",c1,c2,c3); return 0; }