acwing 1192 奖金(拓扑排序)

news/2024/7/3 12:28:40 标签: 拓扑排序

题面

在这里插入图片描述

题解

  1. 跑一遍拓扑排序求出所有编号在图中的前后关系
  2. dist[i]:表示i点在拓扑图中离起点的最远距离(可能存在多起点),dist[起点] == 100,边的权值为1

代码

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N = 1e4 + 10, M = 2 * N;

int n, m;
int h[N], ne[M], e[M], idx;
int d[N];
int q[N];
int dist[N];


void add(int a, int b) {
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx++;
}

bool topsort() {
    int hh = 0, tt = -1;
    for (int i = 1; i <= n; i++) {
        if (!d[i]) q[++tt] = i;
    }

    while (hh <= tt) {
        int t = q[hh++];
        for (int i = h[t]; i != -1; i = ne[i]) {
            int j = e[i];
            if (--d[j] == 0) q[++tt] = j;
        }
    }
    if (tt == n - 1) return true;
    else return false;

}

int main() {

    memset(h, -1, sizeof h);
    cin >> n >> m;
    while (m--) {
        int a, b;
        cin >> a >> b;
        add(b, a);
        d[a]++;
    }
    if (topsort()) {
        for (int i = 1; i <= n; i++) dist[i] = 100;
        for (int i = 0; i < n; i++) {
            int j = q[i];
            for (int k = h[j]; k != -1; k = ne[k]) {
                dist[e[k]] = max(dist[e[k]], dist[j] + 1);
            }
        }
        int res=0;
        for(int i=1;i<=n;i++) res+=dist[i];
        cout<<res<<endl;
    } else {
        cout << "Poor Xed" << endl;
    }


    return 0;
}

http://www.niftyadmin.cn/n/1572079.html

相关文章

acwing 1220 生命之树 (树形DP)

题面 题解 代码 #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm>using namespace std; typedef long long ll; const int N 1e5 10, M N * 2;int n; ll w[N]; ll h[N], e[M], ne[M], idx; ll …

eclipse 自动提示

自动提示设置 Window—>Preferences---->Java—>Editor—>Content Assist 设置成 .abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

acwing 3188 manacher算法

题面 题解 我们用mancher算法&#xff0c;可以在O(n) 求出一个串的最大回文串 我们要将题中给定的串变为奇数串&#xff0c;比如abaca 就会变成 $#a#b#a#c#a^用一个p[i] 数组表示以si为中心的最大回文串的半径&#xff0c;那么最后求一个最大的半径-1&#xff0c;就是所求结果 …

acwing 3549 最长非递减子序列

题面 题解&#xff08;DP&#xff09; 代码 #include<bits/stdc.h>using namespace std; //111 222 1111 2222int main() {int n;scanf("%d", &n);int s1 0, s2 0, s3 0, s4 0;while (n--) {int x;scanf("%d", &x);if (x 1) {s1 1…

2017 ICPC西安 H Arrangement for Contests

题面 题解 线段树板子吧&#xff0c;维护一个区间最小值&#xff0c;懒标记更新即可 代码 #include<bits/stdc.h>using namespace std; typedef long long ll; const int N 1e5 10;int T, n, k; ll a[N];struct Node {ll l, r;ll minn;// 最小值ll lazy; //向下更新…

牛客小白月赛34 A dd爱科学1.0

题面 题解 f [i] [j] : 已经处理到了前 i 个字母&#xff0c;第 i 个字母是 j 的最小花费 因为这个序列是非递减的&#xff0c;所以前一个不能大于后一个&#xff0c;直接枚举第 i -1 个字母是什么&#xff0c;来转移方程 代码 #include<bits/stdc.h>using namespace st…

牛客小白月赛34 C dd爱科学2.0

题面 题解 f [i] [j] : 已经处理到了前 i 个字母&#xff0c;第 i 个字母是 j 的最小花费 因为这个序列是非递减的&#xff0c;所以前一个不能大于后一个&#xff0c;直接枚举第 i -1 个字母是什么&#xff0c;来转移方程,最小花费就是 s[i] 变到 字母 j‘A’ 的偏移量 代码 #…

codeforces 1529 B.Sifid and Strange Subsequences

题面 题意 给定一个序列&#xff0c;找一个最长的子序列&#xff0c;满足对于子序列的任意两个数ai,aj&#xff0c;&#xff08;1<i<j<k&#xff09;,使得 | ai - aj | > MAX (子序列中最大的数) 题解 对于子序列&#xff0c;如果都是负数或者0一定成立&#xff0c…