URAL 2089 Experienced coach Twosat
Description
Input
Output
Sample Input
input | output |
---|---|
3 4 |
Yes |
6 4 |
No |
题意:给出n对点a,b 要求从没对点中选出一个,且最终选出的点n个数不能存在相同的。输入数据满足每种数最多出现3次,最少出现1次
思路:第i对点的编号2*i, 2*i+1, 因为每个数最多出现3次,那么完全可以枚举每个数,然后相同的数之间的编号连一条边,表示这两个编号不能同时选,这样跑完twosat就能得到一个满足情况的解或无解。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdlib>
#include <map>
#include <set>
#include <cmath>
using namespace std;
const int N = 2e6 + ;
struct Edge {
int to, nex;
}e[N];
int head[N], tot;
void init() {
tot = ; memset(head, -, sizeof head);
}
void add(int u, int v) {
e[tot].to = v;
e[tot].nex = head[u];
head[u] = tot++;
}
int Low[N], DFN[N], Stack[N], Belong[N];
int Index, top;
int scc;
bool Instack[N];
int num[N]; void Tarjan(int u) {
int v;
Low[u] = DFN[u] = ++Index;
Stack[top++] = u;
Instack[u] = true; for(int i = head[u]; ~i; i = e[i].nex) {
v = e[i].to;
if(!DFN[v]) {
Tarjan(v);
if(Low[u] > Low[v]) Low[u] = Low[v];
}else if(Instack[v] && Low[u] > DFN[v]) Low[u] = DFN[v];
}
if(Low[u] == DFN[u]) {
scc++;
do
{
v = Stack[--top];
Instack[v] = false;
Belong[v] = scc;
num[scc]++;
}while(v != u);
}
}
bool solvable(int n) {
memset(DFN, , sizeof DFN);
memset(Instack, false, sizeof Instack);
memset(num, , sizeof num);
Index = scc = top = ;
for(int i = ; i < n; ++i) if(!DFN[i]) Tarjan(i); for(int i = ; i < n; i += ) {
if(Belong[i] == Belong[i ^ ]) return false;
}
return true;
} queue<int> q1, q2;
vector<vector<int> >dag;
int vis[N];
int indeg[N];
int cf[N];
void solve(int n) {
dag.assign(scc+, vector<int>());
memset(indeg, , sizeof indeg);
memset(vis, , sizeof vis);
for(int u = ; u < n; ++u) {
for(int i = head[u]; ~i; i = e[i].nex) {
int v = e[i].to;
if(Belong[u] != Belong[v]) {
dag[ Belong[v] ].push_back(Belong[u]);
indeg[ Belong[u] ]++;
}
}
}
for(int i = ; i < n; i += ) {
cf[ Belong[i] ] = Belong[i ^ ];
cf[ Belong[i ^ ] ] = Belong[i];
}
while(!q1.empty()) q1.pop();
while(!q2.empty()) q2.pop();
for(int i = ; i <= scc; ++i) if(indeg[i] == ) q1.push(i); while(!q1.empty()) {
int u = q1.front();
q1.pop();
if(vis[u] == ) {
vis[u] = ;
vis[ cf[u] ] = ;
}
int sz = dag[u].size();
for(int i = ; i < sz; ++i) {
indeg[ dag[u][i] ]--;
if(indeg[ dag[u][i] ] == ) q1.push(dag[u][i]);
}
}
}
int r[N];
vector<int> g[N];
int main() {
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int n, m;
while(~scanf("%d%d", &m, &n)) {
init();
int tot = ; int u, v;
for(int i = ; i < m; ++i) {
scanf("%d%d", &u, &v);
r[tot++] = u;
r[tot++] = v;
g[u].push_back( * i);
g[v].push_back( * i + );
} for(int i = ; i <= n; ++i) {
int sx = g[i].size();
for(int j1 = ; j1 < sx; ++j1) {
for(int j2 = j1 + ; j2 < sx; ++j2) {
int v1 = g[i][j1];
int v2 = g[i][j2];
add(v1, v2 ^ );
add(v2, v1 ^ );
}
}
}
if(solvable( * m)) {
solve( * m);
puts("Yes");
for(int i = ; i < m; ++i) {
if(vis[ Belong[ * i] ]) printf("%d\n", r[ * i + ]);
else printf("%d\n", r[ * i]);
}
}else puts("No");
}
return ;
}
URAL 2089 Experienced coach Twosat的更多相关文章
- 【2-SAT】URAL - 2089 - Experienced coach
题意:给出n对点a,b 要求从没对点中选出一个,且最终选出的点n个数不能存在相同的.输入数据满足每种数最多出现3次,最少出现1次 思路:第i对点的编号2*i, 2*i+1, 因为每个数最多出现3 ...
- Ural2089:Experienced coach(二分图匹配)
Misha trains several ACM teams at the university. He is an experienced coach, and he does not undere ...
- URAL 2078~2089
URAL 2078~2089 A - Bowling game 题目描述:给出保龄球每一局击倒的球数,按照保龄球的规则,算出总得分的最小值和最大值. solution 首先是最小值:每一局第一球击倒\ ...
- URAL 1873. GOV Chronicles
唔 神题一道 大家感受一下 1873. GOV Chronicles Time limit: 0.5 secondMemory limit: 64 MB A chilly autumn night. ...
- 【HDU 2089】不要62
http://acm.hdu.edu.cn/showproblem.php?pid=2089 数位dp,参照了打野的博客 预处理出f数组,f[i][j]表示第i位为数字j时的可行的数字总数. 对于区间 ...
- 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome
题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...
- ural 2071. Juice Cocktails
2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...
- ural 2073. Log Files
2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...
- ural 2070. Interesting Numbers
2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...
随机推荐
- [从产品角度学EXCEL 01]-EXCEL是怎样运作的
这是<从产品角度学EXCEL>系列第二篇. 前言请看:从产品角度学EXCEL-系列0-为什么要关注EXCEL的本质 本文不接受无授权转载,如需转载,请先联系我,非常感谢. 1.EXCEL是 ...
- MySql简易配置
选择standard configuration ,然后next Service Name :服务名字 Launch the MySQL Server automatically:是否开机启动mysq ...
- Sublime Text 3 Plugin Better!
Package Control Cmake ConvertUTF Markdown preview MarkdownEditing Marking Changed Rows
- Azure底层架构的初步分析
之所以要写这样的一篇博文的目的是对于大多数搞IT的人来说,一般都会对这个topic很感兴趣,因为底层架构直接关乎到一个公有云平台的performance,其实最主要的原因是我们的客户对此也非常感兴趣, ...
- centos7 使用updatedb和locate命令
centos7默认是没有安装mlocate的,所以无法使用这两个命令 yum install mlocate 就可以了 参考:https://fedorahosted.org/mlocate/
- powershell例子
例子如下: $ErrorActionPreference="Stop" function getlist{ ls D:\tmp2|select name,extension,ful ...
- Git的用法
Git的用法 Git 的也可以理解为版本控制器.版本控制器(维基的解释):维护工程蓝图的标准作法,能追踪工程蓝图从诞生一直到定案的过程.此外,版本控制也是一种软件工程技巧,借此能在软件开发的过程中,确 ...
- jQuery根据name取input值问题
最近做项目用$("input[name=inputName]").val();去取input值得时候发现取值有问题:总是取第一次输入的值,如果在同一个页面不发生表单提交,然后将in ...
- Web项目使用Oracle.DataAccess.dll 类库连接oracle数据库
首先我用的工具是oracle 32位免安装版+Oracle.DataAccess.dll 32位 文件版本4.121.1.0+vs2013 +win7 64位 Oracle.DataAccess.d ...
- spring定时任务详解(@Scheduled注解)( 转 李秀才的博客 )
在springMVC里使用spring的定时任务非常的简单,如下: (一)在xml里加入task的命名空间 xmlns:task="http://www.springframework.or ...