POJ【3687】Labeling Balls

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

POJ【3687】Labeling Balls

拓扑排序 球的重量分别1~n 需要给他们标号1~n
有m个条件 每个条件为a b 表示标号为a的球比b的球轻
尽量把轻的球给小编号
按顺序输出1~n编号对应的球
倒序建图拓扑即可

代码如下

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>

using namespace std;

int in[201],inn[201],wz[201];
bool mp[201][201];

bool Topo(int n,int op)
{
    int i,j,k;
    for(i = 1; i <= n; ++i) inn[i] = in[i];
    for(i = n; i >= 1; --i)
    {
        for(j = n; j >= 1; --j)
        {
            if(!inn[j]) break;
        }
        if(!j) return 0;
        wz[j] = i;
        inn[j]--;
        for(k = 1; k <= n; ++k)
        {
            if(mp[k][j])
            {
                if(!inn[k]) return 0;
                inn[k]--;
            }
        }
    }
    return 1;
}

int main()
{
    int n,m,t,a,b,i;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        memset(in,0,sizeof(in));
        memset(mp,0,sizeof(mp));
        while(m--)
        {
            scanf("%d %d",&a,&b);
            if(!mp[a][b])
                in[a]++;
            mp[a][b] = true;
        }
        if(Topo(n,0))
        {
            for(i = 1; i <= n; ++i)
            {
                if(i > 1) putchar(' ');
                printf("%d",wz[i]);
            }
            puts("");
        }else puts("-1");
    }
    return 0;
}
POJ3687Labeling Balls题解 可以先筛选编号大的球进行拓扑排序,注意图要反建。 阅读详情

相关推荐

POJ 3687 Labeling Balls

题目链接 : POJ3687 Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8714   Accepted: 2366 Description Windy has N balls of distinct weights

Salvare219 508

poj 3687 Labeling Balls

很简单的一道拓扑排序,也是第一次做。。。 附上代码: #include #include int map[202][202]; int vis[202]; int cnt[202]; int ans[202]; int main() { int t; scanf("%d",&t); while(t--) { memset(ma

烟杀流年 1056

POJ 3687 Labeling Balls(拓扑排序)

POJ 3687 Labeling Balls(拓扑排序) http://poj.org/problem?id=3687 题意:给你N个球,编号从1到N.且这N个球的重量各不相同,且他们的重量正好是从1到N个单位.现在还给出了M个关系,每个关系描述了两个不同编号球之间的重量关系.现在要你输出这N个球可能的重量,从1号球到N号球.如果存在多解,则输出1号球最轻的解.如果1号球最轻时依然有多解,那

code 820

POJ 3687 Labeling Balls

Description Windy hasNballs of distinct weights from 1 unit toNunits. Now he tries to label them with 1 toNin such a way that: No two balls share the same label. The labeling satisfi...

dengcuoruan4618的博客 120

POJ——T 3687 Labeling Balls

http://poj.org/problem?id=3687 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14842 Accepted: 4349 Description Windy has N balls of distinct weight...

agcozdwdfvds08078的博客 66

POJ3687Labeling Balls(拓扑排序)

题目地址:

scf0920 940

POJ-3687 Labeling Balls

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 seve

Wilbert的博客 908

POJ 3687 Labeling Balls

反向拓扑排序 反向拓扑排序 • 所谓反向拓扑排序,就是在拓扑排序的基础上,再增加了一个要 求: • 条件1:编号最小的节点要尽量排在前面;(也就是首先保证答案序列在所有序列中编号为1 的 节点在所有序列中的位置是最靠前的) • 条件2:在满足条件1的基础上,编号第二小的节点要尽量排在前面; • 条件3:在满足条件1和条件2的基础上,编号第三小的节点要尽量排在前 面; • …… • 依此类推。 比如上图,好好理解为什么第一个输出的节点不是3 二是6 Labeling Balls Time Limit:

苦鬼·。 276

poj3687 Labeling Balls

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13417   Accepted: 3874 Description Windy has N balls of distinct weights from 1 unit to N un

樱花满地集于我心,楪舞纷飞祈愿相随 426

poj 3687--Labeling Balls

Description Windy hasNballs of distinct weights from 1 unit toNunits. Now he tries to label them with 1 toNin such a way that: No two balls share the same label. The labeling sat...

dianwei7259的博客 170

Labeling BallsPOJ-3687

Problem 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 satisfi...

Alex_McAvoy的博客 459
上一篇: 【POJ 3267】 The Cow Lexicon
下一篇: 【HDOJ 5288】OO’s Sequence
小胡子Haso
博客等级 码龄12年 178粉丝 367原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值