构造。对边的权值排序,权值一样的话,在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. [Java]知乎下巴第0集:让我们一起来做一个知乎爬虫吧哦耶【转】

    文章来源:http://jp.51studyit.com/article/details/16203.htm 作者:  汪海洋 身边的小伙伴们很多都喜欢刷知乎,当然我也不例外, 但是手机刷太消耗流量, ...

  2. Git中的merge命令实现中出现问题及其解决

    Git中的merge命令实现和工作方式 2015年8月17日星期一 丹丹 git代码在合并两个分支的时候总是会出现一下的错误提示,不能正常的完成合并分支,错误提示如图所示: 但是在其他的终端是可以完成 ...

  3. 用GDB调试程序的设置 Segmentation fault(Core Dump)调试

    在写wifi库的时候碰见一个 Segmentation fault(Core Dump) 所以需要用GDB调试下. 在cmake的时候,修改CMakeLists.txt set(CMAKE_C_FLA ...

  4. Loadrunner之文件的上传(八)

    老猪提供: https://mp.weixin.qq.com/s?__biz=MzIwOTMzNDEwNw==&mid=100000013&idx=1&sn=624f5bc74 ...

  5. Android中获取网络数据时的分页加载

    //此实在Fragment中实现的,黄色部分为自动加载,红色部分是需要注意的和手动加载,    蓝色部分是睡眠时间,自我感觉不用写  ,还有就是手动加载时,不知道为什么进去后显示的就是最后一行,求大神 ...

  6. c语言判断打开文件是否为空的方法

    void writeReslut2(char* caseName,double averageTime,double max, double min,int loops,int size){ fpos ...

  7. 在vim下,实现nesC语句的高亮

    默认的vim没有支持nesC语法高亮,给阅读源码带来不便.不过可以通过装NesC Syntax Highlighting插件来解决这个问题,具体操作如下: 步骤一:下载插件 在http://www.v ...

  8. POJ 1236 Network of Schools(tarjan算法 + LCA)

    这个题目网上有很多答案,代码也很像,不排除我的.大家的思路应该都是taijan求出割边,然后找两个点的LCA(最近公共祖先),这两个点和LCA以及其他点构成了一个环,我们判断这个环上的割边有几条,我们 ...

  9. Linux + Apache + PHP 环境搭建

    搭建环境: Ubuntu 15.04 Apache 2.4.16 PHP 5.6.15 1 安装Apache 先安装依赖程序(都安装在 /usr/local/ 目录下) apr-1.5.2.tar.g ...

  10. Git 使用初体验

    http://my.oschina.net/moooofly/blog/228608 很久之前在 http://git.oschina.net/ 上创建了一个私有项目 modb ,目的主要是用来学习如 ...