Description

Misha trains several ACM teams at the university. He is an experienced coach, and he does not underestimate the meaning of friendly and collaborative atmosphere during training sessions. It used to be that way, but one of the teams happened to win contests a little bit more often than others, and hence became slightly too big for their boots. That does not contribute to positive spirit which is essential for successful training. But Misha knows what to do!
Representatives of k teams participate in Misha’s training sessions, each team has three members. Alas, teams rarely attend en masse, but at least one member per team is always present, of course. During the next training session Misha is going to split everyone into npairs, so that each pair will include representatives of different teams. Players will play a mini-contest against each other in each pair.
A situation when no two mini-contests are won by representatives of one team is the one that suits Misha’s goals best. He may be somewhat cunning when selecting winner in each pair in order to achieve such situation. Find out whether Misha will succeed.

Input

The first line contains two numbers — n and k (1 ≤ n ≤ 10 5, 2 ≤ k ≤ 10 5). n lines follow. i-th of these contains two numbers x iy i (1 ≤ x iy i ≤ kx i ≠ y i) — the numbers of teams, whose representatives are in pair number i.
It is guaranteed that each team is represented in no less than one and no more than three pairs.

Output

If Misha is to succeed, output Yes in the first line. In each of the following n lines output one number — the number of team that is to win in the corresponding pair. If there are several answers, output any.
If Misha is to fail, output No in the only line.

Sample Input

input output
3 4
1 2
2 3
1 4
Yes
2
3
4
6 4
1 2
1 3
1 4
2 3
2 4
3 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的更多相关文章

  1. 【2-SAT】URAL - 2089 - Experienced coach

    题意:给出n对点a,b  要求从没对点中选出一个,且最终选出的点n个数不能存在相同的.输入数据满足每种数最多出现3次,最少出现1次 思路:第i对点的编号2*i, 2*i+1,   因为每个数最多出现3 ...

  2. Ural2089:Experienced coach(二分图匹配)

    Misha trains several ACM teams at the university. He is an experienced coach, and he does not undere ...

  3. URAL 2078~2089

    URAL 2078~2089 A - Bowling game 题目描述:给出保龄球每一局击倒的球数,按照保龄球的规则,算出总得分的最小值和最大值. solution 首先是最小值:每一局第一球击倒\ ...

  4. URAL 1873. GOV Chronicles

    唔 神题一道 大家感受一下 1873. GOV Chronicles Time limit: 0.5 secondMemory limit: 64 MB A chilly autumn night. ...

  5. 【HDU 2089】不要62

    http://acm.hdu.edu.cn/showproblem.php?pid=2089 数位dp,参照了打野的博客 预处理出f数组,f[i][j]表示第i位为数字j时的可行的数字总数. 对于区间 ...

  6. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  7. ural 2071. Juice Cocktails

    2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...

  8. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

  9. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

随机推荐

  1. 初试PHP单元测试TDD之安装PHPUnit

    东风吹战鼓擂,一年一度的校招季开始了,最为一名即将踏入社会的搬砖工,自然也闲不下来了.各种总结.恶补.面经在所难免.当遇见敏捷开发时,有点蒙了,这是什么东东,绝对不能吃!既然是一种软件开发的方式,听上 ...

  2. Boost正则表达式的编译与使用方法集

    下载boost 在boost官网上下载任何版本都可以www.boost.org . 将boost压缩包解压到D盘目录下 (我下载的是boost_1_54_0.zip),目录为D:\boost_1_54 ...

  3. GOPATH 使用总结

    GOPATH 环境变量用于指定这样一些目录:除 $GOROOT 之外的包含 Go 项目源代码和二进制文件的目录.go install 和 go 工具会用到 GOPATH:作为编译后二进制的存放目的地 ...

  4. ajax跨域解决方案2

    配置文件添加: <system.webServer>       <httpProtocol>        <customHeaders>          &l ...

  5. How to use Bundle&Minifier and bundleconfig.json in ASP.NET Core

    引言 我们在ASP.NET MVC 中经常会用到 bundleConfig.cs 文件来进行我们 css 和 js 的绑定, 那么在ASP.NET Core 中我们应该如何使用呢? 步骤一 在 Vis ...

  6. bzoj4691: Let There Be Light

    如果原点能被一个光源照到,那么这两个点之间一定没有任何球.我们可以通过三分距离来确定某线段和球是否有交点. 注意到m非常小,于是我们可以枚举原点被哪些光源照到.由于\(O(2^{n}*m)\)会超时, ...

  7. 小波包分解 仿真 matlab

    clc;close all;clear;fs = 100000;t = 1: 100;x = sin(2*pi*4000* t/fs) + sin(2*pi*40000*t/fs); %db8[Lo_ ...

  8. Selenium FF WebDriver 加载firebug 和设置代理

    首先这次使用的webDriver for Firefox的 由于项目的原因,需要在测试的时候加载Firebug和使用vpn,加载代理 Firefox 加载代理,可以从FF菜单上看,代理分为好几种 我这 ...

  9. MySQL索引背后的数据结构及算法原理【转】

    本文来自:张洋的MySQL索引背后的数据结构及算法原理 摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持 ...

  10. 关于目录路径path

    1.框架的目录结构 yii-1 .htaccess index.php protect -config -controllers -components 2.Linux 服务器 CentOs usr/ ...