#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
int read()
{
int x;scanf("%d",&x);
return x;
}
ll lread()
{
ll x;scanf("%lld",&x);
return x;
}
//file head over
#define MaxC 5505
#define MaxN 505
int n,m,u[MaxC],d[MaxC],l[MaxC],r[MaxC],cnt;
int h[MaxN],s[MaxN];
int row[MaxN],col[MaxN];
void init()
{
for(int i = 1;i <= m;i++) r[i-1] = i,l[i] = i-1,u[i] = d[i] = i;
l[0] = m,r[m] = 0;
cnt = m;
return ;
}
void link(int x,int y)
{
++cnt;s[y]++;
d[cnt] = y,u[cnt] = u[y],d[u[y]] = u[y] = cnt;col[cnt] = y,row[cnt] = x;
if(h[x]) l[cnt] = l[h[x]],r[cnt] = h[x],r[l[h[x]]] = cnt,l[h[x]] = cnt;
else h[x] = l[cnt] = r[cnt] = cnt;
}
void remove(int y)//删除第y列
{
r[l[y]] = r[y],l[r[y]] = l[y];
for(int i = d[y];i != y;i = d[i])
{
for(int j = r[i];j != i;j = r[j])
{
s[col[j]]--;
u[d[j]] = u[j],d[u[j]] = d[j];
}
}
return ;
}
void resume(int y)//回复第y列(回溯操作)
{
l[r[y]] = r[l[y]] = y;
for(int i = d[y];i != y;i = d[i])
{
for(int j = r[i];j != i;j= r[j])
{
s[col[j]]++;
u[d[j]] = d[u[j]] = j;
}
}
}
stack<int>ans;
bool dfs(int now)
{
if(r[0] == 0) return 1;
int y = r[0];
for(int i = r[0];i != 0;i = r[i]) if(s[i] < s[y]) y = i;
remove(y);
for(int i = d[y];i != y;i = d[i])
{
ans.push(row[i]);
for(int j = r[i];j != i;j = r[j]) remove(col[j]);
if(!dfs(now+1))ans.pop();
else return 1;
for(int j = r[i];j != i;j = r[j]) resume(col[j]);
}
resume(y);
return 0;
}
int main()
{
n = read(),m = read();
init();
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= m;j++)
{
int t = read();
if(t) link(i,j);
}
}
dfs(1);
if(ans.empty()) puts("No Solution!");
else while(!ans.empty()) printf("%d ",ans.top()),ans.pop();
puts("");
return 0;
}
我把第一个点下下来,本地运行能过啊?