题意:n点,m条边。m条边里面标记为1的最小生成树的边,0为非最小生成树的边。给了每条边的权,如果能构成一个最小生成树则输出图,否则-1。

思路:先按权值小,为生成数边的顺序排序。(根据kruskal)再添加每条0边。这里假定(1,3),(2,4)构成环。

 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3f3f3f3f
#define LL long long
const int SIGMA_SIZE = ;
const int MAXNODE = ;
const int MAXS = + ; int n,m;
struct node
{
int u,v,o;
bool operator <(const node &b)const
{
if(u==b.u)
return v>b.v;
return u<b.u;
}
} a[]; int b[];
int c[];
int fun()
{
int e=,x=,y=;
for(int i=;i<=m;i++)
{
if(a[i].v)
{
b[a[i].o]=e;
c[a[i].o]=++e;
}
else
{
if(y>e)
return ;
b[a[i].o]=x;
c[a[i].o]=y;
if(--x==)
{
y+=;
x=y-;
}
}
}
return ;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=; i<=m; i++)
{
scanf("%d%d",&a[i].u,&a[i].v);
a[i].o=i;
}
sort(a+,a++m);
if(fun())
{
for(int i=; i<=m; i++)
{
cout<<b[i]<<" "<<c[i]<<endl;
}
}
else
{
cout<<"-1"<<endl;
}
return ;
}

codeforce 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. CodeForces 605B Lazy Student

    构造.对边的权值排序,权值一样的话,在MST中的边排到前面,否则权值小的排在前面. 然后边一条一条扫过去,如果是1 ,那么连一个点到集合中,如果是0,集合内的边相连. #include<cstd ...

  3. CF#335 Lazy Student

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

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

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

  5. 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 ...

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

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

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

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

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

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

  9. C#性能优化之Lazy<T> 实现延迟初始化

    在.NET4.0中,可以使用Lazy<T> 来实现对象的延迟初始化,从而优化系统的性能.延迟初始化就是将对象的初始化延迟到第一次使用该对象时.延迟初始化是我们在写程序时经常会遇到的情形,例 ...

随机推荐

  1. [转]LINQ语句之Select/Distinct和Count/Sum/Min/Max/Avg

    在讲述了LINQ,顺便说了一下Where操作,这篇开始我们继续说LINQ语句,目的让大家从语句的角度了解LINQ,LINQ包括LINQ to Objects.LINQ to DataSets.LINQ ...

  2. linux下执行 ls,cat等一些命令报出 -bash: /bin/cat: Cannot allocate memory 有没解决的方法

    环境变量配置出错了cd -- 进入用户目录vim .bash_profile删除以前PATH这一行,把下面的粘帖进去PATH=$PATH:$HOME/bin:/root:/root/snapshot/ ...

  3. hdu 4678 Mine 博弈论

    这是一题简单的博弈论!! 所有的空白+边界的数字(个数为n)为一堆,容易推出其SG函数值为n%2+1: 其他所有的数字(个数为m)的SG值为m%2. 再就是用dfs将空白部分搜一下即可!(注意细节) ...

  4. P59、面试题7:用两个栈实现队列

    题目:用两个栈实现一个队列.队列声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列为插入结点和在队列头部删除结点的功能. stack1专门用于插入数据,stack2 ...

  5. clone函数

    http://blog.csdn.net/caianye/article/details/5947282 http://wenku.baidu.com/link?url=qnq7laYDYm1V8tl ...

  6. Uploadify参数详解

    属性 $('#file_upload').uploadify({    //一属性详解       id: jQuery(this).attr('id'),//绑定的input的ID       la ...

  7. javascript里的post和get有什么区别

    FORM中的get post方法区别Form中的get和post方法,在数据传输过程中分别对应了HTTP协议中的GET和POST方法.二者主要区别如下: 1.Get是用来从服务器上获得数据,而Post ...

  8. Complete The Pattern #1

    Complete The Pattern #1 Task: You have to write a function pattern which creates the following patte ...

  9. How does it work in C#? - Part 3 (C# LINQ in detail)

    http://www.codeproject.com/Articles/383749/How-does-it-work-in-Csharp-Part-Csharp-LINQ-in-d

  10. JsonPath详解

    JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPat ...