查错 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 ...
随机推荐
- xslt语法之---position()函数
最近在学习使用XSLT,很好很强大的样式表语言.使用到了position()函数特此记录一下. position()函数--返回节点位置 语法:position() 参数:无 返回值:整数 用途:该函 ...
- html_entity_decode() 函数
html_entity_decode() 函数 定义和用法 The html_entity_decode() function converts HTML entities to characte ...
- order by
- window.showModalDialog 子窗口和父窗口不兼容最新的谷歌
最新版的谷歌不支持window.showModalDialog的写法,会出现,找不到方法的问题,同时返回值的方法window.dialogArguments;也用不了. 这里就只能用最原版的windo ...
- 我的网站终于连续一星期,ip数1000以上了
相信每个程序员都有一个站长梦,我也是如此.说来惭愧,从2015年4月份开始,我投入了50块的域名费用,50块的空间费用,现在才回本. 由于终于从百度联盟的手里赚了一百块,按耐不住的菜鸟之冲动,于是决定 ...
- 开源的Android开发框架-------PowerFramework使用心得(五)网络请求HTTPRequest
GET请求示例 //所有参数都使用Bundle,用putString Bundle bundle = new Bundle(); bundle.putString("username&quo ...
- Android开发--二维码开发应用(转载!)
android项目开发 二维码扫描 基于android平台的二维码扫描项目,可以查看结果并且链接网址 工具/原料 zxing eclipse 方法/步骤 首先需要用到google提供的zxin ...
- 用java写bp神经网络(三)
孔子曰,吾日三省吾身.我们如果跟程序打交道,除了一日三省吾身外,还要三日一省吾代码.看代码是否可以更简洁,更易懂,更容易扩展,更通用,算法是否可以再优化,结构是否可以再往上抽象.代码在不断的重构过程中 ...
- 使用Xcode插件,让iOS开发更加便捷
在iOS开发过程中,写注释是一项必不可少的工作.这不仅有助于自己对代码整理回顾,而且提高了代码的可读性,让代码维护变得容易.但是,写注释又是一项枯燥的工作.我们浪费了大量的时间在输入/*,*,*/这样 ...
- 文字排版--斜体(font-style)
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...