poj3687拓扑排序

反向建图

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>

using namespace std;
const int INF=0xfffffff;

int main()
{
    int n,m,Icase;
    int vis[300];
    int G[300][300];
    int in[300];
    int topo[300];
    while(cin>>Icase){
        while(Icase--){
            memset(topo,0,sizeof(topo));
            memset(G,0,sizeof(G));
            memset(vis,0,sizeof(vis));
            memset(in,0,sizeof(in));
            cin>>n>>m;
            for(int i=0;i<m;i++){
                int a,b;
                cin>>a>>b;
                if(!G[a][b]){
                    in[a]++;
                    G[a][b]=1;
                }
            }
            int flag=1;
            for(int i=n;i>=1;i--){
                int sign;
                bool t=false;
                for(int j=1;j<=n;j++){
                    if(in[j]==0&&!vis[j]){
                        sign=j; t=true;
                    }
                }
                if(!t){
                    flag=0;break;
                }
              //  vis[sign]=1;
                topo[i]=sign;
              //  cout<<topo[i]<<endl;system("pause");
                for(int j=1;j<=n;j++){
                    if(!vis[j]&&G[j][sign]){
                        in[j]--;
                    }
                }
                vis[sign]=1;
            }

            if(!flag)cout<<-1<<endl;
            else{
                int a[300];
                for(int i=1;i<=n;i++){
                    a[topo[i]]=i;
                }
                for(int i=1;i<=n;i++){
                    if(i==1) cout<<a[i];
                    else cout<<" "<<a[i];
                }
                cout<<endl;
            }
        }
    }
    return 0;
}

  

poj3687拓扑排序

时间: 2024-08-04 16:15:05

poj3687拓扑排序的相关文章

poj3687 拓扑排序 还没怎么搞明白 回头再想想

[题意]:n个重量为1~n的球,给定一些球之间的重量比较关系(如 2 1  表示第二个球比第一个球轻),求每个球可能的重量,ans[i] 表示第i个球的重量,要求输出的是ans字典序最小的情况. [思路]:对于给出的a b  建反边,每次 在出度为0的所有点里选一个序号最小的赋值(从n开始 由大到小赋). 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string>

ACM/ICPC 之 拓扑排序-反向(POJ3687)

难点依旧是题意....需要反向构图+去重+看题 POJ3687-Labeling Balls 题意:1-N编号的球,输出满足给定约束的按原编号排列的重量序列,如果有多组答案,则输出编号最小的Ball重量最轻,若依旧多组则输出编号次小的Ball重量最轻的方案. 题解:在看懂题意后,再开始做会省很多时间...曲解题意还真是人类的本能啊. 为了完成单向关系排序,需要用到拓扑排序: 为了符合编号最小的重量最轻...的题意,需要用到反向拓扑排序: 输入可能会有重复的关系,因此需要判重: 输出需要按原编号输

Labeling Balls POJ3687 【拓扑排序反向建边】【邻接表】

http://poj.org/problem?id=3687 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share the same label. The labeling satisfies several constrains like "The

POJ3687 Labeling Balls (拓扑排序)经典

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11469   Accepted: 3295 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share

POJ3687 反向拓扑排序

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16032   Accepted: 4713 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share

POJ 3687 Labeling Balls 逆向拓扑排序

链接: poj3687 题意: 有N个标号为1~N的小球,重量(不包括断言)依次增加 ,现给出M句断言 ,断言格式为a b 表示小球a轻于小球b     要求根据重量大小依次输出1~N号小球应在的的位置(重量递增)不满足断言则输出-1 题解: 因为重量是依次增加的  不能按常规的构造edge[a][b]=1生成拓扑排序 既然关系格式和一般拓扑排序是相反的  我们可以尝试着构造反向边edge[b][a]=1: indegree[a]++;   再根据小球标号从后往前进行拓扑排序,即可得到一幅完整的

拓扑排序讲解

在这里我们要说的拓扑排序是有前提的 我们在这里说的拓扑排序是基于有向无环图的!!!. (⊙o⊙)…我所说的有向无环图都知道是什么东西吧.. 如果不知道,我们下面先来来说说什么是有向无环图. 所谓有向无环图,顾名思义是不存在环的有向图(至于有向图是什么不知道的在前面我们有一个图论讲解上都有). 点的入度:以这个点为结束点的边数. 点的出度:以这个点为出发点的边的条数. 拓扑序就是对于一个节点的一个排列,使得(u,v)属于E,那么u一定出现在v的前面.然而拓扑排序就是一个用来求拓扑序的东西. 对于左

CSU 1804: 有向无环图(拓扑排序)

http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1804 题意:…… 思路:对于某条路径,在遍历到某个点的时候,之前遍历过的点都可以到达它,因此在这个时候对答案的贡献就是∑(a1 + a2 + a3 + ... + ai) * bv,其中a是之前遍历到的点,v是当前遍历的点. 这样想之后就很简单了.类似于前缀和,每次遍历到一个v点,就把a[u]加给a[v],然后像平时的拓扑排序做就行了. 1 #include <bits/stdc++.h>

7-9-有向图无环拓扑排序-图-第7章-《数据结构》课本源码-严蔚敏吴伟民版

课本源码部分 第7章  图 - 有向无环图拓扑排序 ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接??? <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码+习题集解析使用说明        课本源码合辑  链接??? <数据结构>课本源码合辑        习题集全解析  链接??? <数据结构题集>习题解析合辑        本源码引入的文件  链接? Status.h.SequenceStack.c.ALGraph.c