题目: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且编号最大的点 

代码:(本题)

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 250000
14 #define maxm 500+100
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define for0(i,n) for(int i=0;i<=(n);i++)
19 #define for1(i,n) for(int i=1;i<=(n);i++)
20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
22 #define mod 1000000007
23 using namespace std;
24 inline int read()
25 {
26     int x=0,f=1;char ch=getchar();
27     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
28     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
29     return x*f;
30 }
31 int n,m,head[maxn],inp[maxn],ans[maxn],cnt;
32 struct edge{int go,next;}e[maxn];
33 priority_queue<int,vector<int>,greater<int> >q;
34 int main()
35 {
36     freopen("input.txt","r",stdin);
37     freopen("output.txt","w",stdout);
38     n=read();m=read();
39     for1(i,m)
40     {
41         int x=read(),y=read();
42         e[i].go=y;e[i].next=head[x];head[x]=i;inp[y]++;
43     }
44     for1(i,n)if(!inp[i])q.push(i);
45     while(!q.empty())
46     {
47         int x=q.top();q.pop();ans[++cnt]=x;
48         for(int i=head[x],y;i;i=e[i].next)
49          {
50              inp[y=e[i].go]--;
51              if(!inp[y])q.push(y);
52          }
53     }
54     if(cnt<n)printf("OMG.\n");else for1(i,n)printf("%d ",ans[i]);
55     return 0;
56 }
View Code

相关文章: