构造。对边的权值排序,权值一样的话,在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. 关于stringWithFormat: - 两段NSString如何合成一段

    http://blog.sina.com.cn/s/blog_6b1e4a0601019pib.html str = [NSString stringWithFormat:@"%@,%@&q ...

  2. 在Windows上安装Python

    首先,从官网下载 最新版本 的Python 2.7.可通过 Python官网 的”Windows Installer”链接保证下载到的版本是最新的. Windows版本是MSI文件格式,双击它即可开始 ...

  3. php过滤函数

    addcslashes — 以 C 语言风格使用反斜线转义字符串中的字符 addslashes — 使用反斜线引用字符串 strip_tags — 从字符串中去除 HTML 和 PHP 标记 stri ...

  4. PHP图形处理函数试题

    一.问答题 1.取得当前安装的 GD 库的信息的函数是? 2.取得图像大小的函数是? 3.为一幅图像分配颜色 + alpha的函数是? 4.新建一个基于调色板的图像的函数是? 5.新建一个黑色图像的函 ...

  5. PHP xdebug的安装

    xdebug实际上就是PHP的一个第三方扩展 安装xdebug步骤和添加一个PHP扩展一样 linux:去xdebug官网下载对应版本的源码,然后像编译其他linux扩展一样,详解我的一篇关于Linu ...

  6. 关于Select * 与Select 字段名 的问题!

    [转]http://blog.csdn.net/tongyu2009/article/details/8252418 1.SELECT * 语句取出表中的所有字段,不论该字段的数据对调用的应用程序是否 ...

  7. Qt 5简介

    Qt 5简介 Qt 5概要介绍 在Qt 5这个版本中,Qt Quick成为了Qt的核心.但是Qt 5也继续提供了本地C++强大的功能来完成更好的用户体验,也提供了对OpenGL/OpenGL ES图形 ...

  8. Fatal error: Class ‘mysqli’ not found in解决办法

    在使用[$conn = new \mysqli($servername, $username, $password,$dbname);]连接msql数据库的时候 出现错误:[Fatal error: ...

  9. phpStorm 2016.1.2 最新版激活方法【亲测可用】

    测试日期:2016-07-29 下载地址:https://yunpan.cn/c6mWAGbExcyjf  访问密码 00fb 1.windows版本 菜单help >>>> ...

  10. jquery给html元素添加内容

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