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 n pairs, 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 ≤ 105, 2 ≤ k ≤ 105). n lines follow. i-th of these contains two numbers xiyi (1 ≤ xiyi ≤ kxi ≠ yi) — 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.

Samples

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
Problem Author: Alexander Ipatov, prepared by Egor Shchelkonogov

题意:有K个队伍,N次比赛。问如果教练来决定每次比赛谁赢谁输,是否可以找到一种方案,使得每支队伍最多赢一场。

思路:每一次比赛对两支队伍匹配连边,如果匹配次数等于N,则可行。

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn],To[maxn],cnt,N,M,T;
int linker[maxn],vis[maxn];
void add(int u,int v){
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
}
bool find(int u){
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(vis[v]!=T){
vis[v]=T;
if(!linker[v]||find(linker[v])){
linker[v]=u; return true;
}
}
}
return false;
}
int main()
{
int u,v,i,ans=;
scanf("%d%d",&N,&M);
for(int i=;i<=N;i++){
scanf("%d%d",&u,&v);
add(u,i); add(v,i);
}
for(i=;i<=M;i++){
T=i; if(find(i)) ans++;
}
if(ans<N) printf("No\n");
else {
printf("Yes\n");
for(i=;i<=N;i++) printf("%d\n",linker[i]);
}
return ;
}

Ural2089:Experienced coach(二分图匹配)的更多相关文章

  1. HDOJ 5093 Battle ships 二分图匹配

    二分图匹配: 分别按行和列把图展开.hungary二分图匹配. ... 例子: 4 4 *ooo o### **#* ooo* 按行展开. .. . *ooo o#oo oo#o ooo# **#o ...

  2. URAL 2089 Experienced coach Twosat

    Description Misha trains several ACM teams at the university. He is an experienced coach, and he doe ...

  3. UVA 12549 - 二分图匹配

    题意:给定一个Y行X列的网格,网格种有重要位置和障碍物.要求用最少的机器人看守所有重要的位置,每个机器人放在一个格子里,面朝上下左右四个方向之一发出激光直到射到障碍物为止,沿途都是看守范围.机器人不会 ...

  4. POJ 1274 裸二分图匹配

    题意:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶,告诉每头奶牛愿意产奶的牛棚编号,求出最多能分配到的牛栏的数量. 分析:直接二分图匹配: #include<stdio.h> #includ ...

  5. BZOJ1433 ZJOI2009 假期的宿舍 二分图匹配

    1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2375  Solved: 1005[Submit][Sta ...

  6. HDU1281-棋盘游戏-二分图匹配

    先跑一个二分图匹配,然后一一删去匹配上的边,看能不能达到最大匹配数,不能这条边就是重要边 /*----------------------------------------------------- ...

  7. HDU 1083 网络流之二分图匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...

  8. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

  9. BZOJ 1059 & 二分图匹配

    题意: 判断一个黑白染色的棋盘能否通过交换行或列使对角线上都是黑色. SOL: 真是有点醉...这种问题要么很神要么很水...第一眼感觉很水但就是不造怎么做...想了10分钟怎么感觉就是判断个数够不够 ...

随机推荐

  1. P2866 糟糕的一天

    糟糕的一天 洛谷链接 挺水的一道题,也就是看每个数后面有多少数比它小,最后把总数加起来. 我们可以用单调栈来实现,并用一个数组来保存每个数现在的状态. 代码: #include<cstdio&g ...

  2. GO 语言周报【七月第 1 期】

    TIOBE 七月排名 Go 进入前十 TIOBE 七月头条:Go 语言达到历史最高并进入前十.对于 Go 语言来说,这是一个里程碑时刻,我们可以更大胆地想象,它下一步的发展会达到怎样的高度.Go 是否 ...

  3. 【转载】js中对象的使用

    原文链接:http://www.jb51.net/article/90256.htm[侵删] 简单记录javascript中对象的使用 一.创建对象 //创建一个空对象 var o={}; //创建一 ...

  4. [Bzoj2733][Hnoi2012] 永无乡(BST)(Pb_ds tree)

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 4108  Solved: 2195[Submit][Statu ...

  5. java基础语法1

    一:基础语法之--标识符,修饰符,关键字 1.标识符: 定义:类名.变量名以及方法名都被称为标识符.自定义的名字. 注意: ·所有的标识符都应该以字母(A-Z或者a-z),美元符($).或者下划线(_ ...

  6. libevent API 介绍

    基本应用场景也是使用 libevnet 的基本流程,下面来考虑一个最简单的场景,使用livevent 设置定时器,应用程序只需要执行下面几个简单的步骤即可. 1)首先初始化 libevent 库,并保 ...

  7. 工作总结 使用html模板发邮件 前面空一大块

    HTML邮件的本质其实是发送了一个html页面.邮件的空白必然是页面的空白,所以你要找到你发送邮件的html模板所在,然后去掉空白即可,如果这是一个公共文件,需要注意你往往用的只是你的部分,很大程度还 ...

  8. HiWorkV1.3版震撼公布,今日起正式公开測试!

    今天HiWork迎来了公开測试和V1.3大版本号更迭,HiWork集成的机器人达到20种,未读消息提醒亦可从不同维度进行设置,不断变好真是件振奋人心的事儿呢. 在这个看重颜值(kan lian)的互联 ...

  9. Yii2 mongodb 扩展的where的条件增加大于 小于号

    1. mongodb的where中有比較丰富的 条件.例如以下: static $builders = [ 'NOT' => 'buildNotCondition', 'AND' => ' ...

  10. CodeForces 567C. Geometric Progression(map 数学啊)

    题目链接:http://codeforces.com/problemset/problem/567/C C. Geometric Progression time limit per test 1 s ...