查错 CH Round #57 - Story of the OI Class
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2357%20-%20Story%20of%20the%20OI%20Class/查错
题解:刚开始看见立马以为是 p1790拓扑编号,于是改了下输出就交了。。。
然后就爆零了。。。
仔细看题发现:
本题要求 尽早处理编号小的点
尽量使编号小的点最早被处理
难道不一样?
后来发现了 p1790的样例:
5 4
4 1
1 3
5 3
2 5
然后就明白了
对p1790来说,要想1号尽早被处理,第一次就得处理4
而本题要求处理顺序最小 那么第一次处理的必须得是2
然后就呵呵了
所以这两题的解法分别是:
本题:正向贪心,每次选取入度为0的且编号最小的点
p1790:反向贪心,反向建图,每次选取出度为0且编号最大的点
代码:(本题)
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 250000
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,m,head[maxn],inp[maxn],ans[maxn],cnt;
struct edge{int go,next;}e[maxn];
priority_queue<int,vector<int>,greater<int> >q;
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();m=read();
for1(i,m)
{
int x=read(),y=read();
e[i].go=y;e[i].next=head[x];head[x]=i;inp[y]++;
}
for1(i,n)if(!inp[i])q.push(i);
while(!q.empty())
{
int x=q.top();q.pop();ans[++cnt]=x;
for(int i=head[x],y;i;i=e[i].next)
{
inp[y=e[i].go]--;
if(!inp[y])q.push(y);
}
}
if(cnt<n)printf("OMG.\n");else for1(i,n)printf("%d ",ans[i]);
return ;
}
(p1790)
var head,q,next,go,a,f,outp:array[..] of longint;
i,h,t,n,m,x,y,l,tot:longint;
function get:longint;
var i,k:longint;
begin
get:=a[];
a[]:=a[l];dec(l);
i:=;k:=;
while k<=l do
begin
if (k<l) and (a[k]<a[k+]) then inc(k);
if (a[]<a[k]) then begin a[i]:=a[k];i:=k;k:=k<<;end
else k:=l+;
end;
a[i]:=a[];
end;
procedure put(x:longint);
var i,k:longint;
begin
inc(l);
a[]:=x;
i:=l;k:=l>>;
while k>= do
begin
if a[]>a[k] then begin a[i]:=a[k];i:=k;k:=k>>;end
else k:=;
end;
a[i]:=x;
end;
procedure init;
begin
readln(n,m);
for i:= to m do
begin
readln(x,y);
go[i]:=x;
next[i]:=head[y];
head[y]:=i;
inc(outp[x]);
end;
end;
procedure main;
begin
h:=;t:=;
for i:= to n do
if outp[i]= then
put(i);
tot:=n;
while l<> do
begin
x:=get;
f[x]:=tot;dec(tot);
i:=head[x];
while i<> do
begin
y:=go[i];
dec(outp[y]);
if outp[y]= then put(y);
i:=next[i];
end;
end;
for i:= to n do write(f[i],' ');
end;
begin
init;
main;
end.
查错 CH Round #57 - Story of the OI Class的更多相关文章
- CH Round #57 - Story of the OI Class 凯撒密码
很有意思的一道题目 考场上想的是HASH成一个整数,把末位asicc码值*1,依次乘*10,得到一个整数,然后利用等差性.唯一性快排Nlogn乱搞的 证明如下: 对于明文abcde 密文 bcdef ...
- 【拓扑排序】【堆】CH Round #57 - Story of the OI Class 查错
拓扑排序,要让字典序最小,所以把栈改成堆. #include<cstdio> #include<queue> #include<algorithm> using n ...
- 导航软件 CH Round #57 - Story of the OI Class
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2357%20-%20Story%20of%20the%20OI%20Class/导航软件 题解:刚开始看见题目, ...
- 凯撒密码 CH Round #57 - Story of the OI Class
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2357%20-%20Story%20of%20the%20OI%20Class/凯撒密码 题解:刚开始想map, ...
- CH Round #72树洞[二分答案 DFS&&BFS]
树洞 CH Round #72 - NOIP夏季划水赛 描述 在一片栖息地上有N棵树,每棵树下住着一只兔子,有M条路径连接这些树.更特殊地是,只有一棵树有3条或更多的路径与它相连,其它的树只有1条或2 ...
- CH Round #52 还教室[线段树 方差]
还教室 CH Round #52 - Thinking Bear #1 (NOIP模拟赛) [引子]还记得 NOIP 2012 提高组 Day2 中的借教室吗?时光飞逝,光阴荏苒,两年过去了,曾经借教 ...
- CH Round #30 摆花[矩阵乘法]
摆花 CH Round #30 - 清明欢乐赛 背景及描述 艺术馆门前将摆出许多花,一共有n个位置排成一排,每个位置可以摆花也可以不摆花.有些花如果摆在相邻的位置(隔着一个空的位置不算相邻),就不好看 ...
- 使用DBUnit框架数据库插入特殊字符失败的查错经历
本文记录的是使用DBUnit测试框架进行数据库数据插入时,插入特殊字符失败的查错经历.希望能对向我这样的小白同学们在遇到类似问题时,能够有一些启发.背景:在写跟数据库交互模块的单元测试,数据库表中的e ...
- contesthunter CH Round #64 - MFOI杯水题欢乐赛day1 solve
http://www.contesthunter.org/contest/CH Round %2364 - MFOI杯水题欢乐赛 day1/Solve Solve CH Round #64 - MFO ...
随机推荐
- 将MVC中的Controllers、Model和View分别放到单独的项目中
Model: 新建-项目-Windows-类库 MVCTest.Model Controller:新建-项目-Windows-控制台应用程序 MVCTest.Bussiness Views:新建-项目 ...
- 函数对象的prototype总结
通过看 http://www.cnblogs.com/mindsbook/archive/2009/09/19/javascriptYouMustKnowPrototype.html 该文章和对代码的 ...
- 强大的Core Image框架,各种滤镜处理图像
首先介绍一下Core Image,他是一个很强大的图像处理框架,他可以让你简单的应用各种滤镜来处理图像,比如说色相,饱和度,亮度等等...他是运用GPU(CPU)实时地处理图像数据和视频的帧.而且Co ...
- iOS自动布局之autoresizingi
对于iOS的app开发者来说,不会像Android开发者一样为很多的屏幕尺寸来做界面适配,因此硬编码的坐标也能工作良好,但是从设计模式上来说这不是好的做法.而且也还有一些问题,如iPhone5的适配, ...
- python中的几种集成分类器
from sklearn import ensemble 集成分类器(ensemble): 1.bagging(ensemble.bagging.BaggingClassifier) 对随机选取的子样 ...
- java.lang.String类compareTo()返回值解析
一.compareTo()的返回值是int,它是先比较对应字符的大小(ASCII码顺序)1.如果字符串相等返回值02.如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值(ascii码值 ...
- ios开发之代理&&协议
Object-C是不支持多继承的,所以很多时候都是用Protocol(协议)来代替.Protocol(协议)只能定义公用的一套接口,但不能提供具体的实现方法.也就是说,它只告诉你要做什么,但具体怎么做 ...
- SVN更新失败,提示locked
使用SVN更新资源时,提示locked,解决方案如下: 首先找到是哪个文件不能进行更新/提交,在本地工作区间中找到这个文件对应的目录,目录里面会有.svn文件夹,这个文件夹默认是隐藏的,需要设置文件夹 ...
- ie8 background css没有显示?——都是空格惹的祸
ie8 background css没有显示?——都是空格惹的祸
- yii2源码学习笔记(三)
组件(component),是Yii框架的基类,实现了属性.事件.行为三类功能,如果需要事件和行为的功能,需要继承该类. yii\base\Component代码详解 <?php /** * @ ...