• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

HDU 4751 Divide Groups 2-SAT(2013 ACM/ICPC Asia Regional Nanjing Online 1004)

彩笔请多指教!

转载请注明出处__谢谢! http://www.cnblogs.com/HaibaraAi/articles/3336673.html by---HaibaraAi 求认识大神(ˇ^ˇ),目前只会水题- -#~~~!
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751
数据略水(︶︿︶)据说直接dfs更快了
 
 1 #include <queue>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 #define INF 0x7fffffff
 7 #define maxn 105
 8 int n,m,ans;
 9 int c[maxn];
10 int mp[maxn][maxn];
11 int bfs(int u){
12     queue<int>q;
13     while(!q.empty()){q.pop();}
14     q.push(u);
15     while(!q.empty()){
16         u=q.front();q.pop();
17         for(int i=0;i<n;i++){
18             if(u==i||mp[u][i]&&mp[i][u])continue;
19             if(c[i]==-1){
20                 c[i]=c[u]^1;
21                 q.push(i);
22             }
23             else if(c[i]==c[u])return 1;
24         }
25     }
26     return 0;
27 }
28 int main(){
29     while(~scanf("%d",&n)){
30         memset(mp,0,sizeof mp);
31         memset(c,-1,sizeof c);
32         int i;
33         for(i=0;i<n;i++){
34             int v;
35             while(scanf("%d",&v)&&v)
36                 mp[i][v-1]=1;
37         }
38         for(i=0;i<n;i++){
39             if(c[i]==-1){
40                 c[i]=0;
41                 if(bfs(i))break;
42             }
43         }
44         if(i<n)printf("NO\n");
45         else printf("YES\n");
46     }
47     return 0;
48 }
View Code 2013-09-24 12:55:26 
posted @ 2013-09-24 12:56  HaibaraAi  阅读(106)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3