构造。对边的权值排序,权值一样的话,在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. 在 .NET 4 中使用托管可扩展性框架构建可组合的应用程序

    https://msdn.microsoft.com/zh-cn/library/ff576068.aspx http://blogs.msdn.com/b/nblumhardt/archive/20 ...

  2. perl命令+关键字

    来源: http://www.cnblogs.com/itech/archive/2012/11/01/2749666.html http://www.garybeene.com/vb/tut-per ...

  3. 禁止root远程登录 sshd问题:A protocol error occurred. Change of username or service not allowed

    在研究Linux安全的时候遇到一个问题,原本打算修改linux直接远程root登陆,修改为sshd的配置文件后 Nano /etc/ssh/sshd_config 把#PermitRootLogin ...

  4. window窗体程序意外崩溃,EventType clr20r3错误的解决方法

    EventType clr20r3, P1 ggreadcard.exe, P2 1.0.0.0, P3 51d3d283, P4 zljy.common, P5 1.0.0.0, P6 4fc6c2 ...

  5. UIView的基本属性及ANimation

    frame属性:可以使用该属性改变尺寸和位置 相对于父视图bounds:改变尺寸 相对自身center:改变视图的位置alpha:改变视图的透明度backgroundColor:改变视图的背景cont ...

  6. 全文检索工具推荐FileLocator

    全文检索工具推荐FileLocator https://www.baidu.com/link?url=_vaDZaJ_OePrAX-BTUD5hjTymnvN7_1oIAnWyS25hqxAg0nUH ...

  7. win10怎么启用网络发现,网络发现已关闭怎么办

    脑和电脑之间传输文件的方式很多,其中一种就是使用局域网,在网络中我们的电脑应该可以被其他电脑发现是非常方便使用文件共享的,尤其是在使用家庭组网络的时候,那么win10里面怎么启用网络发现呢? 工具/原 ...

  8. char与varchar、nvarchar区别

    char     char是定长的,也就是当你输入的字符小于你指定的数目时,char(8),你输入的字符小于8时,它会再后面补空值.当你输入的字符大于指定的数时,它会截取超出的字符.    nvarc ...

  9. Math类的round方法小解

    在Math类中有三个关于“四舍五入”的静态方法(ceil,floor,round): ① Math.ceil(number) 向上取整,Math.ceil(11.2) 结果:12            ...

  10. HDOJ3743<分治>

    题意:求一个排列的逆序数. #include<cstdio> #include<iostream> #include<algorithm> const int ma ...