跳到正文

个人记录

1007模拟赛T3

hanyihong_约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年10月7日
收录
2026年11月11日
更新
2026年11月19日
篇幅
约 3 分钟
获赞
1

版本

  1. v1当前
C++
#include <bits/stdc++.h>
#define int long long // 十年oi一场空,不开long long见祖宗
using namespace std;
const signed N = 1e6 + 5, mod = 1e9 + 7, M = 2000005; // 记得取模
int n, m, T, u, v, c, in[N], f[N], head[N], cnt;      // in是入度,f是答案
struct Edge
{
    int to, w, next;
} edge[M]; // 链式前向星存图
inline void add(int u, int v, int w)
{
    edge[cnt].to = v, edge[cnt].w = w, edge[cnt].next = head[u];
    head[u] = cnt++;
}
void topo() // 拓扑排序
{
    queue<int> q;
    for (int i = 1; i <= n; i++)
    {
        if (!in[i])
            q.push(i);
        f[i] = 1;
    }
    while (!q.empty())
    {
        int x = q.front();
        q.pop();
        for (int i = head[x]; i != -1; i = edge[i].next)
        {
            f[edge[i].to] = (f[edge[i].to] + (f[x] * edge[i].w) % mod + mod) % mod; // 更新答案
            if (--in[edge[i].to] == 0)
                q.push(edge[i].to);
        }
    }
}
void init() // 初始化head,防止死循环
{
    memset(head, -1, sizeof(head));
    cnt = 0;
}
inline int read() // 快读板子
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = x * 10 + ch - 48;
        ch = getchar();
    }
    return x * f;
}
signed main()
{
    n = read(), m = read(), T = read();
    init();
    for (int i = 1; i <= m; i++)
    {
        u = read(), v = read(), c = read();
        add(u, v, c), in[v]++; // 加边,更新入度
    }
    topo();
    while (T--)
    {
        int x;
        x = read();
        cout << f[x] << '\n';
    }
    return 0;
} // 这是图片,复制不了一点,气不气
原文