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. K/3Cloud 分页报表示例参考

    分页报表首先需要实现的方法就是GetList,这个方法用来获得分页的条件. 其他的就和其他报表类似了. using System; using System.Collections.Generic; ...

  2. 最长链(codevs 1814)

    题目描述 Description 现给出一棵N个结点二叉树,问这棵二叉树中最长链的长度为多少,保证了1号结点为二叉树的根. 输入描述 Input Description 输入的第1行为包含了一个正整数 ...

  3. Codeforces 658B Bear and Displayed Friends【set】

    题目链接: http://codeforces.com/contest/658/problem/B 题意: 给定元素编号及亲密度,每次插入一个元素,并按亲密度从大到小排序.给定若干操作,回答每次询问的 ...

  4. zookeeper原理浅析(一)

    参考:https://www.cnblogs.com/leocook/p/zk_0.html 代码:https://github.com/littlecarzz/zookeeper 1. 什么是Zoo ...

  5. MongoDB学习day09--Mongoose数据校验

    一.Mongoose检验参数 required : 表示这个数据必须传入max: 用于 Number 类型数据, 最大值 min: 用于 Number 类型数据, 最小值 enum:枚举类型, 要求数 ...

  6. 巧用MySQL AHI加速神器,让你的InnoDB查询飞起来!

    DBAPLUS  http://mp.weixin.qq.com/s/cIjQIz-ZngSYJ3k2ZBBSsg

  7. ArcGIS10.x Engine直连提示连接超时ORA-12170 来自:http://www.iarcgis.com/?p=1004

    导语 随着Esri大力宣传直连,用户也越来越由服务连接,改为直连,当然ArcGIS Engine开发用户也不例外. 环境 Oracle数据库,ArcGIS版本不限,不过由于9版本多以服务连接,以10版 ...

  8. 第五讲:使用html5中的canvas动态画出物理学上平抛运动

    <html> <head> <title>平抛运动</title> <script src="../js/jscex.jscexRequ ...

  9. Eclipse中git插件导入远程库和上传项目源代码到远程库

    陆陆续续,从github,csdn的code.之前实习的小公司也是用git管理.发如今版本号控制方面确实比較方便.代码一敲完 . 自己由于完毕了新功能.加入一个新分支.然后提交上去,这就是程序猿一天干 ...

  10. vue Iframe

    1.Iframe.vue <!-- Iframe --> <template> <div> <!-- 标题栏 --> <mt-header tit ...