构造。对边的权值排序,权值一样的话,在MST中的边排到前面,否则权值小的排在前面。

然后边一条一条扫过去,如果是1 ,那么连一个点到集合中,如果是0,集合内的边相连。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; const int maxn=+;
struct Edge
{
int u,v;
int w;
int info;
int id;
} e[maxn];
int n,m;
int flag;
int last[maxn];
int K;
int now; bool cmp(const Edge&a,const Edge&b)
{
if(a.w==b.w) return a.info>b.info;
return a.w<b.w;
} bool cmp2(const Edge&a,const Edge&b)
{
return a.id<b.id;
} void init()
{
now=;
flag=;
K=;
for(int i=; i<=n; i++) last[i]=i;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=; i<=m; i++)
{
scanf("%d%d",&e[i].w,&e[i].info);
e[i].id=i;
}
sort(e+,e+m+,cmp); for(int i=; i<=m; i++)
{
if(e[i].info==)
{
e[i].u=;
now++;
e[i].v=now; K=;
}
else if(e[i].info==)
{
while()
{
if(K>=now+)
{
printf("-1\n");
flag=;
break;
}
if(last[K]+<=now)
{
e[i].u=K;
e[i].v=last[K]+;
last[K]++;
break;
}
else K++;
}
}
if(flag) break;
} sort(e+,e+m+,cmp2); if(flag==)
{
for(int i=; i<=m; i++)
printf("%d %d\n",e[i].u,e[i].v);
}
}
return ;
}

CodeForces 605B Lazy Student的更多相关文章

  1. 605B. Lazy Student(codeforces Round 335)

    B. Lazy Student time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  2. codeforce 605B. Lazy Student

    题意:n点,m条边.m条边里面标记为1的最小生成树的边,0为非最小生成树的边.给了每条边的权,如果能构成一个最小生成树则输出图,否则-1. 思路:先按权值小,为生成数边的顺序排序.(根据kruskal ...

  3. Codeforces Round #335 (Div. 2) D. Lazy Student 构造

    D. Lazy Student Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/606/probl ...

  4. Codeforces Round #335 (Div. 2) D. Lazy Student 贪心+构造

    题目链接: http://codeforces.com/contest/606/problem/D D. Lazy Student time limit per test2 secondsmemory ...

  5. Codeforces Round #335 (Div. 2) D. Lazy Student 贪心

    D. Lazy Student   Student Vladislav came to his programming exam completely unprepared as usual. He ...

  6. CF#335 Lazy Student

    Lazy Student time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. 【22.73%】【codeforces 606D】Lazy Student

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. cf 605B B. Lazy Student 构造 好题

    题意: 一个n个节点的图,有m条边,已知这个图的一个mst 现在如果我们知道这个图的m条边,和知道mst的n-1条边是哪些,问能不能构造出一个满足条件的图 思路:排序+构造 数组deg[i]表示节点i ...

  9. codeforces 14A - Letter & codeforces 859B - Lazy Security Guard - [周赛水题]

    就像title说的,是昨天(2017/9/17)周赛的两道水题…… 题目链接:http://codeforces.com/problemset/problem/14/A time limit per ...

随机推荐

  1. 谷歌浏览器web开发教程之开始篇:使用sublime

    你的代码编辑器是主要的开发工具:你使用它去编辑和保存代码段.你可以通过学习编辑器快捷键和以及安装关键插件来好而快的写出代码. 目录 安装sublime文本编辑器 为什么使用包管理器? 安装插件 摘要 ...

  2. jquery给html元素添加内容

    append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 after() - 在被选元素之后插入内容 before() - 在被选元素之前插入内容 实例 $(& ...

  3. c printf()详解[转载]

    ref : http://www.cnblogs.com/yuaqua/archive/2011/10/21/2219856.html #include <cstdio> #include ...

  4. Creating your own header file in C

    终于跑起来了,含自定义 include .h 的c语言程序,超开心呀! header files contain prototypes for functions you define in a .c ...

  5. zTree异步加载并初始化树时全部展开(贴主要代码)

    <%@page pageEncoding="UTF-8"%> <%@include file="/commons/include/html_doctyp ...

  6. 闭包 -> map / floatMap / filter / reduce 浅析

    原创: 转载请注明出处 闭包是自包含的函数代码块,可以在代码中被传递和使用 闭包可以捕获和存储其所在上下文中任意常量和变量的引用.这就是所谓的闭合并包裹着这些常量和变量,俗称闭包.Swift 会为您管 ...

  7. 快学Scala-第八章 继承

    知识点: 1.扩展类 extends关键字,在定义中给出子类需要而超类没有的字段和方法,或者重写超类的方法. 2.重写方法 在Scala中重写一个非抽象方法必须 override 修饰符 public ...

  8. Android中ViewPager如何设置不能通过屏幕左右滑动来切换页面

    //很多时候,我想禁止用户通过屏幕的左右滑动来切换界面!如何实现! //创建一个类继承viewpager,实现 onTouchEvent   和   onInterceptTouchEvent方法,都 ...

  9. 2--OC -- 类的创建与实例化

    2.OC -- 类的创建与实例化   一.OC类的简述 1.OC类分为2个文件:.h文件用于类的声明,.m文件用于实现.h的函数: 2.类是声明使用关键字:@interface.@end : 3.类是 ...

  10. Bootstrap Modal 垂直居中

    Bootstrap 的 modal 正文中如果内容较少的话,并不会垂直居中,而是偏上, 如果想要达到垂直居中的效果,需要自动动手了. 可以在初始显示时设置垂直居中,可以这样做: $('#YourMod ...