优先队列 + 反向拓扑

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
const int maxn = 1e5 + ;
int n, m;
vector<int>G[maxn];
int inDeg[maxn];
int dp[maxn];
int cnt;
int sum ,ans ;
int mp[][];
bool topsort()
{
//queue<int>q;
priority_queue<int>q;
//queue<int>q;
cnt = ;
sum = ;
while(!q.empty())q.pop();
for(int i = ; i <= n ; i++) if(!inDeg[i]) q.push(i);
while(!q.empty())
{
int now = q.top();
q.pop();
// cout << now << endl;
sum ++;
dp[now] = n--;
// cout << dp[now] << endl;
for(int i = ; i < G[now].size(); i++)
{
int to = G[now][i];
inDeg[to]--;
if(!inDeg[to]) q.push(to);
// dp[to] = max(dp[to],dp[now] + G[now][i].second);
} }
if(sum == ans)return ;
else return ;
} int main()
{
int t;
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
// cin >> t;
cin >> t;
while(t--){ cin >> n >> m;
ans = n;
memset(inDeg,,sizeof inDeg);
memset(dp,,sizeof dp);
for(int i = ; i <= n ; i++) G[i].clear();
while(m--)
{
int u, v, w;
cin >> u >> v ; //if(mp[u][v] == 0)
G[v].push_back(u); //G[v].push_back(make_pair((u,w)));
inDeg[u] ++;
}
if(topsort())
//int maxx = 0;
for(int i = ; i <= ans ; i++)
printf("%d%c",dp[i]," \n"[i == ans]);
else
puts("-1");
}
return ;
}

Labeling Balls POJ - 3687 优先队列 + 反向拓扑的更多相关文章

  1. POJ 3687 Labeling Balls 逆向建图,拓扑排序

    题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...

  2. HDU 4857 逃生 (优先队列+反向拓扑)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 解题报告:有n个点,有m个条件限制,限制是像这样的,输入a  b,表示a必须排在b的前面,如果不 ...

  3. POJ 3687 逆序拓扑

    额.题目大意:有N个球.编号和重量都是唯一不重复的.然后.给你m个pair a和b,表示编号为a的点一定比编号为b的点轻.然后捏.输出每个点对应的重量.关键是要求.如果有多种可能性的话,输出让序号小的 ...

  4. [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 D ...

  5. POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16100   Accepted: 4726 D ...

  6. poj 3687 Labeling Balls【反向拓扑】

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12246   Accepted: 3508 D ...

  7. POJ 3687:Labeling Balls(优先队列+拓扑排序)

    id=3687">Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10178 Acc ...

  8. POJ3687——Labeling Balls(反向建图+拓扑排序)

    Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...

  9. poj——3687 Labeling Balls

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14835   Accepted: 4346 D ...

随机推荐

  1. microsoft office 2010 visio激活

    office2010 tookit这款绿色的软件进行Visio2010激活 http://jingyan.baidu.com/article/4f34706ecae169e387b56dd1.html

  2. MySQL + centos +主从复制

    MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的 ...

  3. 7、kubernetes资源清单之Service资源190714

    一.Service简介 Service为Pod提供固定服务端点 Service的本质是一条iptables或者ipvs的转发规则 userspace:1.1- iptables:1.1+ ipvs:1 ...

  4. ASP.NET中的物理路径与虚拟路径

    物理路径:c:\PathsAndURLs\Content\Colors.html虚拟路径:(http://localhost:53274/Content/Colors.html)路径中端口号后面的那部 ...

  5. 8.进行图片的裁剪,同时使用resize将图片的维度进行变化

    1.img.crop((x1, y1, x2, y2)) 进行图片的裁剪 参数说明: x1, y1, x2, y2 表示图片的大小 2. img.resize((w, h)) # 进行图片的维度变化 ...

  6. 8.6培训 D1

    今天是赵和旭老师讲课(也是 zhx) 动态规划 利用最优化原理把多阶段过程转化为一系列单阶段问题,利用各阶段之间的关系,逐个求解(有点像分治?) 更具体的,假设我们可以计算出小问题的最优解,那么我们凭 ...

  7. SAEJ1757-1-2015(一)

    SURFACE VEHICLE STANDARD           表面车辆标准 Standard Metrology for Vehicular Displays             车载显示 ...

  8. 第八章 SpringCloud之Feign、Hystrix结合使用

    #这个章节主要是针对Hystrix的使用,因为Feign的章节在上一节已经实现了,整个代码也是在上一个章节的基础上修改的 ##################Hystrix一个简单Demo实现#### ...

  9. idea 编译 netty 源码

    git clone netty 源码,运行 example 报错 全量 mvn compile -DskipTests=true 后,依然报错 手动在 netty-buffer 模块中添加对应的依赖 ...

  10. Git使用手册/Git教程:git fetch 将远程仓库的分支及分支最新版本代码拉取到本地

    相关文章: 关于验证是否存在ssh配置以及生成SSH Key的方法可以参照文章:Git使用手册:生成SSH Key 关于SSH Key的使用和公钥在gitHub.gitLab的配置等,请参考文章:Gi ...