P8779 [蓝桥杯 2022 省 A] 推导部分和

并查集板子题

#include <bits/stdc++.h>
#define R(x) x = read()
#define RLL(x) x = readLL()
using namespace std;

typedef long long LL;
const int N = 1e5 + 5;

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 - '0';
        ch = getchar();
    }
    return x*f;
}

inline LL readLL()
{
    LL 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 - '0';
        ch = getchar();
    }
    return x*f;
}

int n, m, q;
int fa[N];
LL dis[N];

int find(int x)
{
    if(x == fa[x])
        return x;
    int oldFa = fa[x];
    fa[x] = find(fa[x]);
    dis[x] += dis[oldFa];
    return fa[x];
}

void init()
{
    for(int i = 1; i <= n; i++)
        fa[i] = i;
}

void printLL(LL x)
{
    if(x < 0)
    {
        putchar('-');
        x = -x;
    }
    if(x > 9)
        printLL(x/10);
    putchar(x%10+'0');
    return;
}

int main()
{
    R(n);R(m);R(q);
    init();
    for(int i = 1; i <= m; i++)
    {
        int l, r; LL s;
        R(l);R(r);RLL(s);
        l--;
        int fL = find(l), fR = find(r);
        if(fL == fR)
            continue;
        LL a = dis[l], b = dis[r];
        fa[fL] = fR;
        dis[fL] = s + b -a;
    }
    for(int i = 1; i <= q; i++)
    {
        int l, r;
        R(l);R(r);
        l--;
        int fL = find(l), fR = find(r);
        if(fL != fR)
            puts("UNKNOWN");
        else
            {printLL(dis[l] - dis[r]); puts("");}
    }
    return 0;
}

因为想复刻考场的感觉,于是没有用以前的板子,自己重写写了一遍快读,结果大意出锅了!

inline LL readLL()
{
    LL 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 - '0';
        ch = getchar();
    }
    return x*f;
}

 

f=-1后面的getchar()一开始忘了加,面对TLE的10个点不知所措——我这不是O(N)算法吗

😂

原来是后面开始出现负数,开始出现读入时无限循环的情况

幸好这道题数字之间只有一个空格,否则一个点都过不了()

posted @ 2024-04-10 21:34  Gold_stein  阅读(13)  评论(0编辑  收藏  举报