标签:
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1137 Accepted Submission(s): 309
#include<cstdio> #include<cstring> #include<string> #include<cstdlib> #include<vector> using namespace std; int n,m,mp[15][15],root[15]; bool dfs(int x,int cnt) { if(cnt>m) return false; if(x==n) return true; for(int i=0;i<x;i++) { if(root[i]!=i) continue; bool flag=true; for(int j=i;j<x&&flag;j++) { if(root[j]==i) flag=mp[j][x]; } if(flag) { root[x]=i; if(dfs(x+1,cnt)) return true; root[x]=x; } } if(dfs(x+1,cnt+1)) return true; } int main() { int x,y; while(scanf("%d%d",&n,&m)!=EOF) { memset(mp,0,sizeof(mp)); for(int i=0;i<n;i++) { scanf("%d",&x); for(int j=0;j<x;j++) { scanf("%d",&y); mp[i][y]=1; //mp[y][i]=1; } } for(int i=0;i<n;i++) root[i]=i; if(dfs(0,0)||n<=m) printf("YES\n"); else printf("NO\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4443117.html