Almost Fault-Tolerant Database
题意:求一个数组与给出的所有数组里相同位置下最多2个不同 若不能 输出NO
思路:以第一个数组为模板,改第一个数组,最多改2个元素,暴力dfs ,遇到相差>=5的数组一定不行。
#include<bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int inf=2e18+100;
const int maxn=3e5+100;
vector<int>g[maxn];
bool fg;
int n,m;
bool dfs(int x)//x为已经修改的个数
{
if(x>2)return 0;
for(int i=1;i<n;i++)
{
vector<int>tmp;
for(int j=0;j<m;j++)
{
if(g[0][j]!=g[i][j])tmp.push_back(j);
}
int cnt=tmp.size();
if(cnt<=2)continue;
else if(cnt>=5){return 0;}
else
{
for(auto it:tmp)
{
int tp=g[0][it];
g[0][it]=g[i][it];
if(dfs(x+1))return 1;
g[0][it]=tp;
}
return 0;
}
}
return 1;
}
signed main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
int x;
cin>>x;
g[i].push_back(x);
}
}
fg=dfs(0);
if(fg)
{
cout<<"Yes"<<"\n";
for(auto it:g[0])cout<<it<<" ";
cout<<"\n";
}
else
{
cout<<"No"<<"\n";
}
}