Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
29415 | LYLAKIOI | 【BJ】T2 | C++ | 解答错误 | 40 | 199 MS | 2628 KB | 1594 | 2024-05-07 08:35:09 |
#include<bits/stdc++.h> #define up(i,l,r) for(int i=(l);i<=(r);++i) #define down(i,l,r) for(int i=(l);i>=(r);--i) #define p_b push_back #define m_p make_pair #define pi pair<int,int> #define p1 first #define p2 second using namespace std; typedef long long ll; const int maxn=1e5+10,mod=1e9+7; inline ll read(){ ll x=0;short t=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')t=-1;ch=getchar();} while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar(); return x*t; }int n,m,vis[maxn],flg[maxn],nd,res=233; vector<int>v[maxn]; bool fail; bool dfs(int u,int fa){ if(vis[u])return 1; vis[u]=1;for(int x:v[u])if(x!=fa&&dfs(x,u))return 1; vis[u]=0;return 0; } bool dfs2(int u,int fa){ flg[u]=1; int ct=0; for(int x:v[u])if(x!=fa&&dfs2(x,u))++ct; if(ct>2)fail=1; if(!ct)return 1; return 0; } bool chk(){ up(i,1,n+m+nd)flg[i]=0; fail=0; up(i,1,n+m)if(!flg[i])dfs2(i,0); return (!fail); } void dfs3(int u){ if(u>n+m){ if(chk())res=min(res,nd);return; } dfs3(u+1); ++nd;v[u].p_b(nd+n+m),v[nd+n+m].p_b(u); dfs3(u+1); v[u].pop_back(),v[nd+n+m].pop_back();--nd; } void slv(){ n=read(),m=read(); up(i,1,n){ string s;cin>>s;s=" "+s; up(j,1,m)if(s[j]=='#')v[i].p_b(n+j),v[n+j].p_b(i); }up(i,1,n+m)if(dfs(i,0)){ printf("-1\n");return; } dfs3(1); if(res==233)res=-1; cout<<res; }int main(){ // freopen("b.in","r",stdin); slv(); // cerr<<clock()*1.0/CLOCKS_PER_SEC<<"s\n"; return 0; }