lightoj 1251 (Two_Sat)
- #include<cstdio>
- #include<cstring>
- #include<iostream>
- #include<cmath>
- #include<algorithm>
- #include<queue>
- #include<vector>
- using namespace std;
- const int maxn = ;
- const int INF = 0x3f3f3f;
- int n,m;
- char a[maxn][];
- int b[maxn][];
- struct TwoSat{
- int n;
- vector<int> G[maxn*];
- bool mark[*maxn];
- int s[*maxn],cnt;
- void init(int n){
- this->n = n;
- memset(mark,,sizeof(mark));
- for(int i=;i<=*n;i++) G[i].clear();
- }
- void add_clause(int u,int uval,int v,int vval){
- u = u* + uval; //u,v有矛盾;
- v = v* + vval;
- G[u].push_back(v^);
- G[v].push_back(u^);
- }
- bool dfs(int u){
- if(mark[u^]) return false;
- if(mark[u]) return true;
- mark[u] = true;
- s[cnt++] = u; //记录你所选择的路径;
- for(int i=;i<G[u].size();i++ ){
- if( !dfs(G[u][i]) ) return false;
- }
- return true;
- }
- bool solve(){
- for(int i=;i<*n;i+=){
- if(!mark[i] && !mark[i+]){
- cnt = ;
- if(!dfs(i)){
- while(cnt > ) mark[s[--cnt]] = false;
- if(!dfs(i+)) return false;
- }
- }
- }
- return true;
- }
- void print(){
- int ans[maxn],pv = ;
- for(int i=;i<*n;i+=){
- if(mark[i] && a[i/][] == '+'){
- ans[pv++] = b[i/][];
- }
- else if(mark[i+] && a[i/][] == '+'){
- ans[pv++] = b[i/][];
- }
- }
- sort(ans,ans+pv);
- int num = unique(ans,ans+pv) - ans;
- printf("%d",num);
- for(int i=;i<num;i++) printf(" %d",ans[i]);
- printf("\n");
- }
- }solver;
- int main()
- {
- //freopen("E:\\acm\\input.txt","r",stdin);
- int T;
- cin>>T;
- for(int t=;t<=T;t++){
- scanf("%d %d",&n,&m);
- solver.init(n);
- vector<int> p1[],p2[],n1[],n2[];
- for(int i=;i<n;i++){
- int temp;
- scanf("%c%c%d%c%c%d",&temp,&a[i][],&b[i][],&temp,&a[i][],&b[i][]);
- if(a[i][] == '+'){
- p1[b[i][]].push_back(i);
- p2[b[i][]].push_back();
- }
- else{
- n1[b[i][]].push_back(i);
- n2[b[i][]].push_back();
- }
- if(a[i][] == '+'){
- p1[b[i][]].push_back(i);
- p2[b[i][]].push_back();
- }
- else{
- n1[b[i][]].push_back(i);
- n2[b[i][]].push_back();
- }
- }
- for(int i=;i<=m;i++){
- for(int j=;j<p1[i].size();j++)
- for(int k=;k<n1[i].size();k++){
- solver.add_clause(p1[i][j],p2[i][j],n1[i][k],n2[i][k]);
- }
- }
- if(solver.solve()){
- printf("Case %d: Yes\n",t);
- solver.print();
- }
- else printf("Case %d: No\n",t);
- }
- }
题目链接:http://lightoj.com/volume_showproblem.php?problem=1251
lightoj 1251 (Two_Sat)的更多相关文章
- 2-Sat+输出可行解(个人模版)
2-Sat+输出可行解: //LightOJ 1251 #include<stdio.h> #include<string.h> #include<vector> ...
- BZOJ 1251: 序列终结者 [splay]
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 3778 Solved: 1583[Submit][Status][Discu ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- LightOj 1298 - One Theorem, One Year(DP + 欧拉)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1298 题意:给你两个数 n, p,表示一个数是由前 k 个素数组成的,共有 n 个素数 ...
- hduoj 1251 统计难题
http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- 1214 - Large Division -- LightOj(大数取余)
http://lightoj.com/volume_showproblem.php?problem=1214 这就是一道简单的大数取余. 还想还用到了同余定理: 所谓的同余,顾名思义,就是许多的数被一 ...
- LightOJ Beginners Problems 部分题解
相关代码请戳 https://coding.net/u/tiny656/p/LightOJ/git 1006 Hex-a-bonacci. 用数组模拟记录结果,注意取模 1008 Fibsieve's ...
随机推荐
- INSERT INTO SELECT FROM 这语句怎么用
如果两表字段相同,则可以直接这样用. insert into table_a select * from table_b 如果两表字段不同,a表需要b中的某几个字段即可,则可以如下使用: insert ...
- iOS远程消息推送
iOS 推送基础知识 Apple 使用公共密钥数字证书对来自 iOS 应用程序的推送请求进行身份验证,所以您首先需要创建身份验证密钥,并向 Apple 注册它们.我将在下一节中花相当长的篇幅来直接介绍 ...
- 十七、C# 反射、特性和动态编程
反射.特性和动态编程 1.访问元数据 2.成员调用 3.泛型上的反射 4.自定义特性 5.特性构造器 6.具名参数 7.预定义特性 8.动态编程 特性(attribute)是在一个程序集中插入 ...
- sicily-2499 平方数
题目分析: 一个数可以表示成四种状态,所以可以用一个状态数组来存放该数由几个数的平方和表示.1.表示该数本身是完全平方.2.表示该数是由两个平方和3.表示三个.4.表示4个.一次遍历找出本身是完全平方 ...
- SGU 165.Basketball
题意 输入n个在[1.95,2.05]范围内的数. 保证他们的平均数为2.00. 现在要求把这些数调整出一个顺序, 使得任意长度为K的子段和与2.00* ...
- java数据类型学习
java数据类型基本分为两类: 一类为基本数据类型: 数值类型: 整数类型:byte.short.int.long 浮点类型:float.double 字符类型:char 布尔类型:boolean 一 ...
- 关于fork( )函数父子进程返回值的问题
fork()是linux的系统调用函数sys_fork()的提供给用户的接口函数,fork()函数会实现对中断int 0x80的调用过程并把调用结果返回给用户程序. fork()的函数定义是在init ...
- Bootstrap_表单_表单提示信息
平常在制作表单验证时,要提供不同的提示信息.在Bootstrap框架中也提供了这样的效果.使用了一个"help-block"样式,将提示信息以块状显示,并且显示在控件底部. < ...
- 新一代的代码编辑神器Sublime Text 3(使用指南)
首先附上官网下载链接:http://www.sublimetext.com/3 接下来是安装sublime最强大的插件功能:Package Control 一.简单的安装方法 使用Ctrl+`快捷键或 ...
- BZOJ 1507 Editor
Description Input 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中可能会插入一些回车符,请忽略掉它 ...